[FOLLOWUP][TASK] EXT:form - change signal slots to hooks
[Packages/TYPO3.CMS.git] / typo3 / sysext / form / Classes / Domain / Model / FormElements / Section.php
1 <?php
2 declare(strict_types=1);
3 namespace TYPO3\CMS\Form\Domain\Model\FormElements;
4
5 /*
6 * This file is part of the TYPO3 CMS project.
7 *
8 * It originated from the Neos.Form package (www.neos.io)
9 *
10 * It is free software; you can redistribute it and/or modify it under
11 * the terms of the GNU General Public License, either version 2
12 * of the License, or any later version.
13 *
14 * For the full copyright and license information, please read the
15 * LICENSE.txt file that was distributed with this source code.
16 *
17 * The TYPO3 project - inspiring people to share!
18 */
19
20 use TYPO3\CMS\Core\Utility\GeneralUtility;
21 use TYPO3\CMS\Extbase\Validation\Validator\NotEmptyValidator;
22 use TYPO3\CMS\Extbase\Validation\Validator\ValidatorInterface;
23
24 /**
25 * A Section, being part of a bigger Page
26 *
27 * This class contains multiple FormElements ({@link FormElementInterface}).
28 *
29 * Please see {@link FormDefinition} for an in-depth explanation.
30 *
31 * Scope: frontend
32 * **This class is NOT meant to be sub classed by developers.**
33 */
34 class Section extends AbstractSection implements FormElementInterface
35 {
36
37 /**
38 * @var array
39 */
40 protected $properties = [];
41
42 /**
43 * Will be called as soon as the element is (tried to be) added to a form
44 * @see registerInFormIfPossible()
45 *
46 * @return void
47 * @internal
48 */
49 public function initializeFormElement()
50 {
51 if (
52 isset($GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['ext/form']['initializeFormElement'])
53 && is_array($GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['ext/form']['initializeFormElement'])
54 ) {
55 foreach ($GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['ext/form']['initializeFormElement'] as $className) {
56 $hookObj = GeneralUtility::makeInstance($className);
57 if (method_exists($hookObj, 'initializeFormElement')) {
58 $hookObj->initializeFormElement(
59 $this
60 );
61 }
62 }
63 }
64 }
65
66 /**
67 * Returns a unique identifier of this element.
68 * While element identifiers are only unique within one form,
69 * this includes the identifier of the form itself, making it "globally" unique
70 *
71 * @return string the "globally" unique identifier of this element
72 * @api
73 */
74 public function getUniqueIdentifier(): string
75 {
76 $formDefinition = $this->getRootForm();
77 return sprintf('%s-%s', $formDefinition->getIdentifier(), $this->identifier);
78 }
79
80 /**
81 * Get the default value with which the Form Element should be initialized
82 * during display.
83 * Note: This is currently not used for section elements
84 *
85 * @return mixed the default value for this Form Element
86 * @api
87 */
88 public function getDefaultValue()
89 {
90 return null;
91 }
92
93 /**
94 * Set the default value with which the Form Element should be initialized
95 * during display.
96 * Note: This is currently ignored for section elements
97 *
98 * @param mixed $defaultValue the default value for this Form Element
99 * @api
100 */
101 public function setDefaultValue($defaultValue)
102 {
103 }
104
105 /**
106 * Get all element-specific configuration properties
107 *
108 * @return array
109 * @api
110 */
111 public function getProperties(): array
112 {
113 return $this->properties;
114 }
115
116 /**
117 * Set an element-specific configuration property.
118 *
119 * @param string $key
120 * @param mixed $value
121 * @return void
122 * @api
123 */
124 public function setProperty(string $key, $value)
125 {
126 $this->properties[$key] = $value;
127 }
128
129 /**
130 * Set the rendering option $key to $value.
131 *
132 * @param string $key
133 * @param mixed $value
134 * @return mixed
135 * @api
136 */
137 public function setRenderingOption(string $key, $value)
138 {
139 $this->renderingOptions[$key] = $value;
140 }
141
142 /**
143 * Get all validators on the element
144 *
145 * @return \SplObjectStorage
146 * @internal
147 */
148 public function getValidators(): \SplObjectStorage
149 {
150 $formDefinition = $this->getRootForm();
151 return $formDefinition->getProcessingRule($this->getIdentifier())->getValidators();
152 }
153
154 /**
155 * Add a validator to the element
156 *
157 * @param ValidatorInterface $validator
158 * @return void
159 * @api
160 */
161 public function addValidator(ValidatorInterface $validator)
162 {
163 $formDefinition = $this->getRootForm();
164 $formDefinition->getProcessingRule($this->getIdentifier())->addValidator($validator);
165 }
166
167 /**
168 * Whether or not this element is required
169 *
170 * @return bool
171 * @api
172 */
173 public function isRequired(): bool
174 {
175 foreach ($this->getValidators() as $validator) {
176 if ($validator instanceof NotEmptyValidator) {
177 return true;
178 }
179 }
180 return false;
181 }
182 }