Skip to content

Instantly share code, notes, and snippets.

@hissy
Last active January 30, 2019 13:28
Show Gist options
  • Save hissy/b21f5ff4e778a9fbf0372d7810a0b917 to your computer and use it in GitHub Desktop.
Save hissy/b21f5ff4e778a9fbf0372d7810a0b917 to your computer and use it in GitHub Desktop.
[concrete5] MultilingualPageUrlResolver: Get localized page path from original page path
<?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;
}
}
@hissy
Copy link
Author

hissy commented Jan 22, 2019

How to add this resolver to your URL manager: add below code to application/bootstrap/app.php

use Concrete\Core\Url\Resolver\Manager\ResolverManagerInterface;
use Application\Concrete\Url\MultilingualPageUrlResolver;

$urlResolverManager = $this->app->make(ResolverManagerInterface::class);
$urlResolverManager->addResolver('application.multilingual', $this->app->make(MultilingualPageUrlResolver::class));

@eskema
Copy link

eskema commented Jan 22, 2019

I had to do something similar (in functionality) one time. Does this work with more than one multilingual section?

@hissy
Copy link
Author

hissy commented Jan 30, 2019

@eskema Yes, it works.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment