From adb1381e432a27bb3749813ee57d95c8f217b25d Mon Sep 17 00:00:00 2001 From: Andreas Schempp Date: Wed, 8 Dec 2021 14:50:14 +0100 Subject: [PATCH] Correctly handle unroutable pages --- src/FrontendModule/ChangeLanguageModule.php | 20 +++++++++++++------- src/Navigation/NavigationItem.php | 19 +++++++++++++++++-- 2 files changed, 30 insertions(+), 9 deletions(-) diff --git a/src/FrontendModule/ChangeLanguageModule.php b/src/FrontendModule/ChangeLanguageModule.php index 5a14275..73503d8 100755 --- a/src/FrontendModule/ChangeLanguageModule.php +++ b/src/FrontendModule/ChangeLanguageModule.php @@ -7,9 +7,11 @@ use Contao\FrontendTemplate; use Contao\Input; use Contao\PageModel; +use Contao\StringUtil; use Contao\System; use Haste\Frontend\AbstractFrontendModule; use Haste\Generator\RowClass; +use Symfony\Component\Routing\Exception\ExceptionInterface; use Terminal42\ChangeLanguage\Event\ChangelanguageNavigationEvent; use Terminal42\ChangeLanguage\Helper\AlternateLinks; use Terminal42\ChangeLanguage\Helper\LanguageText; @@ -100,10 +102,14 @@ protected function compile(): void } if ($item->isDirectFallback() && !$headerLinks->has($item->getLanguageTag())) { - $headerLinks->addFromNavigationItem($item, $urlParameters); - - if ($item->getRootPage()->fallback && !$item->getRootPage()->languageRoot) { - $headerLinks->setDefault($item->getHref($urlParameters), $item->getTitle()); + try { + $headerLinks->addFromNavigationItem($item, $urlParameters); + + if ($item->getRootPage()->fallback && !$item->getRootPage()->languageRoot) { + $headerLinks->setDefault($item->getHref($urlParameters), $item->getTitle()); + } + } catch (ExceptionInterface $e) { + // Ignore unroutable pages } } @@ -131,9 +137,9 @@ protected function generateTemplateArray(NavigationItem $item, UrlParameterBag $ 'class' => 'lang-'.$item->getNormalizedLanguage().($item->isDirectFallback() ? '' : ' nofallback').($item->isCurrentPage() ? ' active' : ''), 'link' => $item->getLabel(), 'subitems' => '', - 'href' => specialchars($item->getHref($urlParameterBag)), - 'title' => specialchars(strip_tags($item->getTitle())), - 'pageTitle' => specialchars(strip_tags($item->getPageTitle())), + 'href' => StringUtil::specialchars($item->getHref($urlParameterBag, true)), + 'title' => StringUtil::specialchars(strip_tags($item->getTitle())), + 'pageTitle' => StringUtil::specialchars(strip_tags($item->getPageTitle())), 'accesskey' => '', 'tabindex' => '', 'nofollow' => false, diff --git a/src/Navigation/NavigationItem.php b/src/Navigation/NavigationItem.php index 41e9913..52b8b02 100644 --- a/src/Navigation/NavigationItem.php +++ b/src/Navigation/NavigationItem.php @@ -5,6 +5,7 @@ namespace Terminal42\ChangeLanguage\Navigation; use Contao\PageModel; +use Symfony\Component\Routing\Exception\ExceptionInterface; use Terminal42\ChangeLanguage\Language; class NavigationItem @@ -196,8 +197,10 @@ public function getLocaleId() /** * @return string + * + * @throws ExceptionInterface */ - public function getHref(UrlParameterBag $urlParameterBag) + public function getHref(UrlParameterBag $urlParameterBag, bool $catch = false) { $targetPage = $this->targetPage ?: $this->rootPage; @@ -205,7 +208,19 @@ public function getHref(UrlParameterBag $urlParameterBag) $targetPage = PageModel::findFirstPublishedRegularByPid($targetPage->id) ?: $targetPage; } - $href = $targetPage->getAbsoluteUrl($urlParameterBag->generateParameters()); + try { + $href = $targetPage->getAbsoluteUrl($urlParameterBag->generateParameters()); + } catch (ExceptionInterface $e) { + if (!$catch) { + throw $e; + } + + $this->targetPage = null; + $this->isDirectFallback = false; + $this->isCurrentPage = false; + + return $this->getHref($urlParameterBag); + } if (null !== ($queryString = $urlParameterBag->generateQueryString())) { $href .= '?'.$queryString;