Skip to content

Commit

Permalink
[FEATURE] Make it possible to assign variables to view
Browse files Browse the repository at this point in the history
In event FormControllerFormActionEvent, make it possible to assign
variables to view.

Resolves: in2code-de#1198
  • Loading branch information
sypets committed Dec 10, 2024
1 parent ec096b4 commit cf51523
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 2 deletions.
2 changes: 1 addition & 1 deletion Classes/Controller/FormController.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
30 changes: 29 additions & 1 deletion Classes/Events/FormControllerFormActionEvent.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

use In2code\Powermail\Controller\FormController;
use In2code\Powermail\Domain\Model\Form;
use TYPO3Fluid\Fluid\View\ViewInterface;

final class FormControllerFormActionEvent
{
Expand All @@ -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;
}

/**
Expand Down Expand Up @@ -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);
}
}

0 comments on commit cf51523

Please sign in to comment.