|
| 1 | +.. include:: /Includes.rst.txt |
| 2 | + |
| 3 | +.. _events: |
| 4 | + |
| 5 | +======================= |
| 6 | +PSR-14 Events |
| 7 | +======================= |
| 8 | + |
| 9 | +Use the :code:`FrontendEditDropdownModifyEvent` to modify the edit menu to your needs. You can add, remove or modify buttons for specific content elements. See the example below: |
| 10 | + |
| 11 | +.. code-block:: php |
| 12 | + :caption: Classes/EventListener/ModifyFrontendEditListener.php |
| 13 | +
|
| 14 | + <?php |
| 15 | +
|
| 16 | + declare(strict_types=1); |
| 17 | +
|
| 18 | + namespace Vendor\Package\EventListener; |
| 19 | +
|
| 20 | + use TYPO3\CMS\Backend\Routing\UriBuilder; |
| 21 | + use TYPO3\CMS\Core\Imaging\IconFactory; |
| 22 | + use TYPO3\CMS\Core\Utility\GeneralUtility; |
| 23 | + use Xima\XimaTypo3FrontendEdit\Enumerations\ButtonType; |
| 24 | + use Xima\XimaTypo3FrontendEdit\Event\FrontendEditDropdownModifyEvent; |
| 25 | + use Xima\XimaTypo3FrontendEdit\Template\Component\Button; |
| 26 | +
|
| 27 | + class ModifyFrontendEditListener |
| 28 | + { |
| 29 | + public function __construct(protected readonly IconFactory $iconFactory, protected readonly UriBuilder $uriBuilder) |
| 30 | + { |
| 31 | + } |
| 32 | +
|
| 33 | + public function __invoke(FrontendEditDropdownModifyEvent $event): void |
| 34 | + { |
| 35 | + $contentElement = $event->getContentElement(); |
| 36 | + $menuButton = $event->getMenuButton(); |
| 37 | +
|
| 38 | + // Example 1 |
| 39 | + // Append a custom button (after the existing edit_page button) for your plugin to e.g. edit the referenced entity |
| 40 | + if ($contentElement['CType'] === 'list' && $contentElement['list_type'] === 'custom_plugin_name') { |
| 41 | + $menuButton->appendAfterChild(new Button( |
| 42 | + 'Edit entity', |
| 43 | + ButtonType::Link, |
| 44 | + $this->uriBuilder->buildUriFromRoute( |
| 45 | + 'record_edit', |
| 46 | + [ |
| 47 | + 'edit' => [ |
| 48 | + 'custom_entity' => [ |
| 49 | + $contentElement['custom_entity_uid'] => 'edit', |
| 50 | + ], |
| 51 | + ], |
| 52 | + 'returnUrl' => $event->getReturnUrl(), |
| 53 | + ], |
| 54 | + )->__toString(), |
| 55 | + $this->iconFactory->getIcon('content-idea', 'small') |
| 56 | + ), |
| 57 | + 'edit_page', |
| 58 | + 'edit_custom_entity' |
| 59 | + ); |
| 60 | + } |
| 61 | +
|
| 62 | + // Example 2 |
| 63 | + // Remove existing buttons |
| 64 | + $menuButton->removeChild('div_action'); |
| 65 | +
|
| 66 | + $event->setMenuButton($menuButton); |
| 67 | + } |
| 68 | + } |
| 69 | +
|
| 70 | +Don't forget to register your event listener via PHP attributes (TYPO3 >= 13): |
| 71 | + |
| 72 | + |
| 73 | +.. code-block:: php |
| 74 | + :caption: Classes/EventListener/ModifyFrontendEditListener.php |
| 75 | +
|
| 76 | + #[AsEventListener( |
| 77 | + identifier: 'ext-some-extension/modify-frontend-edit-listener', |
| 78 | + )] |
| 79 | +
|
| 80 | +or register the event listener in your :code:`Services.yaml`: |
| 81 | + |
| 82 | +.. code-block:: yaml |
| 83 | + :caption: Configuration/Services.yaml |
| 84 | +
|
| 85 | + services: |
| 86 | + Vendor\Package\EventListener\ModifyFrontendEditListener: |
| 87 | + tags: |
| 88 | + - name: event.listener |
| 89 | + identifier: 'ext-some-extension/modify-frontend-edit-listener' |
0 commit comments