|
| 1 | +<?php |
| 2 | + |
| 3 | +/** |
| 4 | + * This file is part of MetaModels/core. |
| 5 | + * |
| 6 | + * (c) 2012-2025 The MetaModels team. |
| 7 | + * |
| 8 | + * For the full copyright and license information, please view the LICENSE |
| 9 | + * file that was distributed with this source code. |
| 10 | + * |
| 11 | + * This project is provided in good faith and hope to be usable by anyone. |
| 12 | + * |
| 13 | + * @package MetaModels/core |
| 14 | + * @author Ingolf Steinhardt <[email protected]> |
| 15 | + * @copyright 2012-2025 The MetaModels team. |
| 16 | + * @license https://github.com/MetaModels/core/blob/master/LICENSE LGPL-3.0-or-later |
| 17 | + * @filesource |
| 18 | + */ |
| 19 | + |
| 20 | +namespace MetaModels\CoreBundle\EventListener\DcGeneral\Table; |
| 21 | + |
| 22 | +use ContaoCommunityAlliance\DcGeneral\Contao\RequestScopeDeterminator; |
| 23 | +use ContaoCommunityAlliance\DcGeneral\Contao\View\Contao2BackendView\Event\GetSelectModeButtonsEvent; |
| 24 | +use ContaoCommunityAlliance\DcGeneral\DataDefinition\ContainerInterface; |
| 25 | +use ContaoCommunityAlliance\DcGeneral\Event\AbstractEnvironmentAwareEvent; |
| 26 | +use ContaoCommunityAlliance\DcGeneral\Event\AbstractModelAwareEvent; |
| 27 | + |
| 28 | +use function array_key_exists; |
| 29 | +use function in_array; |
| 30 | + |
| 31 | +class SelectModeButtonsListener |
| 32 | +{ |
| 33 | + public function __construct( |
| 34 | + private readonly RequestScopeDeterminator $scopeDeterminator |
| 35 | + ) { |
| 36 | + } |
| 37 | + |
| 38 | + /** |
| 39 | + * Delete copy button at edit all. |
| 40 | + * |
| 41 | + * @param GetSelectModeButtonsEvent $event The event. |
| 42 | + * |
| 43 | + * @return void |
| 44 | + */ |
| 45 | + public function handle(GetSelectModeButtonsEvent $event): void |
| 46 | + { |
| 47 | + if (!$this->wantToHandle($event)) { |
| 48 | + return; |
| 49 | + } |
| 50 | + |
| 51 | + $buttons = $event->getButtons(); |
| 52 | + if (array_key_exists('copy', $buttons)) { |
| 53 | + unset($buttons['copy']); |
| 54 | + $event->setButtons($buttons); |
| 55 | + } |
| 56 | + } |
| 57 | + |
| 58 | + /** |
| 59 | + * Test if the event is for the correct table and in backend scope. |
| 60 | + * |
| 61 | + * @param AbstractEnvironmentAwareEvent $event The event to test. |
| 62 | + * |
| 63 | + * @return bool |
| 64 | + */ |
| 65 | + protected function wantToHandle(AbstractEnvironmentAwareEvent $event) |
| 66 | + { |
| 67 | + if (!$this->scopeDeterminator->currentScopeIsBackend()) { |
| 68 | + return false; |
| 69 | + } |
| 70 | + |
| 71 | + $environment = $event->getEnvironment(); |
| 72 | + $dataDefinition = $environment->getDataDefinition(); |
| 73 | + assert($dataDefinition instanceof ContainerInterface); |
| 74 | + |
| 75 | + if ( |
| 76 | + !in_array( |
| 77 | + $dataDefinition->getName(), |
| 78 | + [ |
| 79 | + 'tl_metamodel', |
| 80 | + 'tl_metamodel_attribute', |
| 81 | + 'tl_metamodel_dca_sortgroup', |
| 82 | + 'tl_metamodel_dcasetting', |
| 83 | + 'tl_metamodel_dcasetting_condition', |
| 84 | + 'tl_metamodel_rendersetting', |
| 85 | + 'tl_metamodel_searchable_pages', |
| 86 | + ] |
| 87 | + ) |
| 88 | + ) { |
| 89 | + return false; |
| 90 | + } |
| 91 | + |
| 92 | + if ( |
| 93 | + ($event instanceof AbstractModelAwareEvent) |
| 94 | + && $dataDefinition->getName() !== $event->getModel()->getProviderName() |
| 95 | + ) { |
| 96 | + return false; |
| 97 | + } |
| 98 | + |
| 99 | + return true; |
| 100 | + } |
| 101 | +} |
0 commit comments