Last active
January 30, 2019 13:28
-
-
Save hissy/b21f5ff4e778a9fbf0372d7810a0b917 to your computer and use it in GitHub Desktop.
[concrete5] MultilingualPageUrlResolver: Get localized page path from original page path
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
// application/src/Concrete/Url/MultilingualPageUrlResolver.php | |
namespace Application\Concrete\Url; | |
use Concrete\Core\Application\ApplicationAwareInterface; | |
use Concrete\Core\Application\ApplicationAwareTrait; | |
use Concrete\Core\Multilingual\Page\Section\Section; | |
use Concrete\Core\Page\Page; | |
use Concrete\Core\Url\Resolver\PageUrlResolver; | |
use Concrete\Core\Url\Resolver\UrlResolverInterface; | |
/** | |
* Multilingual Page Url Resolver. | |
* | |
* \URL::to('/foo') | |
* -> 'http://www.example.com/foo' in default section | |
* -> 'http://www.example.com/ja/foo' in multilingual section | |
*/ | |
class MultilingualPageUrlResolver implements UrlResolverInterface, ApplicationAwareInterface | |
{ | |
use ApplicationAwareTrait; | |
/** | |
* @var PageUrlResolver | |
*/ | |
protected $pageUrlResolver; | |
/** | |
* MultilingualPageUrlResolver constructor. | |
* | |
* @param PageUrlResolver $pageUrlResolver | |
*/ | |
public function __construct(PageUrlResolver $pageUrlResolver) | |
{ | |
$this->pageUrlResolver = $pageUrlResolver; | |
} | |
/** | |
* @param array $arguments | |
* @param \League\URL\URLInterface|null $resolved | |
* | |
* @return \League\URL\URLInterface|null | |
*/ | |
public function resolve(array $arguments, $resolved = null) | |
{ | |
if ($resolved) { | |
// We don't need to do any post processing on urls. | |
return $resolved; | |
} | |
if ($this->app->make('multilingual/detector')->isEnabled()) { | |
if ($arguments) { | |
$page = head($arguments); | |
} | |
if (isset($page) && (is_scalar($page) || (is_object($page) && method_exists($page, '__toString')))) { | |
$path = (string) $page; | |
if ($path == '/' || $path == '') { | |
$page = Page::getByID(Page::getHomePageID()); | |
} else { | |
$page = Page::getByPath($path); | |
} | |
} | |
if (isset($page) && $page instanceof Page && !$page->isError() && $page->isActive()) { | |
$c = Page::getCurrentPage(); | |
if (is_object($c) && !$c->isError()) { | |
$section = Section::getBySectionOfSite($c); | |
} | |
$pageSection = Section::getBySectionOfSite($page); | |
if (isset($section) && is_object($section) && is_object($pageSection) && $section->getLocale() != $pageSection->getLocale()) { | |
$relatedID = $section->getTranslatedPageID($page); | |
if ($relatedID) { | |
$relatedPage = Page::getByID($relatedID); | |
if ($relatedPage && !$relatedPage->isError()) { | |
array_shift($arguments); | |
array_unshift($arguments, $relatedPage); | |
return $this->pageUrlResolver->resolve($arguments); | |
} | |
} | |
} | |
} | |
} | |
return null; | |
} | |
} |
I had to do something similar (in functionality) one time. Does this work with more than one multilingual section?
@eskema Yes, it works.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
How to add this resolver to your URL manager: add below code to
application/bootstrap/app.php