‪TYPO3CMS  10.4
AbstractRenderable.php
Go to the documentation of this file.
1 <?php
2 
3 declare(strict_types=1);
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 
18 /*
19  * Inspired by and partially taken from the Neos.Form package (www.neos.io)
20  */
21 
23 
32 
42 {
43 
51  protected ‪$type;
52 
58  protected ‪$identifier;
59 
65  protected ‪$parentRenderable;
66 
72  protected ‪$label = '';
73 
79  protected ‪$renderingOptions = [];
80 
86  protected ‪$index = 0;
87 
93  protected ‪$templateName = '';
94 
100  protected ‪$variants = [];
101 
107  public function ‪getType(): string
108  {
110  }
111 
117  public function ‪getIdentifier(): string
118  {
120  }
121 
127  public function ‪setIdentifier(string ‪$identifier)
128  {
129  $this->identifier = ‪$identifier;
130  }
131 
140  public function ‪setOptions(array $options, bool $resetValidators = false)
141  {
142  if (isset($options['label'])) {
143  $this->‪setLabel($options['label']);
144  }
145 
146  if (isset($options['defaultValue'])) {
147  $this->‪setDefaultValue($options['defaultValue']);
148  }
149 
150  if (isset($options['properties'])) {
151  foreach ($options['properties'] as $key => $value) {
152  $this->‪setProperty($key, $value);
153  }
154  }
155 
156  if (isset($options['renderingOptions'])) {
157  foreach ($options['renderingOptions'] as $key => $value) {
158  $this->‪setRenderingOption($key, $value);
159  }
160  }
161 
162  if (isset($options['validators'])) {
163  $runtimeCache = GeneralUtility::makeInstance(CacheManager::class)->getCache('runtime');
164  $configurationHashes = $runtimeCache->get('formAbstractRenderableConfigurationHashes') ?: [];
165 
166  if ($resetValidators) {
167  $processingRule = $this->‪getRootForm()->‪getProcessingRule($this->‪getIdentifier());
168  foreach ($this->‪getValidators() as ‪$validator) {
169  $processingRule->‪removeValidator(‪$validator);
170  }
171  $configurationHashes = [];
172  }
173 
174  foreach ($options['validators'] as $validatorConfiguration) {
175  $configurationHash = md5(
176  spl_object_hash($this) .
177  json_encode($validatorConfiguration)
178  );
179  if (in_array($configurationHash, $configurationHashes)) {
180  continue;
181  }
182  $this->‪createValidator($validatorConfiguration['identifier'], $validatorConfiguration['options'] ?? []);
183  $configurationHashes[] = $configurationHash;
184  $runtimeCache->set('formAbstractRenderableConfigurationHashes', $configurationHashes);
185  }
186  }
187 
188  if (isset($options['variants'])) {
189  foreach ($options['variants'] as $variantConfiguration) {
190  $this->‪createVariant($variantConfiguration);
191  }
192  }
193 
195  $options,
196  ['label', 'defaultValue', 'properties', 'renderingOptions', 'validators', 'formEditor', 'variants']
197  );
198  }
199 
208  public function ‪createValidator(string $validatorIdentifier, array $options = [])
209  {
210  $validatorsDefinition = $this->‪getRootForm()->‪getValidatorsDefinition();
211  if (isset($validatorsDefinition[$validatorIdentifier]) && is_array($validatorsDefinition[$validatorIdentifier]) && isset($validatorsDefinition[$validatorIdentifier]['implementationClassName'])) {
212  $implementationClassName = $validatorsDefinition[$validatorIdentifier]['implementationClassName'];
213  $defaultOptions = $validatorsDefinition[$validatorIdentifier]['options'] ?? [];
214 
215  ‪ArrayUtility::mergeRecursiveWithOverrule($defaultOptions, $options);
216 
218  ‪$validator = GeneralUtility::makeInstance(ObjectManager::class)
219  ->get($implementationClassName, $defaultOptions);
220  $this->‪addValidator($validator);
221  return ‪$validator;
222  }
223  throw new ‪ValidatorPresetNotFoundException('The validator preset identified by "' . $validatorIdentifier . '" could not be found, or the implementationClassName was not specified.', 1328710202);
224  }
225 
232  {
233  $formDefinition = $this->‪getRootForm();
234  $formDefinition->getProcessingRule($this->‪getIdentifier())->addValidator($validator);
235  }
236 
243  public function ‪getValidators(): \SplObjectStorage
244  {
245  $formDefinition = $this->‪getRootForm();
246  return $formDefinition->getProcessingRule($this->‪getIdentifier())->getValidators();
247  }
248 
254  public function ‪setDataType(string $dataType)
255  {
256  $formDefinition = $this->‪getRootForm();
257  $formDefinition->getProcessingRule($this->‪getIdentifier())->setDataType($dataType);
258  }
259 
265  public function ‪getRendererClassName(): string
266  {
268  }
269 
275  public function ‪getRenderingOptions(): array
276  {
278  }
279 
287  public function ‪setRenderingOption(string $key, $value)
288  {
289  if (is_array($value) && isset($this->renderingOptions[$key]) && is_array($this->renderingOptions[$key])) {
290  ‪ArrayUtility::mergeRecursiveWithOverrule($this->renderingOptions[$key], $value);
291  $this->renderingOptions[$key] = ‪ArrayUtility::removeNullValuesRecursive($this->renderingOptions[$key]);
292  } elseif ($value === null) {
293  unset($this->renderingOptions[$key]);
294  } else {
295  $this->renderingOptions[$key] = $value;
296  }
297  }
298 
304  public function ‪getParentRenderable()
305  {
307  }
308 
315  {
316  $this->parentRenderable = ‪$parentRenderable;
318  }
319 
326  public function ‪getRootForm(): ‪FormDefinition
327  {
328  $rootRenderable = ‪$this->parentRenderable;
329  while ($rootRenderable !== null && !($rootRenderable instanceof ‪FormDefinition)) {
330  $rootRenderable = $rootRenderable->‪getParentRenderable();
331  }
332  if ($rootRenderable === null) {
333  throw new ‪FormDefinitionConsistencyException(sprintf('The form element "%s" is not attached to a parent form.', $this->identifier), 1326803398);
334  }
335 
336  return $rootRenderable;
337  }
338 
344  public function ‪registerInFormIfPossible()
345  {
346  try {
347  $rootForm = $this->‪getRootForm();
348  $rootForm->registerRenderable($this);
349  } catch (‪FormDefinitionConsistencyException $exception) {
350  }
351  }
352 
358  public function ‪onRemoveFromParentRenderable()
359  {
360  foreach (‪$GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['ext/form']['beforeRemoveFromParentRenderable'] ?? [] as $className) {
361  $hookObj = GeneralUtility::makeInstance($className);
362  if (method_exists($hookObj, 'beforeRemoveFromParentRenderable')) {
363  $hookObj->beforeRemoveFromParentRenderable(
364  $this
365  );
366  }
367  }
368 
369  try {
370  $rootForm = $this->‪getRootForm();
371  $rootForm->unregisterRenderable($this);
372  } catch (FormDefinitionConsistencyException $exception) {
373  }
374  $this->parentRenderable = null;
375  }
376 
383  public function ‪getIndex(): int
384  {
385  return ‪$this->index;
386  }
387 
394  public function ‪setIndex(int ‪$index)
395  {
396  $this->index = ‪$index;
397  }
398 
404  public function ‪getLabel(): string
405  {
407  }
408 
414  public function ‪setLabel(string ‪$label)
415  {
416  $this->label = ‪$label;
417  }
418 
419  public function ‪setDefaultValue($defaultValue)
420  {
421  // todo: this method must either be abstract and implemented in sub classes or get a proper method body.
422  }
423 
424  public function ‪setProperty(string $key, $value)
425  {
426  // todo: this method must either be abstract and implemented in sub classes or get a proper method body.
427  }
428 
434  public function ‪getTemplateName(): string
435  {
436  return empty($this->renderingOptions['templateName'])
437  ? $this->type
438  : $this->renderingOptions['templateName'];
439  }
440 
446  public function ‪isEnabled(): bool
447  {
448  return !isset($this->renderingOptions['enabled']) || (bool)$this->renderingOptions['enabled'] === true;
449  }
450 
456  public function ‪getVariants(): array
457  {
458  return ‪$this->variants;
459  }
460 
465  public function ‪createVariant(array $options): ‪RenderableVariantInterface
466  {
467  ‪$identifier = $options['identifier'] ?? '';
468  unset($options['identifier']);
469 
470  $variant = GeneralUtility::makeInstance(ObjectManager::class)
471  ->get(RenderableVariant::class, ‪$identifier, $options, $this);
472 
473  $this->‪addVariant($variant);
474  return $variant;
475  }
476 
482  public function ‪addVariant(‪RenderableVariantInterface $variant)
483  {
484  $this->variants[$variant->‪getIdentifier()] = $variant;
485  }
486 
493  public function ‪applyVariant(‪RenderableVariantInterface $variant)
494  {
495  $variant->‪apply();
496  }
497 }
‪TYPO3\CMS\Form\Domain\Model\Renderable\AbstractRenderable\$index
‪int $index
Definition: AbstractRenderable.php:80
‪TYPO3\CMS\Form\Domain\Model\Exception\FormDefinitionConsistencyException
Definition: FormDefinitionConsistencyException.php:27
‪TYPO3\CMS\Form\Domain\Model\Renderable\AbstractRenderable\$label
‪string $label
Definition: AbstractRenderable.php:68
‪TYPO3\CMS\Form\Domain\Model\Renderable\AbstractRenderable\setRenderingOption
‪mixed setRenderingOption(string $key, $value)
Definition: AbstractRenderable.php:279
‪TYPO3\CMS\Form\Domain\Model\Renderable\AbstractRenderable\isEnabled
‪bool isEnabled()
Definition: AbstractRenderable.php:438
‪TYPO3\CMS\Form\Domain\Model\Renderable\AbstractRenderable\onRemoveFromParentRenderable
‪onRemoveFromParentRenderable()
Definition: AbstractRenderable.php:350
‪TYPO3\CMS\Form\Domain\Model\Renderable\AbstractRenderable\setLabel
‪setLabel(string $label)
Definition: AbstractRenderable.php:406
‪TYPO3\CMS\Form\Domain\Model\Renderable
Definition: AbstractCompositeRenderable.php:22
‪TYPO3\CMS\Form\Domain\Model\Renderable\AbstractRenderable\setOptions
‪setOptions(array $options, bool $resetValidators=false)
Definition: AbstractRenderable.php:132
‪TYPO3\CMS\Form\Domain\Model\Renderable\AbstractRenderable\getParentRenderable
‪CompositeRenderableInterface null getParentRenderable()
Definition: AbstractRenderable.php:296
‪TYPO3\CMS\Form\Domain\Model\Renderable\RenderableVariantInterface\apply
‪apply()
‪TYPO3\CMS\Form\Mvc\ProcessingRule\removeValidator
‪removeValidator(ValidatorInterface $validator)
Definition: ProcessingRule.php:151
‪TYPO3\CMS\Form\Domain\Model\Renderable\AbstractRenderable\$parentRenderable
‪CompositeRenderableInterface $parentRenderable
Definition: AbstractRenderable.php:62
‪TYPO3\CMS\Core\Utility\ArrayUtility\mergeRecursiveWithOverrule
‪static mergeRecursiveWithOverrule(array &$original, array $overrule, $addKeys=true, $includeEmptyValues=true, $enableUnsetFeature=true)
Definition: ArrayUtility.php:654
‪TYPO3\CMS\Form\Domain\Model\Renderable\AbstractRenderable
Definition: AbstractRenderable.php:42
‪TYPO3\CMS\Form\Domain\Model\Renderable\AbstractRenderable\setProperty
‪setProperty(string $key, $value)
Definition: AbstractRenderable.php:416
‪TYPO3\CMS\Form\Domain\Model\Renderable\AbstractRenderable\$type
‪string $type
Definition: AbstractRenderable.php:50
‪TYPO3\CMS\Form\Domain\Model\Renderable\AbstractRenderable\$renderingOptions
‪array $renderingOptions
Definition: AbstractRenderable.php:74
‪TYPO3\CMS\Form\Domain\Model\Renderable\AbstractRenderable\addVariant
‪addVariant(RenderableVariantInterface $variant)
Definition: AbstractRenderable.php:474
‪TYPO3\CMS\Form\Domain\Model\Renderable\AbstractRenderable\getTemplateName
‪string getTemplateName()
Definition: AbstractRenderable.php:426
‪TYPO3\CMS\Form\Domain\Model\Renderable\AbstractRenderable\getIndex
‪int getIndex()
Definition: AbstractRenderable.php:375
‪TYPO3\CMS\Form\Domain\Model\Renderable\AbstractRenderable\getRootForm
‪FormDefinition getRootForm()
Definition: AbstractRenderable.php:318
‪TYPO3\CMS\Form\Domain\Model\Renderable\RenderableInterface
Definition: RenderableInterface.php:32
‪TYPO3\CMS\Form\Domain\Model\Renderable\AbstractRenderable\getIdentifier
‪string getIdentifier()
Definition: AbstractRenderable.php:109
‪TYPO3\CMS\Form\Domain\Model\Renderable\AbstractRenderable\getVariants
‪RenderableVariantInterface[] getVariants()
Definition: AbstractRenderable.php:448
‪TYPO3\CMS\Form\Domain\Model\Renderable\AbstractRenderable\setDefaultValue
‪setDefaultValue($defaultValue)
Definition: AbstractRenderable.php:411
‪$validator
‪if(isset($args['d'])) $validator
Definition: validateRstFiles.php:218
‪TYPO3\CMS\Form\Domain\Model\Renderable\AbstractRenderable\$templateName
‪string $templateName
Definition: AbstractRenderable.php:86
‪TYPO3\CMS\Form\Domain\Model\Renderable\AbstractRenderable\$variants
‪array $variants
Definition: AbstractRenderable.php:92
‪TYPO3\CMS\Form\Domain\Model\Renderable\AbstractRenderable\createValidator
‪mixed createValidator(string $validatorIdentifier, array $options=[])
Definition: AbstractRenderable.php:200
‪TYPO3\CMS\Form\Domain\Model\FormDefinition\getProcessingRule
‪ProcessingRule getProcessingRule(string $propertyPath)
Definition: FormDefinition.php:655
‪TYPO3\CMS\Form\Domain\Model\Renderable\AbstractRenderable\getValidators
‪SplObjectStorage getValidators()
Definition: AbstractRenderable.php:235
‪TYPO3\CMS\Core\Cache\CacheManager
Definition: CacheManager.php:35
‪TYPO3\CMS\Form\Domain\Model\Renderable\AbstractRenderable\setIdentifier
‪setIdentifier(string $identifier)
Definition: AbstractRenderable.php:119
‪TYPO3\CMS\Form\Domain\Model\Renderable\AbstractRenderable\getLabel
‪string getLabel()
Definition: AbstractRenderable.php:396
‪TYPO3\CMS\Form\Domain\Model\Renderable\RenderableInterface\getParentRenderable
‪CompositeRenderableInterface null getParentRenderable()
‪TYPO3\CMS\Form\Domain\Model\FormDefinition\getValidatorsDefinition
‪array getValidatorsDefinition()
Definition: FormDefinition.php:687
‪TYPO3\CMS\Form\Domain\Model\Renderable\RenderableVariantInterface\getIdentifier
‪string getIdentifier()
‪TYPO3\CMS\Form\Domain\Model\FormDefinition
Definition: FormDefinition.php:223
‪TYPO3\CMS\Form\Domain\Model\Renderable\AbstractRenderable\getType
‪string getType()
Definition: AbstractRenderable.php:99
‪TYPO3\CMS\Form\Domain\Model\Renderable\AbstractRenderable\registerInFormIfPossible
‪registerInFormIfPossible()
Definition: AbstractRenderable.php:336
‪TYPO3\CMS\Form\Domain\Model\Renderable\AbstractRenderable\setIndex
‪setIndex(int $index)
Definition: AbstractRenderable.php:386
‪TYPO3\CMS\Form\Domain\Model\Renderable\VariableRenderableInterface
Definition: VariableRenderableInterface.php:29
‪TYPO3\CMS\Form\Domain\Model\FormDefinition\getRendererClassName
‪string getRendererClassName()
Definition: FormDefinition.php:727
‪TYPO3\CMS\Core\Utility\ArrayUtility
Definition: ArrayUtility.php:24
‪TYPO3\CMS\Form\Domain\Model\Renderable\AbstractRenderable\getRendererClassName
‪string getRendererClassName()
Definition: AbstractRenderable.php:257
‪$GLOBALS
‪$GLOBALS['TYPO3_CONF_VARS']['EXTCONF']['adminpanel']['modules']
Definition: ext_localconf.php:5
‪TYPO3\CMS\Form\Domain\Model\Renderable\AbstractRenderable\getRenderingOptions
‪array getRenderingOptions()
Definition: AbstractRenderable.php:267
‪TYPO3\CMS\Core\Utility\ArrayUtility\assertAllArrayKeysAreValid
‪static assertAllArrayKeysAreValid(array $arrayToTest, array $allowedArrayKeys)
Definition: ArrayUtility.php:33
‪TYPO3\CMS\Form\Domain\Model\Renderable\AbstractRenderable\createVariant
‪RenderableVariantInterface createVariant(array $options)
Definition: AbstractRenderable.php:457
‪TYPO3\CMS\Core\Utility\ArrayUtility\removeNullValuesRecursive
‪static array removeNullValuesRecursive(array $array)
Definition: ArrayUtility.php:233
‪TYPO3\CMS\Form\Domain\Model\Renderable\AbstractRenderable\$identifier
‪string $identifier
Definition: AbstractRenderable.php:56
‪TYPO3\CMS\Form\Domain\Model\Renderable\CompositeRenderableInterface
Definition: CompositeRenderableInterface.php:32
‪TYPO3\CMS\Form\Domain\Model\Renderable\AbstractRenderable\addValidator
‪addValidator(ValidatorInterface $validator)
Definition: AbstractRenderable.php:223
‪TYPO3\CMS\Form\Domain\Model\Renderable\AbstractRenderable\setDataType
‪setDataType(string $dataType)
Definition: AbstractRenderable.php:246
‪TYPO3\CMS\Core\Utility\GeneralUtility
Definition: GeneralUtility.php:46
‪TYPO3\CMS\Extbase\Validation\Validator\ValidatorInterface
Definition: ValidatorInterface.php:22
‪TYPO3\CMS\Extbase\Object\ObjectManager
Definition: ObjectManager.php:28
‪TYPO3\CMS\Form\Domain\Model\Renderable\AbstractRenderable\setParentRenderable
‪setParentRenderable(CompositeRenderableInterface $parentRenderable)
Definition: AbstractRenderable.php:306
‪TYPO3\CMS\Form\Domain\Model\Exception\ValidatorPresetNotFoundException
Definition: ValidatorPresetNotFoundException.php:27
‪TYPO3\CMS\Form\Domain\Model\Renderable\AbstractRenderable\applyVariant
‪applyVariant(RenderableVariantInterface $variant)
Definition: AbstractRenderable.php:485
‪TYPO3\CMS\Form\Domain\Model\Renderable\RenderableVariantInterface
Definition: RenderableVariantInterface.php:28