Skip to content

Commit

Permalink
Use the content URL generator if available (#223)
Browse files Browse the repository at this point in the history
  • Loading branch information
aschempp committed May 15, 2024
1 parent cff3749 commit 1efdc6f
Showing 1 changed file with 9 additions and 1 deletion.
10 changes: 9 additions & 1 deletion src/Navigation/NavigationItem.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
namespace Terminal42\ChangeLanguage\Navigation;

use Contao\PageModel;
use Contao\System;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Symfony\Component\Routing\Exception\ExceptionInterface;
use Terminal42\ChangeLanguage\Language;

Expand Down Expand Up @@ -163,7 +165,13 @@ public function getHref(UrlParameterBag $urlParameterBag, bool $catch = false):
}

try {
$href = $targetPage->getAbsoluteUrl($urlParameterBag->generateParameters());
$container = System::getContainer();

if ($urlGenerator = $container->get('contao.routing.content_url_generator', ContainerInterface::NULL_ON_INVALID_REFERENCE)) {
$href = $urlGenerator->generate($targetPage, ['parameters' => $urlParameterBag->generateParameters()]);
} else {
$href = $targetPage->getAbsoluteUrl($urlParameterBag->generateParameters());
}
} catch (ExceptionInterface $e) {
if (!$catch) {
throw $e;
Expand Down

0 comments on commit 1efdc6f

Please sign in to comment.