‪TYPO3CMS  ‪main
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 
31 
38 {
39  public function ‪__construct(
40  protected ‪ContentObjectRenderer $cObj,
41  protected ‪LanguageMenuProcessor $languageMenuProcessor,
42  ) {}
43 
44  #[AsEventListener('typo3-seo/hreflangGenerator')]
45  public function ‪__invoke(‪ModifyHrefLangTagsEvent $event): void
46  {
47  $request = $event->‪getRequest();
48  $pageInformation = $request->getAttribute('frontend.page.information');
49  $pageRecord = $pageInformation->getPageRecord();
50  if ((int)$pageRecord['no_index'] === 1) {
51  return;
52  }
53  $this->cObj->setRequest($event->‪getRequest());
54  ‪$languages = $this->languageMenuProcessor->process($this->cObj, [], [], []);
55  $site = $request->getAttribute('site');
56  $siteLanguage = $request->getAttribute('language', $site->getDefaultLanguage());
57  $pageId = $pageInformation->getId();
58 
59  $hrefLangs = $event->‪getHrefLangs();
60  foreach (‪$languages['languagemenu'] as $language) {
61  if (!empty($language['link']) && $language['hreflang']) {
62  if ($language['languageId'] === 0) {
63  // No need to fetch default language
64  $page = ($pageRecord['_TRANSLATION_SOURCE'] ?? null)?->toArray(true) ?? $pageRecord;
65  } elseif ($language['languageId'] === ($pageRecord['_REQUESTED_OVERLAY_LANGUAGE'] ?? false)) {
66  // No need to fetch current language
67  $page = $pageRecord;
68  } else {
69  $page = $this->‪getTranslatedPageRecord($pageId, $language['languageId'], $site);
70  }
71  // do not set hreflang if a page is not translated explicitly
72  if (empty($page)) {
73  continue;
74  }
75  // do not set hreflang when canonical is set explicitly
76  if (!empty($page['canonical_link'])) {
77  continue;
78  }
79 
80  $href = $this->‪getAbsoluteUrl($language['link'], $siteLanguage);
81  $hrefLangs[$language['hreflang']] = $href;
82  }
83  }
84  if (count($hrefLangs) > 1) {
85  if (array_key_exists(‪$languages['languagemenu'][0]['hreflang'], $hrefLangs)) {
86  $hrefLangs['x-default'] = $hrefLangs[‪$languages['languagemenu'][0]['hreflang']];
87  }
88  }
89  $event->‪setHrefLangs($hrefLangs);
90  }
91 
92  protected function ‪getAbsoluteUrl(string ‪$url, ‪SiteLanguage $siteLanguage): string
93  {
94  $uri = new ‪Uri(‪$url);
95  if (empty($uri->getHost())) {
96  ‪$url = $siteLanguage->‪getBase()->withPath($uri->getPath());
97 
98  if ($uri->getQuery()) {
99  ‪$url = ‪$url->withQuery($uri->getQuery());
100  }
101  }
102  return (string)‪$url;
103  }
104 
105  protected function ‪getTranslatedPageRecord(int $pageId, int $languageId, ‪Site $site): array
106  {
107  $targetSiteLanguage = $site->‪getLanguageById($languageId);
108  $languageAspect = ‪LanguageAspectFactory::createFromSiteLanguage($targetSiteLanguage);
109 
110  $context = clone GeneralUtility::makeInstance(Context::class);
111  $context->setAspect('language', $languageAspect);
112 
113  $pageRepository = GeneralUtility::makeInstance(PageRepository::class, $context);
114  $pageRecord = $pageRepository->getPage($pageId);
115  // Overlay was requested but did not apply
116  if ($languageId > 0 && !isset($pageRecord['_LOCALIZED_UID'])) {
117  return [];
118  }
119  return $pageRecord;
120  }
121 }
‪TYPO3\CMS\Core\Context\LanguageAspectFactory
Definition: LanguageAspectFactory.php:27
‪TYPO3\CMS\Frontend\Event\ModifyHrefLangTagsEvent\getRequest
‪getRequest()
Definition: ModifyHrefLangTagsEvent.php:37
‪TYPO3\CMS\Core\Attribute\AsEventListener
Definition: AsEventListener.php:25
‪$languages
‪$languages
Definition: updateIsoDatabase.php:104
‪TYPO3\CMS\Frontend\Event\ModifyHrefLangTagsEvent\getHrefLangs
‪getHrefLangs()
Definition: ModifyHrefLangTagsEvent.php:32
‪TYPO3\CMS\Core\Context\LanguageAspectFactory\createFromSiteLanguage
‪static createFromSiteLanguage(SiteLanguage $language)
Definition: LanguageAspectFactory.php:31
‪TYPO3\CMS\Core\Context\Context
Definition: Context.php:54
‪TYPO3\CMS\Frontend\DataProcessing\LanguageMenuProcessor
Definition: LanguageMenuProcessor.php:47
‪TYPO3\CMS\Core\Http\Uri
Definition: Uri.php:30
‪TYPO3\CMS\Core\Site\Entity\Site
Definition: Site.php:42
‪TYPO3\CMS\Seo\HrefLang\HrefLangGenerator\getAbsoluteUrl
‪getAbsoluteUrl(string $url, SiteLanguage $siteLanguage)
Definition: HrefLangGenerator.php:92
‪TYPO3\CMS\Core\Site\Entity\SiteLanguage
Definition: SiteLanguage.php:27
‪TYPO3\CMS\Seo\HrefLang\HrefLangGenerator
Definition: HrefLangGenerator.php:38
‪TYPO3\CMS\Core\Site\Entity\SiteLanguage\getBase
‪getBase()
Definition: SiteLanguage.php:179
‪TYPO3\CMS\Seo\HrefLang
Definition: HrefLangGenerator.php:18
‪TYPO3\CMS\Seo\HrefLang\HrefLangGenerator\getTranslatedPageRecord
‪getTranslatedPageRecord(int $pageId, int $languageId, Site $site)
Definition: HrefLangGenerator.php:105
‪TYPO3\CMS\Seo\HrefLang\HrefLangGenerator\__construct
‪__construct(protected ContentObjectRenderer $cObj, protected LanguageMenuProcessor $languageMenuProcessor,)
Definition: HrefLangGenerator.php:39
‪TYPO3\CMS\Frontend\Event\ModifyHrefLangTagsEvent
Definition: ModifyHrefLangTagsEvent.php:27
‪TYPO3\CMS\Webhooks\Message\$url
‪identifier readonly UriInterface $url
Definition: LoginErrorOccurredMessage.php:36
‪TYPO3\CMS\Core\Site\Entity\Site\getLanguageById
‪getLanguageById(int $languageId)
Definition: Site.php:247
‪TYPO3\CMS\Frontend\Event\ModifyHrefLangTagsEvent\setHrefLangs
‪setHrefLangs(array $hrefLangs)
Definition: ModifyHrefLangTagsEvent.php:54
‪TYPO3\CMS\Seo\HrefLang\HrefLangGenerator\__invoke
‪__invoke(ModifyHrefLangTagsEvent $event)
Definition: HrefLangGenerator.php:45
‪TYPO3\CMS\Frontend\ContentObject\ContentObjectRenderer
Definition: ContentObjectRenderer.php:102
‪TYPO3\CMS\Core\Domain\Repository\PageRepository
Definition: PageRepository.php:69
‪TYPO3\CMS\Core\Utility\GeneralUtility
Definition: GeneralUtility.php:52