‪TYPO3CMS  9.5
HrefLangGenerator.php
Go to the documentation of this file.
1 <?php
2 declare(strict_types = 1);
3 
5 
6 /*
7  * This file is part of the TYPO3 CMS project.
8  *
9  * It is free software; you can redistribute it and/or modify it under
10  * the terms of the GNU General Public License, either version 2
11  * of the License, or any later version.
12  *
13  * For the full copyright and license information, please read the
14  * LICENSE.txt file that was distributed with this source code.
15  *
16  * The TYPO3 project - inspiring people to share!
17  */
18 
19 use Psr\Http\Message\ServerRequestInterface;
27 
34 {
40  public ‪$cObj;
41 
46 
50  protected ‪$request;
51 
59  {
60  if (‪$cObj === null) {
61  ‪$cObj = GeneralUtility::makeInstance(ContentObjectRenderer::class);
62  }
63  if (‪$typoScriptFrontendController === null) {
65  }
66  if (‪$request === null) {
67  ‪$request = $this->‪getRequest();
68  }
69 
70  $this->cObj = ‪$cObj;
71  $this->typoScriptFrontendController = ‪$typoScriptFrontendController;
72  $this->request = ‪$request;
73  }
74 
75  public function ‪generate(): string
76  {
77  $hreflangs = [];
78  if ((int)$this->typoScriptFrontendController->page['no_index'] === 1) {
79  return '';
80  }
81 
82  if ($this->request->getAttribute('site') instanceof Site) {
83  $languageMenu = GeneralUtility::makeInstance(LanguageMenuProcessor::class);
84  $languages = $languageMenu->process($this->cObj, [], [], []);
85  foreach ($languages['languagemenu'] as $language) {
86  if ($language['available'] === 1 && !empty($language['link'])) {
87  $href = $this->‪getAbsoluteUrl($language['link']);
88  $hreflangs[] =
89  '<link rel="alternate" hreflang="' . htmlspecialchars($language['hreflang']) . '" href="' . htmlspecialchars($href) . '"/>';
90  }
91  }
92 
93  if (count($hreflangs) > 1) {
94  $href = $this->‪getAbsoluteUrl($languages['languagemenu'][0]['link']);
95  $hreflangs[] =
96  '<link rel="alternate" hreflang="x-default" href="' . htmlspecialchars($href) . '"/>' . LF;
97 
98  $this->‪getTypoScriptFrontendController()->additionalHeaderData[] = implode(LF, $hreflangs);
99  }
100  }
101 
102  return implode(LF, $hreflangs);
103  }
104 
109  protected function ‪getAbsoluteUrl(string $url): string
110  {
111  $uri = new Uri($url);
112  if (empty($uri->getHost())) {
113  $url = $this->‪getSiteLanguage()->‪getBase()->withPath($uri->getPath());
114 
115  if ($uri->getQuery()) {
116  $url = $url->withQuery($uri->getQuery());
117  }
118  }
119 
120  return (string)$url;
121  }
122 
126  protected function ‪getSiteLanguage(): SiteLanguage
127  {
128  return $this->request->getAttribute('language');
129  }
130 
134  protected function ‪getTypoScriptFrontendController(): TypoScriptFrontendController
135  {
136  return ‪$GLOBALS['TSFE'];
137  }
138 
142  protected function ‪getRequest(): ServerRequestInterface
143  {
144  return ‪$GLOBALS['TYPO3_REQUEST'];
145  }
146 }
‪TYPO3\CMS\Seo\HrefLang\HrefLangGenerator\$request
‪ServerRequestInterface $request
Definition: HrefLangGenerator.php:47
‪TYPO3\CMS\Seo\HrefLang\HrefLangGenerator\$cObj
‪ContentObjectRenderer $cObj
Definition: HrefLangGenerator.php:39
‪TYPO3\CMS\Seo\HrefLang\HrefLangGenerator\getSiteLanguage
‪SiteLanguage getSiteLanguage()
Definition: HrefLangGenerator.php:123
‪TYPO3\CMS\Frontend\DataProcessing\LanguageMenuProcessor
Definition: LanguageMenuProcessor.php:45
‪TYPO3\CMS\Core\Http\Uri
Definition: Uri.php:27
‪TYPO3\CMS\Core\Site\Entity\Site
Definition: Site.php:39
‪TYPO3\CMS\Core\Site\Entity\SiteLanguage
Definition: SiteLanguage.php:25
‪TYPO3\CMS\Seo\HrefLang\HrefLangGenerator\getTypoScriptFrontendController
‪TypoScriptFrontendController getTypoScriptFrontendController()
Definition: HrefLangGenerator.php:131
‪TYPO3\CMS\Seo\HrefLang\HrefLangGenerator
Definition: HrefLangGenerator.php:34
‪TYPO3\CMS\Seo\HrefLang
Definition: HrefLangGenerator.php:4
‪TYPO3\CMS\Seo\HrefLang\HrefLangGenerator\generate
‪generate()
Definition: HrefLangGenerator.php:72
‪TYPO3\CMS\Frontend\Controller\TypoScriptFrontendController
Definition: TypoScriptFrontendController.php:97
‪TYPO3\CMS\Core\Site\Entity\SiteLanguage\getBase
‪UriInterface getBase()
Definition: SiteLanguage.php:204
‪$GLOBALS
‪$GLOBALS['TYPO3_CONF_VARS']['EXTCONF']['adminpanel']['modules']
Definition: ext_localconf.php:5
‪TYPO3\CMS\Seo\HrefLang\HrefLangGenerator\$typoScriptFrontendController
‪TypoScriptFrontendController $typoScriptFrontendController
Definition: HrefLangGenerator.php:43
‪TYPO3\CMS\Frontend\ContentObject\ContentObjectRenderer
Definition: ContentObjectRenderer.php:91
‪TYPO3\CMS\Seo\HrefLang\HrefLangGenerator\getRequest
‪ServerRequestInterface getRequest()
Definition: HrefLangGenerator.php:139
‪TYPO3\CMS\Core\Utility\GeneralUtility
Definition: GeneralUtility.php:45
‪TYPO3\CMS\Seo\HrefLang\HrefLangGenerator\__construct
‪__construct(ContentObjectRenderer $cObj=null, TypoScriptFrontendController $typoScriptFrontendController=null, ServerRequestInterface $request=null)
Definition: HrefLangGenerator.php:55
‪TYPO3\CMS\Seo\HrefLang\HrefLangGenerator\getAbsoluteUrl
‪string getAbsoluteUrl(string $url)
Definition: HrefLangGenerator.php:106