‪TYPO3CMS  ‪main
AddPageTypeZeroSource.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 
34 
43 {
47  private array ‪$ignoredDokTypes = [
50  ];
51 
52  #[AsEventListener(identifier: 'redirects-add-page-type-zero-source', after: 'redirects-add-plain-slug-replacement-source')]
53  public function ‪__invoke(‪SlugRedirectChangeItemCreatedEvent $event): void
54  {
55  // Create full resolved uri for page type zero.
56  $changeItem = $event->‪getSlugRedirectChangeItem();
57  // Do not create a redirect source for ignored doktypes
58  if (in_array((int)($changeItem->getOriginal()['doktype'] ?? 0), $this->ignoredDokTypes, true)) {
59  return;
60  }
61 
62  try {
63  $pageTypeZeroSource = $this->‪createPageTypeZeroSource(
64  $changeItem->getPageId(),
65  $changeItem->getSite(),
66  $changeItem->getSiteLanguage(),
67  );
69  // Could not properly link to page. Nothing left to do, so return directly.
70  return;
71  }
72  $sources = $changeItem->getSourcesCollection()->all();
73  // If page type zero source results in the same uri, plain slug replacement source is removed. This avoids
74  // the creation of duplicated redirects. PageTypeSource is taken as stronger match and therefor used.
75  $sources = array_filter($sources, fn($source) => !$this->‪sourceEqualsPageTypeZeroSource($source, $pageTypeZeroSource));
76  $sources[] = $pageTypeZeroSource;
77  $changeItem = $changeItem->withSourcesCollection(new ‪RedirectSourceCollection(...array_values($sources)));
78  $event->‪setSlugRedirectChangeItem($changeItem);
79  }
80 
81  private function ‪sourceEqualsPageTypeZeroSource(‪RedirectSourceInterface $source, ‪PageTypeSource $pageTypeZeroSource): bool
82  {
83  return $source instanceof ‪PlainSlugReplacementRedirectSource
84  && $source->‪getHost() === $pageTypeZeroSource->‪getHost()
85  && $source->‪getPath() === $pageTypeZeroSource->‪getPath();
86  }
87 
88  private function ‪createPageTypeZeroSource(int $pageUid, ‪Site $site, ‪SiteLanguage $siteLanguage): ‪PageTypeSource
89  {
90  try {
91  $context = GeneralUtility::makeInstance(Context::class);
92  $uri = $site->‪getRouter($context)->generateUri(
93  $pageUid,
94  [
95  '_language' => $siteLanguage,
96  'type' => 0,
97  ],
98  '',
100  );
101  return new ‪PageTypeSource(
102  $uri->getHost() ?: '*',
103  $uri->getPath(),
104  0,
105  [],
106  );
107  } catch (\InvalidArgumentException | ‪InvalidRouteArgumentsException $e) {
109  sprintf(
110  'The link to the page with ID "%d" and type "%d" could not be generated: %s',
111  $pageUid,
112  0,
113  $e->getMessage()
114  ),
115  1671639962,
116  $e
117  );
118  }
119  }
120 }
‪TYPO3\CMS\Redirects\EventListener\AddPageTypeZeroSource\$ignoredDokTypes
‪array $ignoredDokTypes
Definition: AddPageTypeZeroSource.php:47
‪TYPO3\CMS\Core\Routing\RouterInterface
Definition: RouterInterface.php:28
‪TYPO3\CMS\Core\Attribute\AsEventListener
Definition: AsEventListener.php:25
‪TYPO3\CMS\Redirects\EventListener\AddPageTypeZeroSource
Definition: AddPageTypeZeroSource.php:43
‪TYPO3\CMS\Redirects\Event\SlugRedirectChangeItemCreatedEvent\setSlugRedirectChangeItem
‪setSlugRedirectChangeItem(SlugRedirectChangeItem $slugRedirectChangeItem)
Definition: SlugRedirectChangeItemCreatedEvent.php:42
‪TYPO3\CMS\Redirects\EventListener
Definition: AddPageTypeZeroSource.php:18
‪TYPO3\CMS\Core\Context\Context
Definition: Context.php:54
‪TYPO3\CMS\Redirects\RedirectUpdate\PlainSlugReplacementRedirectSource
Definition: PlainSlugReplacementRedirectSource.php:25
‪TYPO3\CMS\Redirects\Event\SlugRedirectChangeItemCreatedEvent
Definition: SlugRedirectChangeItemCreatedEvent.php:32
‪TYPO3\CMS\Core\Site\Entity\Site
Definition: Site.php:42
‪TYPO3\CMS\Core\Site\Entity\SiteLanguage
Definition: SiteLanguage.php:27
‪TYPO3\CMS\Redirects\RedirectUpdate\RedirectSourceInterface\getPath
‪getPath()
‪TYPO3\CMS\Redirects\EventListener\AddPageTypeZeroSource\__invoke
‪__invoke(SlugRedirectChangeItemCreatedEvent $event)
Definition: AddPageTypeZeroSource.php:53
‪TYPO3\CMS\Redirects\RedirectUpdate\PageTypeSource\getHost
‪getHost()
Definition: PageTypeSource.php:29
‪TYPO3\CMS\Core\Domain\Repository\PageRepository\DOKTYPE_SPACER
‪const DOKTYPE_SPACER
Definition: PageRepository.php:103
‪TYPO3\CMS\Core\Routing\InvalidRouteArgumentsException
Definition: InvalidRouteArgumentsException.php:25
‪TYPO3\CMS\Core\Domain\Repository\PageRepository\DOKTYPE_SYSFOLDER
‪const DOKTYPE_SYSFOLDER
Definition: PageRepository.php:104
‪TYPO3\CMS\Redirects\RedirectUpdate\RedirectSourceCollection
Definition: RedirectSourceCollection.php:24
‪TYPO3\CMS\Redirects\RedirectUpdate\RedirectSourceInterface
Definition: RedirectSourceInterface.php:25
‪TYPO3\CMS\Redirects\RedirectUpdate\PageTypeSource
Definition: PageTypeSource.php:21
‪TYPO3\CMS\Redirects\EventListener\AddPageTypeZeroSource\createPageTypeZeroSource
‪createPageTypeZeroSource(int $pageUid, Site $site, SiteLanguage $siteLanguage)
Definition: AddPageTypeZeroSource.php:88
‪TYPO3\CMS\Redirects\Event\SlugRedirectChangeItemCreatedEvent\getSlugRedirectChangeItem
‪getSlugRedirectChangeItem()
Definition: SlugRedirectChangeItemCreatedEvent.php:37
‪TYPO3\CMS\Core\Domain\Repository\PageRepository
Definition: PageRepository.php:69
‪TYPO3\CMS\Redirects\EventListener\AddPageTypeZeroSource\sourceEqualsPageTypeZeroSource
‪sourceEqualsPageTypeZeroSource(RedirectSourceInterface $source, PageTypeSource $pageTypeZeroSource)
Definition: AddPageTypeZeroSource.php:81
‪TYPO3\CMS\Core\Routing\RouterInterface\ABSOLUTE_URL
‪const ABSOLUTE_URL
Definition: RouterInterface.php:32
‪TYPO3\CMS\Core\Utility\GeneralUtility
Definition: GeneralUtility.php:52
‪TYPO3\CMS\Redirects\RedirectUpdate\RedirectSourceInterface\getHost
‪getHost()
‪TYPO3\CMS\Redirects\RedirectUpdate\PageTypeSource\getPath
‪getPath()
Definition: PageTypeSource.php:34
‪TYPO3\CMS\Core\Site\Entity\Site\getRouter
‪getRouter(Context $context=null)
Definition: Site.php:346