[TASK] Make hreflang links absolute 62/58262/5
authorRichard Haeser <richard@maxserv.com>
Wed, 12 Sep 2018 20:51:46 +0000 (22:51 +0200)
committerJigal van Hemert <jigal.van.hemert@typo3.org>
Thu, 13 Sep 2018 22:34:35 +0000 (00:34 +0200)
Create absolute URL's for the hreflang links by using the configuration
of the current site.

Resolves: #86224
Releases: master
Change-Id: Id3196542875862b20145418956fcfdfcc3834241
Reviewed-on: https://review.typo3.org/58262
Reviewed-by: Kevin Appelt <kevin.appelt@icloud.com>
Tested-by: Kevin Appelt <kevin.appelt@icloud.com>
Tested-by: TYPO3com <no-reply@typo3.com>
Reviewed-by: Jörg Bösche <typo3@joergboesche.de>
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 4ff3c62..1339ec9 100644 (file)
@@ -17,12 +17,13 @@ namespace TYPO3\CMS\Seo\HrefLang;
  */
 
 use TYPO3\CMS\Core\Site\Entity\Site;
+use TYPO3\CMS\Core\Site\Entity\SiteLanguage;
 use TYPO3\CMS\Core\Utility\GeneralUtility;
 use TYPO3\CMS\Frontend\ContentObject\ContentObjectRenderer;
 use TYPO3\CMS\Frontend\DataProcessing\LanguageMenuProcessor;
 
 /**
- * Class to add the metatags for the SEO fields in core
+ * Class to add the hreflang tags to the page
  *
  * @internal
  */
@@ -54,15 +55,36 @@ class HreflangGenerator
         if ($GLOBALS['TYPO3_REQUEST']->getAttribute('site') instanceof Site) {
             $languageMenu = GeneralUtility::makeInstance(LanguageMenuProcessor::class);
             $languages = $languageMenu->process($this->cObj, [], [], []);
-            $hreflangs = '' . LF;
+            $hreflangs = '';
             foreach ($languages['languagemenu'] as $language) {
-                if ($language['available'] == 1) {
-                    $hreflangs .= '<link rel="alternate" hreflang="' . $language['hreflang'] . '" href="' . $language['link'] . '"/>' . LF;
+                if ($language['available'] === 1) {
+                    $href = $this->getAbsoluteUrl($language['link']);
+                    $hreflangs .= '<link rel="alternate" hreflang="' . $language['hreflang'] . '" href="' . $href . '"/>' . LF;
                 }
             }
-            $hreflangs .= '<link rel="alternate" hreflang="x-default" href="' . $languages['languagemenu'][0]['link'] . '"/>' . LF;
+
+            $href = $this->getAbsoluteUrl($languages['languagemenu'][0]['link']);
+            $hreflangs .= '<link rel="alternate" hreflang="x-default" href="' . $href . '"/>' . LF;
+
             $GLOBALS['TSFE']->additionalHeaderData[] = $hreflangs;
         }
+
         return $hreflangs;
     }
+
+    /**
+     * @param string $url
+     * @return string
+     */
+    protected function getAbsoluteUrl(string $url): string
+    {
+        /** @var SiteLanguage $siteLanguage */
+        $siteLanguage = $GLOBALS['TYPO3_REQUEST']->getAttribute('language');
+
+        if (strpos($url, 'http') !== 0) {
+            $url = (string)$siteLanguage->getBase()->withPath($url);
+        }
+
+        return $url;
+    }
 }