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\ViewHelpers; |
15
|
|
|
|
16
|
|
|
use Romm\Formz\Core\Core; |
17
|
|
|
use Romm\Formz\Exceptions\ContextNotFoundException; |
18
|
|
|
use Romm\Formz\Exceptions\InvalidOptionValueException; |
19
|
|
|
use Romm\Formz\Exceptions\MissingArgumentException; |
20
|
|
|
use Romm\Formz\Form\FormInterface; |
21
|
|
|
use Romm\Formz\Form\FormObjectFactory; |
22
|
|
|
use Romm\Formz\Service\ViewHelper\FormViewHelperService; |
23
|
|
|
use TYPO3\CMS\Fluid\Core\Rendering\RenderingContextInterface; |
24
|
|
|
use TYPO3\CMS\Fluid\Core\ViewHelper\Facets\CompilableInterface; |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* Use this view helper to get the identifier hash of a given form. |
28
|
|
|
* |
29
|
|
|
* If you use it inside the `FormViewHelper`, you don't have to fill the |
30
|
|
|
* arguments `form` and `name`. |
31
|
|
|
*/ |
32
|
|
|
class FormIdentifierHashViewHelper extends AbstractViewHelper implements CompilableInterface |
33
|
|
|
{ |
34
|
|
|
/** |
35
|
|
|
* @inheritdoc |
36
|
|
|
*/ |
37
|
|
|
public function initializeArguments() |
38
|
|
|
{ |
39
|
|
|
$this->registerArgument('form', 'object', 'Form instance, that must implement `FormInterface`. If this argument is filled, you must also fill the argument `name`.'); |
40
|
|
|
$this->registerArgument('name', 'string', 'Name of the form, which is used only if the argument `form` is filled.'); |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* @inheritdoc |
45
|
|
|
*/ |
46
|
|
|
public function render() |
47
|
|
|
{ |
48
|
|
|
return self::renderStatic($this->arguments, $this->buildRenderChildrenClosure(), $this->renderingContext); |
|
|
|
|
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* @inheritdoc |
53
|
|
|
*/ |
54
|
|
|
public static function renderStatic(array $arguments, \Closure $renderChildrenClosure, RenderingContextInterface $renderingContext) |
55
|
|
|
{ |
56
|
|
|
/** @var FormViewHelperService $formService */ |
57
|
|
|
$formService = Core::instantiate(FormViewHelperService::class); |
58
|
|
|
|
59
|
|
|
$formObject = null; |
60
|
|
|
$form = $arguments['form']; |
61
|
|
|
|
62
|
|
|
if (null !== $form) { |
63
|
|
|
if (false === is_object($form)) { |
64
|
|
|
throw InvalidOptionValueException::formIdentifierViewHelperWrongFormValueType($form); |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
if (false === $form instanceof FormInterface) { |
68
|
|
|
throw InvalidOptionValueException::formIdentifierViewHelperWrongFormValueObjectType($form); |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
if (null === $arguments['name']) { |
72
|
|
|
throw MissingArgumentException::formIdentifierNameMissing(); |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
/** @var FormObjectFactory $formObjectFactory */ |
76
|
|
|
$formObjectFactory = Core::instantiate(FormObjectFactory::class); |
77
|
|
|
|
78
|
|
|
$formObject = $formObjectFactory->getInstanceFromFormInstance($form, $arguments['name']); |
79
|
|
|
} elseif ($formService->formContextExists()) { |
80
|
|
|
$formObject = $formService->getFormObject(); |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
if (null === $formObject) { |
84
|
|
|
throw ContextNotFoundException::formIdentifierViewHelperFormContextNotFound(); |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
return $formObject->getFormIdentifierHash(); |
88
|
|
|
} |
89
|
|
|
} |
90
|
|
|
|
This check looks for parameters that are defined as one type in their type hint or doc comment but seem to be used as a narrower type, i.e an implementation of an interface or a subclass.
Consider changing the type of the parameter or doing an instanceof check before assuming your parameter is of the expected type.