‪TYPO3CMS  10.4
HrefLangGenerator.php
Go to the documentation of this file.
1 <?php
2 
3 declare(strict_types=1);
4 
5 /*
6  * This file is part of the TYPO3 CMS project.
7  *
8  * It is free software; you can redistribute it and/or modify it under
9  * the terms of the GNU General Public License, either version 2
10  * of the License, or any later version.
11  *
12  * For the full copyright and license information, please read the
13  * LICENSE.txt file that was distributed with this source code.
14  *
15  * The TYPO3 project - inspiring people to share!
16  */
17 
19 
20 use Psr\Http\Message\ServerRequestInterface;
32 
39 {
45  public ‪$cObj;
46 
51 
53  {
54  $this->cObj = ‪$cObj;
55  $this->languageMenuProcessor = ‪$languageMenuProcessor;
56  }
57 
58  public function ‪__invoke(‪ModifyHrefLangTagsEvent $event): void
59  {
60  $hrefLangs = $event->‪getHrefLangs();
61  if ((int)$this->‪getTypoScriptFrontendController()->page['no_index'] === 1) {
62  return;
63  }
64 
65  $languages = $this->languageMenuProcessor->process($this->cObj, [], [], []);
67  $siteLanguage = $event->‪getRequest()->getAttribute('language');
68  $pageId = (int)$this->‪getTypoScriptFrontendController()->id;
69 
70  foreach ($languages['languagemenu'] as $language) {
71  if (!empty($language['link']) && $language['hreflang']) {
72  $page = $this->‪getTranslatedPageRecord($pageId, $language['languageId'], $event->‪getRequest());
73  // do not set hreflang if a page is not translated explicitly
74  if (empty($page)) {
75  continue;
76  }
77  // do not set hreflang when canonical is set explicitly
78  if (!empty($page['canonical_link'])) {
79  continue;
80  }
81 
82  $href = $this->‪getAbsoluteUrl($language['link'], $siteLanguage);
83  $hrefLangs[$language['hreflang']] = $href;
84  }
85  }
86 
87  if (count($hrefLangs) > 1) {
88  if (array_key_exists($languages['languagemenu'][0]['hreflang'], $hrefLangs)) {
89  $hrefLangs['x-default'] = $hrefLangs[$languages['languagemenu'][0]['hreflang']];
90  }
91  }
92 
93  $event->‪setHrefLangs($hrefLangs);
94  }
95 
101  protected function ‪getAbsoluteUrl(string $url, SiteLanguage $siteLanguage): string
102  {
103  $uri = new Uri($url);
104  if (empty($uri->getHost())) {
105  $url = $siteLanguage->getBase()->withPath($uri->getPath());
106 
107  if ($uri->getQuery()) {
108  $url = $url->withQuery($uri->getQuery());
109  }
110  }
111 
112  return (string)$url;
113  }
114 
118  protected function ‪getTypoScriptFrontendController(): TypoScriptFrontendController
119  {
120  return ‪$GLOBALS['TSFE'];
121  }
122 
123  protected function ‪getTranslatedPageRecord(int $pageId, int $languageId, ServerRequestInterface $request): array
124  {
125  $site = $request->getAttribute('site');
126  if (!$site instanceof ‪SiteInterface) {
127  return $this->‪getTypoScriptFrontendController()->page;
128  }
129 
130  $targetSiteLanguage = $site->getLanguageById($languageId);
131  $languageAspect = ‪LanguageAspectFactory::createFromSiteLanguage($targetSiteLanguage);
132 
133  $context = clone GeneralUtility::makeInstance(Context::class);
134  $context->setAspect('language', $languageAspect);
135 
136  $pageRepository = GeneralUtility::makeInstance(PageRepository::class, $context);
137  if ($languageId > 0) {
138  return $pageRepository->getPageOverlay($pageId, $languageId);
139  }
140  return $pageRepository->getPage($pageId);
141  }
142 }
‪TYPO3\CMS\Core\Site\Entity\SiteInterface
Definition: SiteInterface.php:26
‪TYPO3\CMS\Core\Context\LanguageAspectFactory
Definition: LanguageAspectFactory.php:27
‪TYPO3\CMS\Frontend\Event\ModifyHrefLangTagsEvent\getRequest
‪getRequest()
Definition: ModifyHrefLangTagsEvent.php:46
‪TYPO3\CMS\Frontend\Event\ModifyHrefLangTagsEvent\getHrefLangs
‪getHrefLangs()
Definition: ModifyHrefLangTagsEvent.php:41
‪TYPO3\CMS\Seo\HrefLang\HrefLangGenerator\$cObj
‪ContentObjectRenderer $cObj
Definition: HrefLangGenerator.php:44
‪TYPO3\CMS\Core\Context\LanguageAspectFactory\createFromSiteLanguage
‪static LanguageAspect createFromSiteLanguage(SiteLanguage $language)
Definition: LanguageAspectFactory.php:34
‪TYPO3\CMS\Core\Context\Context
Definition: Context.php:53
‪TYPO3\CMS\Frontend\DataProcessing\LanguageMenuProcessor
Definition: LanguageMenuProcessor.php:48
‪TYPO3\CMS\Core\Http\Uri
Definition: Uri.php:29
‪TYPO3\CMS\Core\Site\Entity\SiteLanguage
Definition: SiteLanguage.php:26
‪TYPO3\CMS\Seo\HrefLang\HrefLangGenerator\getTypoScriptFrontendController
‪TypoScriptFrontendController getTypoScriptFrontendController()
Definition: HrefLangGenerator.php:116
‪TYPO3\CMS\Seo\HrefLang\HrefLangGenerator
Definition: HrefLangGenerator.php:39
‪TYPO3\CMS\Seo\HrefLang
Definition: HrefLangGenerator.php:18
‪TYPO3\CMS\Seo\HrefLang\HrefLangGenerator\getTranslatedPageRecord
‪getTranslatedPageRecord(int $pageId, int $languageId, ServerRequestInterface $request)
Definition: HrefLangGenerator.php:121
‪TYPO3\CMS\Frontend\Event\ModifyHrefLangTagsEvent
Definition: ModifyHrefLangTagsEvent.php:27
‪TYPO3\CMS\Seo\HrefLang\HrefLangGenerator\__construct
‪__construct(ContentObjectRenderer $cObj, LanguageMenuProcessor $languageMenuProcessor)
Definition: HrefLangGenerator.php:50
‪TYPO3\CMS\Frontend\Controller\TypoScriptFrontendController
Definition: TypoScriptFrontendController.php:98
‪TYPO3\CMS\Core\Site\Entity\SiteLanguage\getBase
‪UriInterface getBase()
Definition: SiteLanguage.php:214
‪$GLOBALS
‪$GLOBALS['TYPO3_CONF_VARS']['EXTCONF']['adminpanel']['modules']
Definition: ext_localconf.php:5
‪TYPO3\CMS\Frontend\Event\ModifyHrefLangTagsEvent\setHrefLangs
‪setHrefLangs(array $hrefLangs)
Definition: ModifyHrefLangTagsEvent.php:59
‪TYPO3\CMS\Seo\HrefLang\HrefLangGenerator\__invoke
‪__invoke(ModifyHrefLangTagsEvent $event)
Definition: HrefLangGenerator.php:56
‪TYPO3\CMS\Frontend\ContentObject\ContentObjectRenderer
Definition: ContentObjectRenderer.php:97
‪TYPO3\CMS\Core\Domain\Repository\PageRepository
Definition: PageRepository.php:52
‪TYPO3\CMS\Seo\HrefLang\HrefLangGenerator\getAbsoluteUrl
‪string getAbsoluteUrl(string $url, SiteLanguage $siteLanguage)
Definition: HrefLangGenerator.php:99
‪TYPO3\CMS\Core\Utility\GeneralUtility
Definition: GeneralUtility.php:46
‪TYPO3\CMS\Seo\HrefLang\HrefLangGenerator\$languageMenuProcessor
‪LanguageMenuProcessor $languageMenuProcessor
Definition: HrefLangGenerator.php:48