‪TYPO3CMS  ‪main
DataHandlerSlugUpdateHook.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 
25 
30 {
37  protected $persistedChangedItems;
38 
39  public function __construct(
40  protected ‪SlugService $slugService,
41  protected ‪SlugRedirectChangeItemFactory $slugRedirectChangeItemFactory,
42  ) {}
43 
49  public function processDatamap_preProcessFieldArray(array $incomingFieldArray, string $table, $id, ‪DataHandler $dataHandler): void
50  {
51  if (
52  $table !== 'pages'
53  || empty($incomingFieldArray['slug'])
54  || $this->‪isNestedHookInvocation($dataHandler)
56  || !$dataHandler->‪checkRecordUpdateAccess($table, $id)
57  ) {
58  return;
59  }
60 
61  $changeItem = $this->slugRedirectChangeItemFactory->create((int)$id);
62  if ($changeItem === null) {
63  return;
64  }
65  $this->‪persistedChangedItems[(int)$id] = $changeItem;
66  }
67 
74  public function ‪processDatamap_afterDatabaseOperations(string $status, string $table, $id, array $fieldArray, DataHandler $dataHandler): void
75  {
76  $persistedChangedItem = $this->‪persistedChangedItems[(int)$id] ?? null;
77 
78  if (
79  $persistedChangedItem === null
80  || $table !== 'pages'
81  || $status !== 'update'
82  || empty($fieldArray['slug'])
83  || $persistedChangedItem->getOriginal()['slug'] === $fieldArray['slug']
84  || $this->‪isNestedHookInvocation($dataHandler)
85  ) {
86  return;
87  }
88  // We merge the fieldArray dataset into with the original record to spare a database query here.
89  $persistedChangedItem = $persistedChangedItem->withChanged(array_merge($persistedChangedItem->getOriginal(), $fieldArray));
90  $this->slugService->rebuildSlugsForSlugChange($id, $persistedChangedItem, $dataHandler->getCorrelationId());
91  }
92 
97  protected function ‪isNestedHookInvocation(DataHandler $dataHandler): bool
98  {
99  $correlationId = $dataHandler->getCorrelationId();
100  $correlationIdAspects = $correlationId ? $correlationId->getAspects() : [];
101  return in_array(‪SlugService::CORRELATION_ID_IDENTIFIER, $correlationIdAspects, true);
102  }
103 }
‪TYPO3\CMS\Core\DataHandling\DataHandler
Definition: DataHandler.php:94
‪TYPO3\CMS\Redirects\Service\SlugService\CORRELATION_ID_IDENTIFIER
‪final const CORRELATION_ID_IDENTIFIER
Definition: SlugService.php:58
‪TYPO3\CMS\Core\DataHandling\DataHandler\checkRecordUpdateAccess
‪bool checkRecordUpdateAccess($table, $id)
Definition: DataHandler.php:7021
‪TYPO3\CMS\Redirects\RedirectUpdate\SlugRedirectChangeItem
Definition: SlugRedirectChangeItem.php:30
‪TYPO3\CMS\Redirects\Hooks
Definition: DataHandlerCacheFlushingHook.php:18
‪TYPO3\CMS\Redirects\Hooks\DataHandlerSlugUpdateHook
Definition: DataHandlerSlugUpdateHook.php:30
‪TYPO3\CMS\Redirects\Hooks\DataHandlerSlugUpdateHook\processDatamap_afterDatabaseOperations
‪processDatamap_afterDatabaseOperations(string $status, string $table, $id, array $fieldArray, DataHandler $dataHandler)
Definition: DataHandlerSlugUpdateHook.php:73
‪TYPO3\CMS\Core\DataHandling\DataHandler\getCorrelationId
‪getCorrelationId()
Definition: DataHandler.php:9624
‪TYPO3\CMS\Core\Utility\MathUtility\canBeInterpretedAsInteger
‪static bool canBeInterpretedAsInteger(mixed $var)
Definition: MathUtility.php:69
‪TYPO3\CMS\Redirects\RedirectUpdate\SlugRedirectChangeItemFactory
Definition: SlugRedirectChangeItemFactory.php:31
‪TYPO3\CMS\Redirects\Hooks\DataHandlerSlugUpdateHook\persistedChangedItems
‪if($changeItem===null) $this persistedChangedItems[(int) $id]
Definition: DataHandlerSlugUpdateHook.php:64
‪TYPO3\CMS\Core\Utility\MathUtility
Definition: MathUtility.php:24
‪TYPO3\CMS\Redirects\Service\SlugService
Definition: SlugService.php:52
‪TYPO3\CMS\Redirects\Hooks\DataHandlerSlugUpdateHook\isNestedHookInvocation
‪isNestedHookInvocation(DataHandler $dataHandler)
Definition: DataHandlerSlugUpdateHook.php:96