|
| 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 | + |
| 18 | +namespace TYPO3\CMS\Scheduler\EventListener; |
| 19 | + |
| 20 | +use Psr\Http\Message\ServerRequestInterface; |
| 21 | +use TYPO3\CMS\Backend\Routing\UriBuilder; |
| 22 | +use TYPO3\CMS\Backend\Template\Components\ModifyButtonBarEvent; |
| 23 | +use TYPO3\CMS\Core\Attribute\AsEventListener; |
| 24 | +use TYPO3\CMS\Core\Imaging\IconFactory; |
| 25 | +use TYPO3\CMS\Core\Imaging\IconSize; |
| 26 | +use TYPO3\CMS\Core\Localization\LanguageService; |
| 27 | +use TYPO3\CMS\Core\Page\PageRenderer; |
| 28 | +use TYPO3\CMS\Core\Utility\GeneralUtility; |
| 29 | + |
| 30 | +/** |
| 31 | + * Listener replaces the "New" button of FormEngine with the button to open the scheduler task wizard |
| 32 | + */ |
| 33 | +final readonly class ReplaceAddNewButtonToFormEngine |
| 34 | +{ |
| 35 | + public function __construct( |
| 36 | + private IconFactory $iconFactory, |
| 37 | + private PageRenderer $pageRenderer, |
| 38 | + private UriBuilder $uriBuilder, |
| 39 | + ) {} |
| 40 | + |
| 41 | + #[AsEventListener] |
| 42 | + public function __invoke(ModifyButtonBarEvent $event): void |
| 43 | + { |
| 44 | + $request = $this->getRequest(); |
| 45 | + |
| 46 | + if (($request->getAttribute('routing')?->getRoute()?->getOptions()['_identifier'] ?? '') !== 'record_edit') { |
| 47 | + return; |
| 48 | + } |
| 49 | + |
| 50 | + $editConfig = $request->getQueryParams()['edit'] ?? null; |
| 51 | + if (!is_array($editConfig) || $editConfig === [] || count($editConfig) > 1 || key($editConfig) !== 'tx_scheduler_task') { |
| 52 | + return; |
| 53 | + } |
| 54 | + |
| 55 | + $buttons = $event->getButtons(); |
| 56 | + $leftButtons = $buttons['left'] ?? []; |
| 57 | + |
| 58 | + $this->pageRenderer->loadJavaScriptModule('@typo3/scheduler/new-scheduler-task-wizard-button.js'); |
| 59 | + |
| 60 | + $addTaskUrl = (string)$this->uriBuilder->buildUriFromRoute('ajax_new_scheduler_task_wizard', [ |
| 61 | + 'returnUrl' => GeneralUtility::sanitizeLocalUrl($request->getQueryParams()['returnUrl'] ?? '') ?: $request->getAttribute('normalizedParams')->getRequestUri(), |
| 62 | + ]); |
| 63 | + |
| 64 | + $languageService = $this->getLanguageService(); |
| 65 | + $newButton = $event->getButtonBar()->makeFullyRenderedButton()->setHtmlSource( |
| 66 | + '<typo3-scheduler-new-task-wizard-button url="' . $addTaskUrl . '" subject="' . htmlspecialchars($languageService->sL('LLL:EXT:scheduler/Resources/Private/Language/locallang.xlf:function.add')) . '">' |
| 67 | + . $this->iconFactory->getIcon('actions-plus', IconSize::SMALL) . htmlspecialchars($languageService->sL('LLL:EXT:scheduler/Resources/Private/Language/locallang.xlf:function.add')) . |
| 68 | + '</typo3-scheduler-new-task-wizard-button>' |
| 69 | + ); |
| 70 | + |
| 71 | + // Find and replace t3js-editform-new button |
| 72 | + // By replacing the existing button we ensure to respect TSconfig and that user has necessary permissions |
| 73 | + foreach ($leftButtons as $groupIndex => $buttonGroup) { |
| 74 | + foreach ($buttonGroup as $buttonIndex => $button) { |
| 75 | + if (method_exists($button, 'getClasses') && str_contains($button->getClasses(), 't3js-editform-new')) { |
| 76 | + $leftButtons[$groupIndex][$buttonIndex] = $newButton; |
| 77 | + } |
| 78 | + } |
| 79 | + } |
| 80 | + |
| 81 | + $buttons['left'] = $leftButtons; |
| 82 | + $event->setButtons($buttons); |
| 83 | + } |
| 84 | + |
| 85 | + private function getLanguageService(): LanguageService |
| 86 | + { |
| 87 | + return $GLOBALS['LANG']; |
| 88 | + } |
| 89 | + |
| 90 | + private function getRequest(): ServerRequestInterface |
| 91 | + { |
| 92 | + return $GLOBALS['TYPO3_REQUEST']; |
| 93 | + } |
| 94 | +} |
0 commit comments