‪TYPO3CMS  ‪main
SlugRedirectChangeItemFactory.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\EventDispatcher\EventDispatcherInterface;
21 use TYPO3\CMS\Backend\Utility\BackendUtility;
25 
31 {
32  public function ‪__construct(
33  private readonly ‪SiteFinder $siteFinder,
34  private readonly EventDispatcherInterface $eventDispatcher,
35  ) {}
36 
37  public function ‪create(int $pageId, ?array $original = null, ?array $changed = null): ?‪SlugRedirectChangeItem
38  {
39  $original ??= BackendUtility::getRecordWSOL('pages', $pageId);
40  if (!$original) {
41  return null;
42  }
43  $languageId = (int)$original['sys_language_uid'];
44  $defaultLanguagePageId = (int)$original['sys_language_uid'] > 0 ? (int)$original['l10n_parent'] : $pageId;
45  try {
46  $site = $this->siteFinder->getSiteByPageId($defaultLanguagePageId);
47  } catch (‪SiteNotFoundException) {
48  // "autoCreateRedirects" and "autoUpdateSlugs" are site configuration settings. Not finding one
49  // means that we should not handle the creation of them, thus no need to create a change item.
50  return null;
51  }
52  $siteLanguage = $site->getLanguageById($languageId);
53  // Verify we should process auto redirect creation or slug updating. If not return early avoiding to create
54  // a change item which is superflous at all.
55  $settings = $site->getSettings();
56  $autoUpdateSlugs = (bool)$settings->get('redirects.autoUpdateSlugs', true);
57  $autoCreateRedirects = (bool)$settings->get('redirects.autoCreateRedirects', true);
58  if (!($autoUpdateSlugs || $autoCreateRedirects)) {
59  return null;
60  }
61  $changeItem = new ‪SlugRedirectChangeItem(
62  defaultLanguagePageId: $defaultLanguagePageId,
63  pageId: $pageId,
64  site: $site,
65  siteLanguage: $siteLanguage,
66  original: $original,
67  sourcesCollection: new ‪RedirectSourceCollection(),
68  changed: $changed
69  );
70  return $this->eventDispatcher->dispatch(new ‪SlugRedirectChangeItemCreatedEvent($changeItem))
71  ->getSlugRedirectChangeItem();
72  }
73 }
‪TYPO3\CMS\Redirects\RedirectUpdate\SlugRedirectChangeItem
Definition: SlugRedirectChangeItem.php:30
‪TYPO3\CMS\Redirects\RedirectUpdate
Definition: PageTypeSource.php:18
‪TYPO3\CMS\Core\Exception\SiteNotFoundException
Definition: SiteNotFoundException.php:25
‪TYPO3\CMS\Core\Site\SiteFinder
Definition: SiteFinder.php:31
‪TYPO3\CMS\Redirects\Event\SlugRedirectChangeItemCreatedEvent
Definition: SlugRedirectChangeItemCreatedEvent.php:32
‪TYPO3\CMS\Redirects\RedirectUpdate\SlugRedirectChangeItemFactory\__construct
‪__construct(private readonly SiteFinder $siteFinder, private readonly EventDispatcherInterface $eventDispatcher,)
Definition: SlugRedirectChangeItemFactory.php:32
‪TYPO3\CMS\Redirects\RedirectUpdate\SlugRedirectChangeItemFactory
Definition: SlugRedirectChangeItemFactory.php:31
‪TYPO3\CMS\Redirects\RedirectUpdate\RedirectSourceCollection
Definition: RedirectSourceCollection.php:24
‪TYPO3\CMS\Redirects\RedirectUpdate\SlugRedirectChangeItemFactory\create
‪create(int $pageId, ?array $original=null, ?array $changed=null)
Definition: SlugRedirectChangeItemFactory.php:37