[TASK] Only render hreflang tag when multiple languages are available 37/58837/2
authorRichard Haeser <richard@maxserv.com>
Sat, 3 Nov 2018 14:21:13 +0000 (15:21 +0100)
committerJigal van Hemert <jigal.van.hemert@typo3.org>
Thu, 8 Nov 2018 21:15:15 +0000 (22:15 +0100)
Make sure the hreflang tags are only rendered when there is more than 1
language enabled for the current page.

Resolves: #86545
Releases: master
Change-Id: Ib2bbccbc25544a4d162e7cb6da0ffc50a31df2ba
Reviewed-on: https://review.typo3.org/58837
Tested-by: TYPO3com <no-reply@typo3.com>
Reviewed-by: Riny van Tiggelen <info@online-gamer.nl>
Tested-by: Riny van Tiggelen <info@online-gamer.nl>
Reviewed-by: Jörg Bösche <typo3@joergboesche.de>
Reviewed-by: Josef Glatz <josef.glatz@typo3.org>
Tested-by: Josef Glatz <josef.glatz@typo3.org>
Reviewed-by: Jigal van Hemert <jigal.van.hemert@typo3.org>
Tested-by: Jigal van Hemert <jigal.van.hemert@typo3.org>
typo3/sysext/seo/Classes/HrefLang/HrefLangGenerator.php

index a72c720..5161a7c 100644 (file)
@@ -64,7 +64,7 @@ class HrefLangGenerator
 
     public function generate(): string
     {
-        $hreflangs = '';
+        $hreflangs = [];
         if ((int)$this->typoScriptFrontendController->page['no_index'] === 1) {
             return '';
         }
@@ -72,21 +72,24 @@ class HrefLangGenerator
         if ($GLOBALS['TYPO3_REQUEST']->getAttribute('site') instanceof Site) {
             $languageMenu = GeneralUtility::makeInstance(LanguageMenuProcessor::class);
             $languages = $languageMenu->process($this->cObj, [], [], []);
-            $hreflangs = '';
             foreach ($languages['languagemenu'] as $language) {
                 if ($language['available'] === 1) {
                     $href = $this->getAbsoluteUrl($language['link']);
-                    $hreflangs .= '<link rel="alternate" hreflang="' . htmlspecialchars($language['hreflang']) . '" href="' . htmlspecialchars($href) . '"/>' . LF;
+                    $hreflangs[] =
+                        '<link rel="alternate" hreflang="' . htmlspecialchars($language['hreflang']) . '" href="' . htmlspecialchars($href) . '"/>';
                 }
             }
 
-            $href = $this->getAbsoluteUrl($languages['languagemenu'][0]['link']);
-            $hreflangs .= '<link rel="alternate" hreflang="x-default" href="' . htmlspecialchars($href) . '"/>' . LF;
+            if (count($hreflangs) > 1) {
+                $href = $this->getAbsoluteUrl($languages['languagemenu'][0]['link']);
+                $hreflangs[] =
+                    '<link rel="alternate" hreflang="x-default" href="' . htmlspecialchars($href) . '"/>' . LF;
 
-            $GLOBALS['TSFE']->additionalHeaderData[] = $hreflangs;
+                $GLOBALS['TSFE']->additionalHeaderData[] = implode(LF, $hreflangs);
+            }
         }
 
-        return $hreflangs;
+        return implode(LF, $hreflangs);
     }
 
     /**