[FOLLOWUP][TASK] EXT:form - change signal slots to hooks
[Packages/TYPO3.CMS.git] / typo3 / sysext / form / Classes / Domain / Model / FormElements / AbstractFormElement.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\Form\Domain\Exception\IdentifierNotValidException;
23 use TYPO3\CMS\Form\Domain\Model\Renderable\AbstractRenderable;
24 use TYPO3\CMS\Form\Domain\Runtime\FormRuntime;
25
26 /**
27 * A base form element, which is the starting point for creating custom (PHP-based)
28 * Form Elements.
29 *
30 * A *FormElement* is a part of a *Page*, which in turn is part of a FormDefinition.
31 * See {@link FormDefinition} for an in-depth explanation.
32 *
33 * Subclassing this class is a good starting-point for implementing custom PHP-based
34 * Form Elements.
35 *
36 * Most of the functionality and API is implemented in {@link \TYPO3\CMS\Form\Domain\Model\Renderable\AbstractRenderable}, so
37 * make sure to check out this class as well.
38 *
39 * Still, it is quite rare that you need to subclass this class; often
40 * you can just use the {@link \TYPO3\CMS\Form\Domain\Model\FormElements\GenericFormElement} and replace some templates.
41 *
42 * Scope: frontend
43 * **This class is meant to be sub classed by developers.**
44 */
45 abstract class AbstractFormElement extends AbstractRenderable implements FormElementInterface
46 {
47
48 /**
49 * @var array
50 */
51 protected $properties = [];
52
53 /**
54 * Constructor. Needs this FormElement's identifier and the FormElement type
55 *
56 * @param string $identifier The FormElement's identifier
57 * @param string $type The Form Element Type
58 * @throws IdentifierNotValidException
59 * @api
60 */
61 public function __construct(string $identifier, string $type)
62 {
63 if (!is_string($identifier) || strlen($identifier) === 0) {
64 throw new IdentifierNotValidException('The given identifier was not a string or the string was empty.', 1477082502);
65 }
66 $this->identifier = $identifier;
67 $this->type = $type;
68 }
69
70 /**
71 * Override this method in your custom FormElements if needed
72 *
73 * @return void
74 * @api
75 */
76 public function initializeFormElement()
77 {
78 if (
79 isset($GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['ext/form']['initializeFormElement'])
80 && is_array($GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['ext/form']['initializeFormElement'])
81 ) {
82 foreach ($GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['ext/form']['initializeFormElement'] as $className) {
83 $hookObj = GeneralUtility::makeInstance($className);
84 if (method_exists($hookObj, 'initializeFormElement')) {
85 $hookObj->initializeFormElement(
86 $this
87 );
88 }
89 }
90 }
91 }
92
93 /**
94 * Get the global unique identifier of the element
95 *
96 * @return string
97 * @api
98 */
99 public function getUniqueIdentifier(): string
100 {
101 $formDefinition = $this->getRootForm();
102 $uniqueIdentifier = sprintf('%s-%s', $formDefinition->getIdentifier(), $this->identifier);
103 $uniqueIdentifier = preg_replace('/[^a-zA-Z0-9_-]/', '_', $uniqueIdentifier);
104 return lcfirst($uniqueIdentifier);
105 }
106
107 /**
108 * Get the default value of the element
109 *
110 * @return mixed
111 * @api
112 */
113 public function getDefaultValue()
114 {
115 $formDefinition = $this->getRootForm();
116 return $formDefinition->getElementDefaultValueByIdentifier($this->identifier);
117 }
118
119 /**
120 * Set the default value of the element
121 *
122 * @param mixed $defaultValue
123 * @return void
124 * @api
125 */
126 public function setDefaultValue($defaultValue)
127 {
128 $formDefinition = $this->getRootForm();
129 $formDefinition->addElementDefaultValue($this->identifier, $defaultValue);
130 }
131
132 /**
133 * Check if the element is required
134 *
135 * @return bool
136 * @api
137 */
138 public function isRequired(): bool
139 {
140 foreach ($this->getValidators() as $validator) {
141 if ($validator instanceof NotEmptyValidator) {
142 return true;
143 }
144 }
145 return false;
146 }
147
148 /**
149 * Set a property of the element
150 *
151 * @param string $key
152 * @param mixed $value
153 * @return void
154 * @api
155 */
156 public function setProperty(string $key, $value)
157 {
158 $this->properties[$key] = $value;
159 }
160
161 /**
162 * Get all properties
163 *
164 * @return array
165 * @api
166 */
167 public function getProperties(): array
168 {
169 return $this->properties;
170 }
171
172 /**
173 * Override this method in your custom FormElements if needed
174 *
175 * @param FormRuntime $formRuntime
176 * @param mixed $elementValue
177 * @param array $requestArguments submitted raw request values
178 * @return void
179 * @api
180 * @deprecated since TYPO3 v8, will be removed in TYPO3 v9
181 */
182 public function onSubmit(FormRuntime $formRuntime, &$elementValue, array $requestArguments = [])
183 {
184 GeneralUtility::logDeprecatedFunction();
185 }
186 }