From cf5152337e945e88b0500dbc5200f2ebb9c5c896 Mon Sep 17 00:00:00 2001 From: Sybille Peters Date: Tue, 10 Dec 2024 18:58:15 +0100 Subject: [PATCH 1/5] [FEATURE] Make it possible to assign variables to view In event FormControllerFormActionEvent, make it possible to assign variables to view. Resolves: #1198 --- Classes/Controller/FormController.php | 2 +- .../Events/FormControllerFormActionEvent.php | 30 ++++++++++++++++++- 2 files changed, 30 insertions(+), 2 deletions(-) diff --git a/Classes/Controller/FormController.php b/Classes/Controller/FormController.php index d0dfdab76..c7ecd9aec 100644 --- a/Classes/Controller/FormController.php +++ b/Classes/Controller/FormController.php @@ -78,7 +78,7 @@ public function formAction(): ResponseInterface /** @var FormControllerFormActionEvent $event */ $event = $this->eventDispatcher->dispatch( - GeneralUtility::makeInstance(FormControllerFormActionEvent::class, $form, $this) + GeneralUtility::makeInstance(FormControllerFormActionEvent::class, $form, $this, $this->view) ); $form = $event->getForm(); SessionUtility::saveFormStartInSession($this->settings, $form); diff --git a/Classes/Events/FormControllerFormActionEvent.php b/Classes/Events/FormControllerFormActionEvent.php index aa2e2ce89..61000eb0b 100644 --- a/Classes/Events/FormControllerFormActionEvent.php +++ b/Classes/Events/FormControllerFormActionEvent.php @@ -5,6 +5,7 @@ use In2code\Powermail\Controller\FormController; use In2code\Powermail\Domain\Model\Form; +use TYPO3Fluid\Fluid\View\ViewInterface; final class FormControllerFormActionEvent { @@ -18,14 +19,17 @@ final class FormControllerFormActionEvent */ protected FormController $formController; + protected ViewInterface $view; + /** * @param Form|null $form * @param FormController $formController */ - public function __construct(?Form $form, FormController $formController) + public function __construct(?Form $form, FormController $formController, ViewInterface $view) { $this->form = $form; $this->formController = $formController; + $this->view = $view; } /** @@ -53,4 +57,28 @@ public function getFormController(): FormController { return $this->formController; } + + /** + * Add a variable to the view data collection. + * Can be chained, so $this->view->assign(..., ...)->assign(..., ...); is possible + * + * @param string $key Key of variable + * @param mixed $value Value of object + * @return ViewInterface an instance of $this, to enable chaining + */ + public function assign(string $key, mixed $value) + { + $this->view->assign($key, $value) + } + + /** + * Add multiple variables to the view data collection + * + * @param array $values array in the format array(key1 => value1, key2 => value2) + * @return ViewInterface an instance of $this, to enable chaining + */ + public function assignMultiple(array $values) + { + $this->view->assignMultiple($values); + } } From ed7956250b903c309c5cc660f1b970a6317f9410 Mon Sep 17 00:00:00 2001 From: Sybille Peters Date: Tue, 10 Dec 2024 19:05:04 +0100 Subject: [PATCH 2/5] Fix lint --- Classes/Events/FormControllerFormActionEvent.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Classes/Events/FormControllerFormActionEvent.php b/Classes/Events/FormControllerFormActionEvent.php index 61000eb0b..60b620f37 100644 --- a/Classes/Events/FormControllerFormActionEvent.php +++ b/Classes/Events/FormControllerFormActionEvent.php @@ -68,7 +68,7 @@ public function getFormController(): FormController */ public function assign(string $key, mixed $value) { - $this->view->assign($key, $value) + $this->view->assign($key, $value); } /** From 2f85e33308d4e42fdf2f3f1cfa5ffe9cc3c4f9f5 Mon Sep 17 00:00:00 2001 From: Sybille Peters Date: Tue, 10 Dec 2024 19:09:33 +0100 Subject: [PATCH 3/5] Fix phpstan --- Classes/Events/FormControllerFormActionEvent.php | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/Classes/Events/FormControllerFormActionEvent.php b/Classes/Events/FormControllerFormActionEvent.php index 60b620f37..7b873cc48 100644 --- a/Classes/Events/FormControllerFormActionEvent.php +++ b/Classes/Events/FormControllerFormActionEvent.php @@ -69,16 +69,18 @@ public function getFormController(): FormController public function assign(string $key, mixed $value) { $this->view->assign($key, $value); + return $this->view; } /** * Add multiple variables to the view data collection * - * @param array $values array in the format array(key1 => value1, key2 => value2) + * @param array $values array in the format array(key1 => value1, key2 => value2) * @return ViewInterface an instance of $this, to enable chaining */ public function assignMultiple(array $values) { $this->view->assignMultiple($values); + return $this->view; } } From 37213410770cd54662fc31b1cee84bc3a4a72594 Mon Sep 17 00:00:00 2001 From: Sybille Peters Date: Wed, 11 Dec 2024 20:59:09 +0100 Subject: [PATCH 4/5] Change functions in event --- Classes/Controller/FormController.php | 5 ++- .../Events/FormControllerFormActionEvent.php | 33 +++++++------------ 2 files changed, 16 insertions(+), 22 deletions(-) diff --git a/Classes/Controller/FormController.php b/Classes/Controller/FormController.php index c7ecd9aec..c40735cf7 100644 --- a/Classes/Controller/FormController.php +++ b/Classes/Controller/FormController.php @@ -78,8 +78,11 @@ public function formAction(): ResponseInterface /** @var FormControllerFormActionEvent $event */ $event = $this->eventDispatcher->dispatch( - GeneralUtility::makeInstance(FormControllerFormActionEvent::class, $form, $this, $this->view) + GeneralUtility::makeInstance(FormControllerFormActionEvent::class, $form, $this) ); + if ($event->getViewVariables()) { + $this->view->assignMultiple($event->getViewVariables()); + } $form = $event->getForm(); SessionUtility::saveFormStartInSession($this->settings, $form); $this->view->assignMultiple( diff --git a/Classes/Events/FormControllerFormActionEvent.php b/Classes/Events/FormControllerFormActionEvent.php index 7b873cc48..c25f8ef1c 100644 --- a/Classes/Events/FormControllerFormActionEvent.php +++ b/Classes/Events/FormControllerFormActionEvent.php @@ -5,7 +5,6 @@ use In2code\Powermail\Controller\FormController; use In2code\Powermail\Domain\Model\Form; -use TYPO3Fluid\Fluid\View\ViewInterface; final class FormControllerFormActionEvent { @@ -19,17 +18,19 @@ final class FormControllerFormActionEvent */ protected FormController $formController; - protected ViewInterface $view; + /** + * @var array + */ + protected array $viewVariables = []; /** * @param Form|null $form * @param FormController $formController */ - public function __construct(?Form $form, FormController $formController, ViewInterface $view) + public function __construct(?Form $form, FormController $formController) { $this->form = $form; $this->formController = $formController; - $this->view = $view; } /** @@ -58,29 +59,19 @@ public function getFormController(): FormController return $this->formController; } - /** - * Add a variable to the view data collection. - * Can be chained, so $this->view->assign(..., ...)->assign(..., ...); is possible - * - * @param string $key Key of variable - * @param mixed $value Value of object - * @return ViewInterface an instance of $this, to enable chaining - */ - public function assign(string $key, mixed $value) + public function getViewVariables(): array { - $this->view->assign($key, $value); - return $this->view; + return $this->viewVariables; } /** - * Add multiple variables to the view data collection + * Add additional variables to the view * - * @param array $values array in the format array(key1 => value1, key2 => value2) - * @return ViewInterface an instance of $this, to enable chaining + * @param array $additionalVariables + * @return void */ - public function assignMultiple(array $values) + public function addViewVariables(array $variables): void { - $this->view->assignMultiple($values); - return $this->view; + $this->viewVariables = array_merge($this->viewVariables, $variables); } } From e82c770a5a8fad47ae8dff6a0323f646fbfd6df7 Mon Sep 17 00:00:00 2001 From: Sybille Peters Date: Wed, 11 Dec 2024 22:06:11 +0100 Subject: [PATCH 5/5] Fix phpstan --- Classes/Events/FormControllerFormActionEvent.php | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/Classes/Events/FormControllerFormActionEvent.php b/Classes/Events/FormControllerFormActionEvent.php index c25f8ef1c..f2c036be6 100644 --- a/Classes/Events/FormControllerFormActionEvent.php +++ b/Classes/Events/FormControllerFormActionEvent.php @@ -59,6 +59,9 @@ public function getFormController(): FormController return $this->formController; } + /** + * @return array + */ public function getViewVariables(): array { return $this->viewVariables; @@ -67,7 +70,7 @@ public function getViewVariables(): array /** * Add additional variables to the view * - * @param array $additionalVariables + * @param array $variables * @return void */ public function addViewVariables(array $variables): void