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\Service\ViewHelper; |
15
|
|
|
|
16
|
|
|
use Romm\Formz\Configuration\Form\Field\Field; |
17
|
|
|
use Romm\Formz\Core\Core; |
18
|
|
|
use TYPO3\CMS\Core\SingletonInterface; |
19
|
|
|
use TYPO3\CMS\Fluid\View\StandaloneView; |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* Contains methods to help view helpers to manipulate data concerning the |
23
|
|
|
* current field. |
24
|
|
|
*/ |
25
|
|
|
class FieldViewHelperService implements SingletonInterface |
26
|
|
|
{ |
27
|
|
|
/** |
28
|
|
|
* @var Field |
29
|
|
|
*/ |
30
|
|
|
protected $currentField; |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* @var array |
34
|
|
|
*/ |
35
|
|
|
protected $fieldOptions = []; |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* @var StandaloneView |
39
|
|
|
*/ |
40
|
|
|
protected $view; |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* Reset every state that can be used by this service. |
44
|
|
|
*/ |
45
|
|
|
public function resetState() |
46
|
|
|
{ |
47
|
|
|
$this->currentField = null; |
48
|
|
|
$this->fieldOptions = []; |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* Checks that the `FieldViewHelper` has been called. If not, an exception |
53
|
|
|
* is thrown. |
54
|
|
|
* |
55
|
|
|
* @return bool |
56
|
|
|
*/ |
57
|
|
|
public function fieldContextExists() |
58
|
|
|
{ |
59
|
|
|
return $this->currentField instanceof Field; |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
/** |
63
|
|
|
* Returns the current field which was defined by the `FieldViewHelper`. |
64
|
|
|
* |
65
|
|
|
* Returns null if no current field was found. |
66
|
|
|
* |
67
|
|
|
* @return Field|null |
68
|
|
|
*/ |
69
|
|
|
public function getCurrentField() |
70
|
|
|
{ |
71
|
|
|
return $this->currentField; |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
/** |
75
|
|
|
* @param Field $field |
76
|
|
|
*/ |
77
|
|
|
public function setCurrentField(Field $field) |
78
|
|
|
{ |
79
|
|
|
$this->currentField = $field; |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
/** |
83
|
|
|
* @param string $name |
84
|
|
|
* @param mixed $value |
85
|
|
|
*/ |
86
|
|
|
public function setFieldOption($name, $value) |
87
|
|
|
{ |
88
|
|
|
$this->fieldOptions[$name] = $value; |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
/** |
92
|
|
|
* @return array |
93
|
|
|
*/ |
94
|
|
|
public function getFieldOptions() |
95
|
|
|
{ |
96
|
|
|
return $this->fieldOptions; |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
/** |
100
|
|
|
* @return StandaloneView |
101
|
|
|
*/ |
102
|
|
|
public function getView() |
103
|
|
|
{ |
104
|
|
|
if (null === $this->view) { |
105
|
|
|
$this->view = Core::instantiate(StandaloneView::class); |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
return $this->view; |
109
|
|
|
} |
110
|
|
|
} |
111
|
|
|
|