|
| 1 | +<?php |
| 2 | + |
| 3 | +/* |
| 4 | + * This file is part of the "LIA Form" Extension for TYPO3 CMS. |
| 5 | + * |
| 6 | + * For the full copyright and license information, please read the |
| 7 | + * LICENSE.txt file that was distributed with this source code. |
| 8 | + */ |
| 9 | + |
| 10 | +declare(strict_types=1); |
| 11 | + |
| 12 | +namespace LIA\LiaForm\EventListener; |
| 13 | + |
| 14 | +use Psr\Http\Message\ServerRequestInterface; |
| 15 | +use TYPO3\CMS\Core\Attribute\AsEventListener; |
| 16 | +use TYPO3\CMS\Form\Domain\Runtime\FormRuntime; |
| 17 | +use TYPO3\CMS\Form\Event\AfterCurrentPageIsResolvedEvent; |
| 18 | + |
| 19 | +/** |
| 20 | + * Seeds the default value of LiaSiteTitle elements with the current page title. |
| 21 | + * |
| 22 | + * The listener identifier is kept as-is: it names the event this listener |
| 23 | + * subscribes to, and consuming extensions may order against it via before/after. |
| 24 | + * It no longer implies anything about $event->currentPage though: this listener |
| 25 | + * only seeds FormState values. |
| 26 | + * |
| 27 | + * @author Johannes Delesky, LOUIS INTERNET <delesky@louis.info> |
| 28 | + */ |
| 29 | +#[AsEventListener( |
| 30 | + identifier: 'lia-form/after-current-page-is-resolved', |
| 31 | + event: AfterCurrentPageIsResolvedEvent::class |
| 32 | +)] |
| 33 | +final class LiaSiteTitleDefaultValueEventListener |
| 34 | +{ |
| 35 | + /** |
| 36 | + * Handle the AfterCurrentPageIsResolvedEvent. |
| 37 | + * |
| 38 | + * Sets default values, the resolved page is left untouched. |
| 39 | + */ |
| 40 | + public function __invoke(AfterCurrentPageIsResolvedEvent $event): void |
| 41 | + { |
| 42 | + // Set default values for LiaSiteTitle elements |
| 43 | + $this->setLiaSiteTitleDefaultValue($event->formRuntime, $event->request); |
| 44 | + } |
| 45 | + |
| 46 | + /** |
| 47 | + * Set default value for LiaSiteTitle elements in FormState. |
| 48 | + * |
| 49 | + * @param FormRuntime $formRuntime The form runtime instance |
| 50 | + * @param ServerRequestInterface $request The request object |
| 51 | + */ |
| 52 | + private function setLiaSiteTitleDefaultValue(FormRuntime $formRuntime, ServerRequestInterface $request): void |
| 53 | + { |
| 54 | + $formState = $formRuntime->getFormState(); |
| 55 | + if ($formState === null) { |
| 56 | + return; |
| 57 | + } |
| 58 | + |
| 59 | + // Get page title from request |
| 60 | + $pageInformation = $request->getAttribute('frontend.page.information'); |
| 61 | + if ($pageInformation === null) { |
| 62 | + return; |
| 63 | + } |
| 64 | + |
| 65 | + $currentTitle = (string)($pageInformation->getPageRecord()['title'] ?? ''); |
| 66 | + if ($currentTitle === '') { |
| 67 | + return; |
| 68 | + } |
| 69 | + |
| 70 | + // Find all LiaSiteTitle elements and set their value in FormState |
| 71 | + foreach ($formRuntime->getFormDefinition()->getRenderablesRecursively() as $element) { |
| 72 | + if ($element->getType() === 'LiaSiteTitle') { |
| 73 | + $existingValue = $formRuntime->getElementValue($element->getIdentifier()); |
| 74 | + if ($existingValue === null || $existingValue === '') { |
| 75 | + $formState->setFormValue($element->getIdentifier(), $currentTitle); |
| 76 | + } |
| 77 | + } |
| 78 | + } |
| 79 | + } |
| 80 | +} |
0 commit comments