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\Exceptions\ContextNotFoundException; |
18
|
|
|
use Romm\Formz\ViewHelpers\FieldViewHelper; |
19
|
|
|
use TYPO3\CMS\Core\SingletonInterface; |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* Contains methods to help view helpers to manipulate data concerning the |
23
|
|
|
* current field. |
24
|
|
|
*/ |
25
|
|
|
class FieldService implements SingletonInterface |
26
|
|
|
{ |
27
|
|
|
/** |
28
|
|
|
* @var Field |
29
|
|
|
*/ |
30
|
|
|
protected $currentField; |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* @var array |
34
|
|
|
*/ |
35
|
|
|
protected $fieldOptions = []; |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* Reset every state that can be used by this service. |
39
|
|
|
*/ |
40
|
|
|
public function resetState() |
41
|
|
|
{ |
42
|
|
|
$this->currentField = null; |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* Checks that the `FieldViewHelper` has been called. If not, an exception |
47
|
|
|
* is thrown. |
48
|
|
|
* |
49
|
|
|
* @return bool |
50
|
|
|
*/ |
51
|
|
|
public function fieldContextExists() |
52
|
|
|
{ |
53
|
|
|
return $this->currentField instanceof Field; |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
/** |
57
|
|
|
* Returns the current field which was defined by the `FieldViewHelper`. |
58
|
|
|
* |
59
|
|
|
* Returns null if no current field was found. |
60
|
|
|
* |
61
|
|
|
* @return Field|null |
62
|
|
|
*/ |
63
|
|
|
public function getCurrentField() |
64
|
|
|
{ |
65
|
|
|
return $this->currentField; |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
/** |
69
|
|
|
* @param Field $field |
70
|
|
|
*/ |
71
|
|
|
public function setCurrentField(Field $field) |
72
|
|
|
{ |
73
|
|
|
$this->currentField = $field; |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
/** |
77
|
|
|
* @param string $name |
78
|
|
|
* @param mixed $value |
79
|
|
|
*/ |
80
|
|
|
public function setFieldOption($name, $value) |
81
|
|
|
{ |
82
|
|
|
$this->fieldOptions[$name] = $value; |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
/** |
86
|
|
|
* @return array |
87
|
|
|
*/ |
88
|
|
|
public function getFieldOptions() |
89
|
|
|
{ |
90
|
|
|
return $this->fieldOptions; |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
/** |
94
|
|
|
* @return $this |
95
|
|
|
*/ |
96
|
|
|
public function resetFieldOptions() |
97
|
|
|
{ |
98
|
|
|
$this->fieldOptions = []; |
99
|
|
|
|
100
|
|
|
return $this; |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
/** |
104
|
|
|
* Unset the current field. |
105
|
|
|
* |
106
|
|
|
* @return $this |
107
|
|
|
*/ |
108
|
|
|
public function removeCurrentField() |
109
|
|
|
{ |
110
|
|
|
$this->currentField = null; |
111
|
|
|
|
112
|
|
|
return $this; |
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
/** |
116
|
|
|
* Checks that the `FieldViewHelper` has been called. If not, an exception |
117
|
|
|
* is thrown. |
118
|
|
|
* |
119
|
|
|
* @throws \Exception |
120
|
|
|
*/ |
121
|
|
|
public function checkIsInsideFieldViewHelper() |
122
|
|
|
{ |
123
|
|
|
if (false === $this->fieldContextExists()) { |
124
|
|
|
throw new ContextNotFoundException( |
125
|
|
|
'The view helper "' . get_called_class() . '" must be used inside the view helper "' . FieldViewHelper::class . '".', |
126
|
|
|
1465243085 |
127
|
|
|
); |
128
|
|
|
} |
129
|
|
|
} |
130
|
|
|
} |
131
|
|
|
|