TYPO3CMS  8
 All Classes Namespaces Files Functions Variables Pages
AbstractRenderable.php
Go to the documentation of this file.
1 <?php
2 declare(strict_types=1);
3 namespace TYPO3\CMS\Form\Domain\Model\Renderable;
4 
5 /*
6  * This file is part of the TYPO3 CMS project.
7  *
8  * It is free software; you can redistribute it and/or modify it under
9  * the terms of the GNU General Public License, either version 2
10  * of the License, or any later version.
11  *
12  * For the full copyright and license information, please read the
13  * LICENSE.txt file that was distributed with this source code.
14  *
15  * The TYPO3 project - inspiring people to share!
16  */
17 
26 
35 abstract class AbstractRenderable implements RenderableInterface
36 {
37 
45  protected $type;
46 
52  protected $identifier;
53 
59  protected $parentRenderable;
60 
66  protected $label = '';
67 
73  protected $renderingOptions = [];
74 
84  protected $rendererClassName = null;
85 
91  protected $index = 0;
92 
99  public function getType(): string
100  {
101  return $this->type;
102  }
103 
110  public function getIdentifier(): string
111  {
112  return $this->identifier;
113  }
114 
124  public function setOptions(array $options)
125  {
126  if (isset($options['label'])) {
127  $this->setLabel($options['label']);
128  }
129 
130  if (isset($options['defaultValue'])) {
131  $this->setDefaultValue($options['defaultValue']);
132  }
133 
134  if (isset($options['properties'])) {
135  foreach ($options['properties'] as $key => $value) {
136  $this->setProperty($key, $value);
137  }
138  }
139 
140  if (isset($options['rendererClassName'])) {
141  $this->setRendererClassName($options['rendererClassName']);
142  }
143 
144  if (isset($options['renderingOptions'])) {
145  foreach ($options['renderingOptions'] as $key => $value) {
146  if (is_array($value)) {
147  $currentValue = isset($this->getRenderingOptions()[$key]) ? $this->getRenderingOptions()[$key] : [];
148  ArrayUtility::mergeRecursiveWithOverrule($currentValue, $value);
149  $this->setRenderingOption($key, $currentValue);
150  } else {
151  $this->setRenderingOption($key, $value);
152  }
153  }
154  }
155 
156  if (isset($options['validators'])) {
157  foreach ($options['validators'] as $validatorConfiguration) {
158  $this->createValidator($validatorConfiguration['identifier'], isset($validatorConfiguration['options']) ? $validatorConfiguration['options'] : []);
159  }
160  }
161 
163  $options,
164  ['label', 'defaultValue', 'properties', 'rendererClassName', 'renderingOptions', 'validators', 'formEditor']
165  );
166  }
167 
177  public function createValidator(string $validatorIdentifier, array $options = [])
178  {
179  $validatorsDefinition = $this->getRootForm()->getValidatorsDefinition();
180  if (isset($validatorsDefinition[$validatorIdentifier]) && is_array($validatorsDefinition[$validatorIdentifier]) && isset($validatorsDefinition[$validatorIdentifier]['implementationClassName'])) {
181  $implementationClassName = $validatorsDefinition[$validatorIdentifier]['implementationClassName'];
182  $defaultOptions = isset($validatorsDefinition[$validatorIdentifier]['options']) ? $validatorsDefinition[$validatorIdentifier]['options'] : [];
183 
184  ArrayUtility::mergeRecursiveWithOverrule($defaultOptions, $options);
185 
186  $validator = GeneralUtility::makeInstance(ObjectManager::class)
187  ->get($implementationClassName, $defaultOptions);
188  $this->addValidator($validator);
189  return $validator;
190  } else {
191  throw new ValidatorPresetNotFoundException('The validator preset identified by "' . $validatorIdentifier . '" could not be found, or the implementationClassName was not specified.', 1328710202);
192  }
193  }
194 
202  public function addValidator(ValidatorInterface $validator)
203  {
204  $formDefinition = $this->getRootForm();
205  $formDefinition->getProcessingRule($this->getIdentifier())->addValidator($validator);
206  }
207 
214  public function getValidators(): \SplObjectStorage
215  {
216  $formDefinition = $this->getRootForm();
217  return $formDefinition->getProcessingRule($this->getIdentifier())->getValidators();
218  }
219 
227  public function setDataType(string $dataType)
228  {
229  $formDefinition = $this->getRootForm();
230  $formDefinition->getProcessingRule($this->getIdentifier())->setDataType($dataType);
231  }
232 
240  public function setRendererClassName(string $rendererClassName)
241  {
242  $this->rendererClassName = $rendererClassName;
243  }
244 
251  public function getRendererClassName()
252  {
254  }
255 
262  public function getRenderingOptions(): array
263  {
265  }
266 
275  public function setRenderingOption(string $key, $value)
276  {
277  $this->renderingOptions[$key] = $value;
278  }
279 
287  public function getParentRenderable()
288  {
290  }
291 
300  {
301  $this->parentRenderable = $parentRenderable;
302  $this->registerInFormIfPossible();
303  }
304 
312  public function getRootForm(): FormDefinition
313  {
314  $rootRenderable = $this->parentRenderable;
315  while ($rootRenderable !== null && !($rootRenderable instanceof FormDefinition)) {
316  $rootRenderable = $rootRenderable->getParentRenderable();
317  }
318  if ($rootRenderable === null) {
319  throw new FormDefinitionConsistencyException(sprintf('The form element "%s" is not attached to a parent form.', $this->identifier), 1326803398);
320  }
321 
322  return $rootRenderable;
323  }
324 
331  public function registerInFormIfPossible()
332  {
333  try {
334  $rootForm = $this->getRootForm();
335  $rootForm->registerRenderable($this);
336  } catch (FormDefinitionConsistencyException $exception) {
337  }
338  }
339 
347  {
348  try {
349  $rootForm = $this->getRootForm();
350  $rootForm->unregisterRenderable($this);
351  } catch (FormDefinitionConsistencyException $exception) {
352  }
353  $this->parentRenderable = null;
354  }
355 
362  public function getIndex(): int
363  {
364  return $this->index;
365  }
366 
374  public function setIndex(int $index)
375  {
376  $this->index = $index;
377  }
378 
385  public function getLabel(): string
386  {
387  return $this->label;
388  }
389 
397  public function setLabel(string $label)
398  {
399  $this->label = $label;
400  }
401 
409  public function beforeRendering(FormRuntime $formRuntime)
410  {
411  }
412 
422  public function onBuildingFinished()
423  {
424  }
425 }
createValidator(string $validatorIdentifier, array $options=[])
static assertAllArrayKeysAreValid(array $arrayToTest, array $allowedArrayKeys)
setParentRenderable(CompositeRenderableInterface $parentRenderable)
static mergeRecursiveWithOverrule(array &$original, array $overrule, $addKeys=true, $includeEmptyValues=true, $enableUnsetFeature=true)
static makeInstance($className,...$constructorArguments)