-
Notifications
You must be signed in to change notification settings - Fork 638
/
AbstractFormElement.php
167 lines (155 loc) · 4.87 KB
/
AbstractFormElement.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
<?php
declare(strict_types = 1);
namespace TYPO3\CMS\Form\Domain\Model\FormElements;
/*
* This file is part of the TYPO3 CMS project.
*
* It originated from the Neos.Form package (www.neos.io)
*
* It is free software; you can redistribute it and/or modify it under
* the terms of the GNU General Public License, either version 2
* of the License, or any later version.
*
* For the full copyright and license information, please read the
* LICENSE.txt file that was distributed with this source code.
*
* The TYPO3 project - inspiring people to share!
*/
use TYPO3\CMS\Core\Utility\ArrayUtility;
use TYPO3\CMS\Core\Utility\GeneralUtility;
use TYPO3\CMS\Extbase\Validation\Validator\NotEmptyValidator;
use TYPO3\CMS\Form\Domain\Exception\IdentifierNotValidException;
use TYPO3\CMS\Form\Domain\Model\Renderable\AbstractRenderable;
/**
* A base form element, which is the starting point for creating custom (PHP-based)
* Form Elements.
*
* A *FormElement* is a part of a *Page*, which in turn is part of a FormDefinition.
* See {@link FormDefinition} for an in-depth explanation.
*
* Subclassing this class is a good starting-point for implementing custom PHP-based
* Form Elements.
*
* Most of the functionality and API is implemented in {@link \TYPO3\CMS\Form\Domain\Model\Renderable\AbstractRenderable}, so
* make sure to check out this class as well.
*
* Still, it is quite rare that you need to subclass this class; often
* you can just use the {@link \TYPO3\CMS\Form\Domain\Model\FormElements\GenericFormElement} and replace some templates.
*
* Scope: frontend
* **This class is meant to be sub classed by developers.**
*/
abstract class AbstractFormElement extends AbstractRenderable implements FormElementInterface
{
/**
* @var array
*/
protected $properties = [];
/**
* Constructor. Needs this FormElement's identifier and the FormElement type
*
* @param string $identifier The FormElement's identifier
* @param string $type The Form Element Type
* @throws IdentifierNotValidException
* @api
*/
public function __construct(string $identifier, string $type)
{
if (!is_string($identifier) || strlen($identifier) === 0) {
throw new IdentifierNotValidException('The given identifier was not a string or the string was empty.', 1477082502);
}
$this->identifier = $identifier;
$this->type = $type;
}
/**
* Override this method in your custom FormElements if needed
*
* @api
*/
public function initializeFormElement()
{
foreach ($GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['ext/form']['initializeFormElement'] ?? [] as $className) {
$hookObj = GeneralUtility::makeInstance($className);
if (method_exists($hookObj, 'initializeFormElement')) {
$hookObj->initializeFormElement(
$this
);
}
}
}
/**
* Get the global unique identifier of the element
*
* @return string
* @api
*/
public function getUniqueIdentifier(): string
{
$formDefinition = $this->getRootForm();
$uniqueIdentifier = sprintf('%s-%s', $formDefinition->getIdentifier(), $this->identifier);
$uniqueIdentifier = preg_replace('/[^a-zA-Z0-9_-]/', '_', $uniqueIdentifier);
return lcfirst($uniqueIdentifier);
}
/**
* Get the default value of the element
*
* @return mixed
* @api
*/
public function getDefaultValue()
{
$formDefinition = $this->getRootForm();
return $formDefinition->getElementDefaultValueByIdentifier($this->identifier);
}
/**
* Set the default value of the element
*
* @param mixed $defaultValue
* @api
*/
public function setDefaultValue($defaultValue)
{
$formDefinition = $this->getRootForm();
$formDefinition->addElementDefaultValue($this->identifier, $defaultValue);
}
/**
* Check if the element is required
*
* @return bool
* @api
*/
public function isRequired(): bool
{
foreach ($this->getValidators() as $validator) {
if ($validator instanceof NotEmptyValidator) {
return true;
}
}
return false;
}
/**
* Set a property of the element
*
* @param string $key
* @param mixed $value
* @api
*/
public function setProperty(string $key, $value)
{
if (is_array($value) && isset($this->properties[$key]) && is_array($this->properties[$key])) {
ArrayUtility::mergeRecursiveWithOverrule($this->properties[$key], $value);
} else {
$this->properties[$key] = $value;
}
}
/**
* Get all properties
*
* @return array
* @api
*/
public function getProperties(): array
{
return $this->properties;
}
}