Created
September 26, 2023 12:58
-
-
Save maikschneider/728e41292cbb92d7b2ec3ec765efd63d to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <?php | |
| namespace Vendor\MyExt\Command; | |
| use Symfony\Component\Console\Command\Command; | |
| use Symfony\Component\Console\Input\InputArgument; | |
| use Symfony\Component\Console\Input\InputInterface; | |
| use Symfony\Component\Console\Output\OutputInterface; | |
| use TYPO3\CMS\Form\Domain\Configuration\FormDefinitionConversionService; | |
| use TYPO3\CMS\Form\Mvc\Persistence\FormPersistenceManagerInterface; | |
| class FormDefinitionUpdateCommand extends Command | |
| { | |
| private const FORM_PERSISTENCE_IDENTIFIER = [ | |
| '1:/user_upload/feedback.form.yaml', | |
| ]; | |
| public function __construct( | |
| protected FormPersistenceManagerInterface $formPersistenceManager, | |
| protected FormDefinitionConversionService $conversionService, | |
| string $name = null | |
| ) { | |
| parent::__construct($name); | |
| } | |
| protected function configure(): void | |
| { | |
| $this->setHelp('Update old form definitions to new schema (recipient and language configuration)'); | |
| $this->addArgument('persistenceIdentifier', InputArgument::OPTIONAL); | |
| } | |
| public function execute(InputInterface $input, OutputInterface $output): int | |
| { | |
| $inputIdentifier = $input->getArgument('persistenceIdentifier'); | |
| $formIdentifier = $inputIdentifier ? [$inputIdentifier] : self::FORM_PERSISTENCE_IDENTIFIER; | |
| foreach ($formIdentifier as $identifier) { | |
| $this->migrateForm($identifier); | |
| } | |
| return Command::SUCCESS; | |
| } | |
| protected function migrateForm(string $formPersistenceIdentifier): void | |
| { | |
| $formDefinition = $this->formPersistenceManager->load($formPersistenceIdentifier); | |
| $formDefinition = $this->migrateEmailFinisherRecipients($formDefinition); | |
| $formDefinition = $this->conversionService->migrateFinisherConfiguration($formDefinition); | |
| $this->formPersistenceManager->save($formPersistenceIdentifier, $formDefinition); | |
| } | |
| protected function migrateEmailFinisherRecipients(array $formDefinition): array | |
| { | |
| foreach ($formDefinition['finishers'] ?? [] as $i => $finisherConfiguration) { | |
| if (!in_array($finisherConfiguration['identifier'], ['EmailToSender', 'EmailToReceiver'], true)) { | |
| continue; | |
| } | |
| $recipientAddress = $finisherConfiguration['options']['recipientAddress'] ?? ''; | |
| $recipientName = $finisherConfiguration['options']['recipientName'] ?? ''; | |
| $carbonCopyAddress = $finisherConfiguration['options']['carbonCopyAddress'] ?? ''; | |
| $blindCarbonCopyAddress = $finisherConfiguration['options']['blindCarbonCopyAddress'] ?? ''; | |
| $replyToAddress = $finisherConfiguration['options']['replyToAddress'] ?? ''; | |
| if (!empty($recipientAddress)) { | |
| $finisherConfiguration['options']['recipients'][$recipientAddress] = $recipientName; | |
| } | |
| if (!empty($carbonCopyAddress)) { | |
| $finisherConfiguration['options']['carbonCopyRecipients'][$carbonCopyAddress] = ''; | |
| } | |
| if (!empty($blindCarbonCopyAddress)) { | |
| $finisherConfiguration['options']['blindCarbonCopyRecipients'][$blindCarbonCopyAddress] = ''; | |
| } | |
| if (!empty($replyToAddress)) { | |
| $finisherConfiguration['options']['replyToRecipients'][$replyToAddress] = ''; | |
| } | |
| unset( | |
| $finisherConfiguration['options']['recipientAddress'], | |
| $finisherConfiguration['options']['recipientName'], | |
| $finisherConfiguration['options']['carbonCopyAddress'], | |
| $finisherConfiguration['options']['blindCarbonCopyAddress'], | |
| $finisherConfiguration['options']['replyToAddress'] | |
| ); | |
| $formDefinition['finishers'][$i] = $finisherConfiguration; | |
| } | |
| return $formDefinition; | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment