|
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\Form\FormInterface; |
|
20
|
|
|
use Romm\Formz\Form\FormObject\FormObjectFactory; |
|
21
|
|
|
use Romm\Formz\Service\ViewHelper\FormViewHelperService; |
|
22
|
|
|
use TYPO3\CMS\Fluid\Core\Rendering\RenderingContextInterface; |
|
23
|
|
|
use TYPO3\CMS\Fluid\Core\ViewHelper\Facets\CompilableInterface; |
|
24
|
|
|
|
|
25
|
|
|
/** |
|
26
|
|
|
* Use this view helper to get the identifier hash of a given form. |
|
27
|
|
|
* |
|
28
|
|
|
* If you use it inside the `FormViewHelper`, you don't have to fill the |
|
29
|
|
|
* arguments `form` and `name`. |
|
30
|
|
|
*/ |
|
31
|
|
|
class FormIdentifierHashViewHelper extends AbstractViewHelper implements CompilableInterface |
|
32
|
|
|
{ |
|
33
|
|
|
/** |
|
34
|
|
|
* @inheritdoc |
|
35
|
|
|
*/ |
|
36
|
|
|
public function initializeArguments() |
|
37
|
|
|
{ |
|
38
|
|
|
$this->registerArgument('form', 'object', 'Form instance, that must implement `FormInterface`.'); |
|
39
|
|
|
} |
|
40
|
|
|
|
|
41
|
|
|
/** |
|
42
|
|
|
* @inheritdoc |
|
43
|
|
|
*/ |
|
44
|
|
|
public function render() |
|
45
|
|
|
{ |
|
46
|
|
|
return self::renderStatic($this->arguments, $this->buildRenderChildrenClosure(), $this->renderingContext); |
|
|
|
|
|
|
47
|
|
|
} |
|
48
|
|
|
|
|
49
|
|
|
/** |
|
50
|
|
|
* @inheritdoc |
|
51
|
|
|
*/ |
|
52
|
|
|
public static function renderStatic(array $arguments, \Closure $renderChildrenClosure, RenderingContextInterface $renderingContext) |
|
53
|
|
|
{ |
|
54
|
|
|
/** @var FormViewHelperService $formService */ |
|
55
|
|
|
$formService = Core::instantiate(FormViewHelperService::class); |
|
56
|
|
|
|
|
57
|
|
|
$formObject = null; |
|
58
|
|
|
$form = $arguments['form']; |
|
59
|
|
|
|
|
60
|
|
|
if (null !== $form) { |
|
61
|
|
|
if (false === is_object($form)) { |
|
62
|
|
|
throw InvalidOptionValueException::formIdentifierViewHelperWrongFormValueType($form); |
|
63
|
|
|
} |
|
64
|
|
|
|
|
65
|
|
|
if (false === $form instanceof FormInterface) { |
|
66
|
|
|
throw InvalidOptionValueException::formIdentifierViewHelperWrongFormValueObjectType($form); |
|
67
|
|
|
} |
|
68
|
|
|
|
|
69
|
|
|
$formObject = FormObjectFactory::get()->getInstanceWithFormInstance($form); |
|
70
|
|
|
} elseif ($formService->formContextExists()) { |
|
71
|
|
|
$formObject = $formService->getFormObject(); |
|
72
|
|
|
} |
|
73
|
|
|
|
|
74
|
|
|
if (null === $formObject) { |
|
75
|
|
|
throw ContextNotFoundException::formIdentifierViewHelperFormContextNotFound(); |
|
76
|
|
|
} |
|
77
|
|
|
|
|
78
|
|
|
return $formObject->getFormHash(); |
|
79
|
|
|
} |
|
80
|
|
|
} |
|
81
|
|
|
|
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.