1
|
|
|
<?php |
2
|
|
|
/* |
3
|
|
|
* 2016 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\Service; |
15
|
|
|
|
16
|
|
|
use Romm\Formz\Configuration\Form\Field\Field; |
17
|
|
|
use Romm\Formz\ViewHelpers\FieldViewHelper; |
18
|
|
|
use Romm\Formz\ViewHelpers\FormViewHelper; |
19
|
|
|
use TYPO3\CMS\Core\SingletonInterface; |
20
|
|
|
use TYPO3\CMS\Core\Utility\GeneralUtility; |
21
|
|
|
use TYPO3\CMS\Fluid\Core\Rendering\RenderingContextInterface; |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* This class contains methods that help ViewHelpers to manipulate data and know |
25
|
|
|
* more things concerning the current form state. |
26
|
|
|
*/ |
27
|
|
|
class FormzViewHelperService implements SingletonInterface |
28
|
|
|
{ |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* @var FormzViewHelperService |
32
|
|
|
*/ |
33
|
|
|
protected static $instance; |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* @return FormzViewHelperService |
37
|
|
|
*/ |
38
|
|
|
public static function get() |
39
|
|
|
{ |
40
|
|
|
if (null === self::$instance) { |
41
|
|
|
self::$instance = GeneralUtility::makeInstance(self::class); |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
return self::$instance; |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* Returns `true` if the `FormViewHelper` context exists. |
49
|
|
|
* |
50
|
|
|
* @return bool |
51
|
|
|
*/ |
52
|
|
|
public function formContextExists() |
53
|
|
|
{ |
54
|
|
|
return null !== FormViewHelper::getVariable(FormViewHelper::FORM_VIEW_HELPER); |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* Returns `true` if the form was submitted by the user. |
59
|
|
|
* |
60
|
|
|
* @return bool |
61
|
|
|
*/ |
62
|
|
|
public function formWasSubmitted() |
63
|
|
|
{ |
64
|
|
|
return true === FormViewHelper::getVariable(FormViewHelper::FORM_WAS_SUBMITTED); |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
/** |
68
|
|
|
* Checks that the `FieldViewHelper` has been called. If not, an exception |
69
|
|
|
* is thrown. |
70
|
|
|
* |
71
|
|
|
* @param RenderingContextInterface $renderingContext |
72
|
|
|
* @return bool |
73
|
|
|
*/ |
74
|
|
|
public function fieldContextExists(RenderingContextInterface $renderingContext) |
75
|
|
|
{ |
76
|
|
|
return null !== $this->getCurrentField($renderingContext); |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
/** |
80
|
|
|
* Returns the current field which was defined by the `FieldViewHelper`. |
81
|
|
|
* |
82
|
|
|
* Returns null if no current field was found. |
83
|
|
|
* |
84
|
|
|
* @param RenderingContextInterface $renderingContext |
85
|
|
|
* @return Field|null |
86
|
|
|
*/ |
87
|
|
|
public function getCurrentField(RenderingContextInterface $renderingContext) |
88
|
|
|
{ |
89
|
|
|
$result = null; |
90
|
|
|
$viewHelperVariableContainer = $renderingContext->getViewHelperVariableContainer(); |
91
|
|
|
|
92
|
|
|
if (true === $viewHelperVariableContainer->exists(FieldViewHelper::class, FieldViewHelper::FIELD_INSTANCE)) { |
93
|
|
|
$fieldInstance = $viewHelperVariableContainer->get(FieldViewHelper::class, FieldViewHelper::FIELD_INSTANCE); |
94
|
|
|
|
95
|
|
|
if ($fieldInstance instanceof Field) { |
96
|
|
|
$result = $fieldInstance; |
97
|
|
|
} |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
return $result; |
101
|
|
|
} |
102
|
|
|
} |
103
|
|
|
|