[FEATURE] Add hreflang support 05/58105/12
authorKevin Appelt <kevin.appelt@icloud.com>
Fri, 31 Aug 2018 15:56:43 +0000 (17:56 +0200)
committerSusanne Moog <susanne.moog@typo3.org>
Sat, 1 Sep 2018 22:03:08 +0000 (00:03 +0200)
This feature adds an hreflang tag for multilanguage-websites (one-tree)
automatically, based on the new LanguageMenuProcessor.

The href is relative as long as the domain is the same.
If the domain differs the href become absolute.
x-default href is the first supported language.

Resolves: #75806
Releases: master
Change-Id: I06754f61d7a03f6894001be11c9ce2a37ac2106f
Reviewed-on: https://review.typo3.org/58105
Reviewed-by: Richard Haeser <richard@maxserv.com>
Tested-by: Richard Haeser <richard@maxserv.com>
Reviewed-by: Björn Jacob <bjoern.jacob@tritum.de>
Tested-by: Björn Jacob <bjoern.jacob@tritum.de>
Reviewed-by: Anja Leichsenring <aleichsenring@ab-softlab.de>
Tested-by: Anja Leichsenring <aleichsenring@ab-softlab.de>
Reviewed-by: Susanne Moog <susanne.moog@typo3.org>
Tested-by: Susanne Moog <susanne.moog@typo3.org>
Tested-by: TYPO3com <no-reply@typo3.com>
typo3/sysext/core/Documentation/Changelog/master/Feature-75806-AddHreflangSupport.rst [new file with mode: 0644]
typo3/sysext/seo/Classes/HrefLang/HreflangGenerator.php [new file with mode: 0644]
typo3/sysext/seo/ext_localconf.php

diff --git a/typo3/sysext/core/Documentation/Changelog/master/Feature-75806-AddHreflangSupport.rst b/typo3/sysext/core/Documentation/Changelog/master/Feature-75806-AddHreflangSupport.rst
new file mode 100644 (file)
index 0000000..c7f6e8a
--- /dev/null
@@ -0,0 +1,18 @@
+.. include:: ../../Includes.txt
+
+======================================
+Feature: #75806 - Add hreflang support
+======================================
+
+See :issue:`75806`
+
+Description
+===========
+
+"hreflang" tags are now added automatically for multilanguage websites based on the one-tree principle.
+
+The href is relative as long as the domain is the same. If the domain differs the href becomes absolute.
+The x-default href is the first supported language.
+The value of "hreflang" is the one set in the new sites module.
+
+.. index:: Frontend, ext:seo
diff --git a/typo3/sysext/seo/Classes/HrefLang/HreflangGenerator.php b/typo3/sysext/seo/Classes/HrefLang/HreflangGenerator.php
new file mode 100644 (file)
index 0000000..4ff3c62
--- /dev/null
@@ -0,0 +1,68 @@
+<?php
+declare(strict_types = 1);
+
+namespace TYPO3\CMS\Seo\HrefLang;
+
+/*
+ * This file is part of the TYPO3 CMS project.
+ *
+ * It is free software; you can redistribute it and/or modify it under
+ * the terms of the GNU General Public License, either version 2
+ * of the License, or any later version.
+ *
+ * For the full copyright and license information, please read the
+ * LICENSE.txt file that was distributed with this source code.
+ *
+ * The TYPO3 project - inspiring people to share!
+ */
+
+use TYPO3\CMS\Core\Site\Entity\Site;
+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
+ *
+ * @internal
+ */
+class HreflangGenerator
+{
+    /**
+     * The content object renderer
+     *
+     * @var ContentObjectRenderer
+     */
+    public $cObj;
+
+    /**
+     * HreflangGenerator constructor
+     *
+     * @param ContentObjectRenderer $cObj
+     */
+    public function __construct(ContentObjectRenderer $cObj = null)
+    {
+        if ($cObj === null) {
+            $cObj = GeneralUtility::makeInstance(ContentObjectRenderer::class);
+        }
+        $this->cObj = $cObj;
+    }
+
+    public function generate(): string
+    {
+        $hreflangs = '';
+        if ($GLOBALS['TYPO3_REQUEST']->getAttribute('site') instanceof Site) {
+            $languageMenu = GeneralUtility::makeInstance(LanguageMenuProcessor::class);
+            $languages = $languageMenu->process($this->cObj, [], [], []);
+            $hreflangs = '' . LF;
+            foreach ($languages['languagemenu'] as $language) {
+                if ($language['available'] == 1) {
+                    $hreflangs .= '<link rel="alternate" hreflang="' . $language['hreflang'] . '" href="' . $language['link'] . '"/>' . LF;
+                }
+            }
+            $hreflangs .= '<link rel="alternate" hreflang="x-default" href="' . $languages['languagemenu'][0]['link'] . '"/>' . LF;
+            $GLOBALS['TSFE']->additionalHeaderData[] = $hreflangs;
+        }
+        return $hreflangs;
+    }
+}
index 3e0c9ec..e92f0c7 100644 (file)
@@ -3,6 +3,8 @@ defined('TYPO3_MODE') or die();
 
 $GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['TYPO3\CMS\Frontend\Page\PageGenerator']['generateMetaTags'][] =
     \TYPO3\CMS\Seo\MetaTag\MetaTagGenerator::class . '->generate';
+$GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['TYPO3\CMS\Frontend\Page\PageGenerator']['generateMetaTags'][] =
+    \TYPO3\CMS\Seo\HrefLang\HreflangGenerator::class . '->generate';
 
 $metaTagManagerRegistry = \TYPO3\CMS\Core\Utility\GeneralUtility::makeInstance(\TYPO3\CMS\Core\MetaTag\MetaTagManagerRegistry::class);
 $metaTagManagerRegistry->registerManager(