1
|
|
|
<?php |
2
|
|
|
/* |
3
|
|
|
* 2017 Romain CANON <[email protected]> |
4
|
|
|
* |
5
|
|
|
* This file is part of the TYPO3 FormZ project. |
6
|
|
|
* It is free software; you can redistribute it and/or modify it |
7
|
|
|
* under the terms of the GNU General Public License, either |
8
|
|
|
* version 3 of the License, or any later version. |
9
|
|
|
* |
10
|
|
|
* For the full copyright and license information, see: |
11
|
|
|
* http://www.gnu.org/licenses/gpl-3.0.html |
12
|
|
|
*/ |
13
|
|
|
|
14
|
|
|
namespace Romm\Formz\Middleware\Item\FormInjection; |
15
|
|
|
|
16
|
|
|
use Romm\Formz\Core\Core; |
17
|
|
|
use Romm\Formz\Middleware\Item\OnBeginMiddleware; |
18
|
|
|
use Romm\Formz\Middleware\Signal\SendsMiddlewareSignal; |
19
|
|
|
use Romm\Formz\Middleware\Processor\PresetMiddlewareInterface; |
20
|
|
|
use Romm\Formz\Middleware\Processor\RemoveFromSingleFieldValidationContext; |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* This middleware takes care of creating new form instances, based on the |
24
|
|
|
* arguments of the current request. |
25
|
|
|
* |
26
|
|
|
* It is based on the controller action of the current context: each argument |
27
|
|
|
* that has a form type (class that implements `FormInterface`) and is not found |
28
|
|
|
* in the request arguments is injected with an empty form instance. |
29
|
|
|
* |
30
|
|
|
* The goal is to provide a form instance to the controller in every case, so |
31
|
|
|
* the developer can manipulate it easily, for instance by pre-setting values. |
32
|
|
|
*/ |
33
|
|
|
class FormInjectionMiddleware extends OnBeginMiddleware implements PresetMiddlewareInterface, SendsMiddlewareSignal, RemoveFromSingleFieldValidationContext |
34
|
|
|
{ |
35
|
|
|
/** |
36
|
|
|
* @var int |
37
|
|
|
*/ |
38
|
|
|
protected $priority = self::PRIORITY_INJECT_FORM; |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* @see FormInjectionMiddleware |
42
|
|
|
*/ |
43
|
|
|
protected function process() |
44
|
|
|
{ |
45
|
|
|
$formObject = $this->getFormObject(); |
46
|
|
|
|
47
|
|
|
$this->beforeSignal()->dispatch(); |
48
|
|
|
|
49
|
|
|
if (false === $formObject->hasForm()) { |
50
|
|
|
/* |
51
|
|
|
* Creating an empty instance of the form. |
52
|
|
|
*/ |
53
|
|
|
$formClassName = $formObject->getClassName(); |
54
|
|
|
$form = Core::get()->getObjectManager()->getEmptyObject($formClassName); |
55
|
|
|
|
56
|
|
|
/* |
57
|
|
|
* The form instance is injected in the form object, for further |
58
|
|
|
* usage. |
59
|
|
|
*/ |
60
|
|
|
$formObject->setForm($form); |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
$this->afterSignal()->dispatch(); |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
/** |
67
|
|
|
* @return array |
68
|
|
|
*/ |
69
|
|
|
public function getAllowedSignals() |
70
|
|
|
{ |
71
|
|
|
return [FormInjectionSignal::class]; |
72
|
|
|
} |
73
|
|
|
} |
74
|
|
|
|