From a69916d7cde65108b06e72e05d03684fcfd0570d Mon Sep 17 00:00:00 2001 From: Kamil Kuzminski Date: Thu, 16 Feb 2023 07:07:20 +0100 Subject: [PATCH] Add the aria-label attribute to links in the frontend module (#222) --- contao/languages/de/default.xlf | 5 ++++- contao/languages/en/default.xlf | 3 +++ src/FrontendModule/ChangeLanguageModule.php | 11 +++++------ src/Navigation/NavigationFactory.php | 12 +++++++++++- src/Navigation/NavigationItem.php | 11 +++++++++++ 5 files changed, 34 insertions(+), 8 deletions(-) diff --git a/contao/languages/de/default.xlf b/contao/languages/de/default.xlf index fb5df3c..977fcb0 100644 --- a/contao/languages/de/default.xlf +++ b/contao/languages/de/default.xlf @@ -21,6 +21,9 @@ Switch to %s Zu %s wechseln + + Zur aktuelle Seite auf %s wechseln + - \ No newline at end of file + diff --git a/contao/languages/en/default.xlf b/contao/languages/en/default.xlf index b033a6e..d362a75 100644 --- a/contao/languages/en/default.xlf +++ b/contao/languages/en/default.xlf @@ -21,6 +21,9 @@ Switch to %s + + Go to current page in %s + diff --git a/src/FrontendModule/ChangeLanguageModule.php b/src/FrontendModule/ChangeLanguageModule.php index 7ad832c..ca5fa04 100755 --- a/src/FrontendModule/ChangeLanguageModule.php +++ b/src/FrontendModule/ChangeLanguageModule.php @@ -35,10 +35,7 @@ class ChangeLanguageModule extends Module private static ?AlternateLinks $alternateLinks = null; - /** - * @return AlternateLinks - */ - public function getAlternateLinks() + public function getAlternateLinks(): AlternateLinks { if (null === self::$alternateLinks) { self::$alternateLinks = new AlternateLinks(); @@ -83,7 +80,7 @@ protected function compile(): void $languageText = new LanguageText(); } - $navigationFactory = new NavigationFactory($pageFinder, $languageText, $currentPage); + $navigationFactory = new NavigationFactory($pageFinder, $languageText, $currentPage, System::getContainer()->get('contao.intl.locales')->getLocales()); $navigationItems = $navigationFactory->findNavigationItems($currentPage); // Do not generate module or header if there is none or only one link @@ -146,8 +143,10 @@ protected function generateTemplateArray(NavigationItem $item, UrlParameterBag $ 'accesskey' => '', 'tabindex' => '', 'nofollow' => false, - 'target' => ($item->isNewWindow() ? ' target="_blank"' : '').' hreflang="'.$item->getLanguageTag().'" lang="'.$item->getLanguageTag().'"', + 'rel' => ' hreflang="'.$item->getLanguageTag().'"'.(empty($item->getAriaLabel() ? '' : ' aria-label="'.$item->getAriaLabel().'"')), + 'target' => ($item->isNewWindow() ? ' target="_blank"' : ''), 'item' => $item, + 'languageTag' => $item->getLanguageTag(), ]; } diff --git a/src/Navigation/NavigationFactory.php b/src/Navigation/NavigationFactory.php index f6a0ad7..f4b3e8d 100644 --- a/src/Navigation/NavigationFactory.php +++ b/src/Navigation/NavigationFactory.php @@ -14,12 +14,14 @@ class NavigationFactory private PageFinder $pageFinder; private LanguageText $languageText; private PageModel $currentPage; + private array $locales = []; - public function __construct(PageFinder $pageFinder, LanguageText $languageText, PageModel $currentPage) + public function __construct(PageFinder $pageFinder, LanguageText $languageText, PageModel $currentPage, array $locales = []) { $this->pageFinder = $pageFinder; $this->languageText = $languageText; $this->currentPage = $currentPage; + $this->locales = $locales; } /** @@ -39,6 +41,14 @@ public function findNavigationItems(PageModel $currentPage): array ); foreach ($navigationItems as $item) { + if (isset($this->locales[$item->getLocaleId()])) { + $item->setAriaLabel( + $item->isDirectFallback() + ? sprintf($GLOBALS['TL_LANG']['MSC']['gotoLanguage'], $this->locales[$item->getLocaleId()]) + : sprintf($GLOBALS['TL_LANG']['MSC']['switchLanguageTo'][1], $this->locales[$item->getLocaleId()]) + ); + } + if (!$item->hasTargetPage()) { try { $item->setTargetPage( diff --git a/src/Navigation/NavigationItem.php b/src/Navigation/NavigationItem.php index a069ea8..237fc14 100644 --- a/src/Navigation/NavigationItem.php +++ b/src/Navigation/NavigationItem.php @@ -13,6 +13,7 @@ class NavigationItem private PageModel $rootPage; private ?PageModel $targetPage = null; private string $linkLabel; + private ?string $ariaLabel = null; private ?bool $newWindow = null; private bool $isDirectFallback = false; private bool $isCurrentPage = false; @@ -66,6 +67,16 @@ public function setLabel(string $label): void $this->linkLabel = $label; } + public function getAriaLabel(): ?string + { + return $this->ariaLabel; + } + + public function setAriaLabel(?string $ariaLabel): void + { + $this->ariaLabel = $ariaLabel; + } + public function getTargetPage(): ?PageModel { return $this->targetPage;