‪TYPO3CMS  ‪main
PageModificationListener.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\Log\LoggerInterface;
21 use Symfony\Component\Messenger\MessageBusInterface;
22 use TYPO3\CMS\Backend\Utility\BackendUtility;
27 use TYPO3\CMS\Webhooks\Message\PageModificationMessage;
28 
38 {
39  public function ‪__construct(
40  protected readonly MessageBusInterface $bus,
41  protected readonly LoggerInterface $logger,
42  protected readonly ‪SiteFinder $siteFinder,
43  ) {}
44 
45  public function ‪processDatamap_afterDatabaseOperations($status, $table, $id, $fieldArray, ‪DataHandler $dataHandler)
46  {
47  if ($table !== 'pages') {
48  return;
49  }
51  $id = $dataHandler->substNEWwithIDs[$id];
52  }
53  try {
54  $id = (int)$id;
55  $site = $this->siteFinder->getSiteByPageId($id);
56  } catch (‪SiteNotFoundException) {
57  // The ID did not have a proper connection to a site, so this is skipped (e.g. when creating a fully new page tree)
58  return;
59  }
60  if ($status === 'new') {
61  $message = new PageModificationMessage(
62  'new',
63  $id,
64  $fieldArray,
65  (string)$site->getRouter()->generateUri($id),
66  $site->getIdentifier(),
67  $dataHandler->BE_USER,
68  );
69  } else {
70  if (isset($fieldArray['hidden'])) {
71  $action = $fieldArray['hidden'] ? 'unpublished' : 'published';
72  } else {
73  $action = 'modified';
74  }
75  $message = new PageModificationMessage(
76  $action,
77  $id,
78  BackendUtility::getRecord('pages', $id),
79  (string)$site->getRouter()->generateUri($id),
80  $site->getIdentifier(),
81  $dataHandler->BE_USER,
82  $fieldArray
83  );
84  }
85  $this->‪dispatchMessage($message);
86  }
87 
88  protected function ‪dispatchMessage(PageModificationMessage $message): void
89  {
90  try {
91  $this->bus->dispatch($message);
92  } catch (\Throwable $e) {
93  // At the moment we ignore every exception here, but we log them.
94  // An exception here means that an error happens while sending the webhook,
95  // and we should not block the execution of other configured webhooks.
96  // This can happen if no transport is configured, and the message is handled directly.
97  $this->logger->error(get_class($message) . ': ' . $e->getMessage());
98  }
99  }
100 }
‪TYPO3\CMS\Core\DataHandling\DataHandler
Definition: DataHandler.php:94
‪TYPO3\CMS\Webhooks\Listener\PageModificationListener\processDatamap_afterDatabaseOperations
‪processDatamap_afterDatabaseOperations($status, $table, $id, $fieldArray, DataHandler $dataHandler)
Definition: PageModificationListener.php:45
‪TYPO3\CMS\Webhooks\Listener\PageModificationListener
Definition: PageModificationListener.php:38
‪TYPO3\CMS\Core\Exception\SiteNotFoundException
Definition: SiteNotFoundException.php:25
‪TYPO3\CMS\Core\Site\SiteFinder
Definition: SiteFinder.php:31
‪TYPO3\CMS\Webhooks\Listener\PageModificationListener\dispatchMessage
‪dispatchMessage(PageModificationMessage $message)
Definition: PageModificationListener.php:88
‪TYPO3\CMS\Core\Utility\MathUtility\canBeInterpretedAsInteger
‪static bool canBeInterpretedAsInteger(mixed $var)
Definition: MathUtility.php:69
‪TYPO3\CMS\Webhooks\Listener
Definition: MessageListener.php:18
‪TYPO3\CMS\Webhooks\Listener\PageModificationListener\__construct
‪__construct(protected readonly MessageBusInterface $bus, protected readonly LoggerInterface $logger, protected readonly SiteFinder $siteFinder,)
Definition: PageModificationListener.php:39
‪TYPO3\CMS\Core\Utility\MathUtility
Definition: MathUtility.php:24