‪TYPO3CMS  9.5
AbstractRenderable.php
Go to the documentation of this file.
1 <?php
2 declare(strict_types = 1);
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 
28 
38 {
39 
47  protected ‪$type;
48 
54  protected ‪$identifier;
55 
61  protected ‪$parentRenderable;
62 
68  protected ‪$label = '';
69 
75  protected ‪$renderingOptions = [];
76 
82  protected ‪$index = 0;
83 
89  protected ‪$templateName = '';
90 
96  protected ‪$variants = [];
97 
103  public function ‪getType(): string
104  {
106  }
107 
113  public function ‪getIdentifier(): string
114  {
116  }
117 
123  public function ‪setIdentifier(string ‪$identifier)
124  {
125  $this->identifier = ‪$identifier;
126  }
127 
136  public function ‪setOptions(array $options, bool $resetValidators = false)
137  {
138  if (isset($options['label'])) {
139  $this->‪setLabel($options['label']);
140  }
141 
142  if (isset($options['defaultValue'])) {
143  $this->setDefaultValue($options['defaultValue']);
144  }
145 
146  if (isset($options['properties'])) {
147  foreach ($options['properties'] as $key => $value) {
148  $this->setProperty($key, $value);
149  }
150  }
151 
152  if (isset($options['renderingOptions'])) {
153  foreach ($options['renderingOptions'] as $key => $value) {
154  $this->‪setRenderingOption($key, $value);
155  }
156  }
157 
158  if (isset($options['validators'])) {
159  $runtimeCache = GeneralUtility::makeInstance(CacheManager::class)->getCache('cache_runtime');
160  $configurationHashes = $runtimeCache->get('formAbstractRenderableConfigurationHashes') ?: [];
161 
162  if ($resetValidators) {
163  $processingRule = $this->‪getRootForm()->‪getProcessingRule($this->‪getIdentifier());
164  foreach ($this->‪getValidators() as ‪$validator) {
165  $processingRule->‪removeValidator(‪$validator);
166  }
167  $configurationHashes = [];
168  }
169 
170  foreach ($options['validators'] as $validatorConfiguration) {
171  $configurationHash = md5(
172  spl_object_hash($this) .
173  json_encode($validatorConfiguration)
174  );
175  if (in_array($configurationHash, $configurationHashes)) {
176  continue;
177  }
178  $this->‪createValidator($validatorConfiguration['identifier'], $validatorConfiguration['options'] ?? []);
179  $configurationHashes[] = $configurationHash;
180  $runtimeCache->set('formAbstractRenderableConfigurationHashes', $configurationHashes);
181  }
182  }
183 
184  if (isset($options['variants'])) {
185  foreach ($options['variants'] as $variantConfiguration) {
186  $this->‪createVariant($variantConfiguration);
187  }
188  }
189 
191  $options,
192  ['label', 'defaultValue', 'properties', 'renderingOptions', 'validators', 'formEditor', 'variants']
193  );
194  }
195 
204  public function ‪createValidator(string $validatorIdentifier, array $options = [])
205  {
206  $validatorsDefinition = $this->‪getRootForm()->‪getValidatorsDefinition();
207  if (isset($validatorsDefinition[$validatorIdentifier]) && is_array($validatorsDefinition[$validatorIdentifier]) && isset($validatorsDefinition[$validatorIdentifier]['implementationClassName'])) {
208  $implementationClassName = $validatorsDefinition[$validatorIdentifier]['implementationClassName'];
209  $defaultOptions = $validatorsDefinition[$validatorIdentifier]['options'] ?? [];
210 
211  ‪ArrayUtility::mergeRecursiveWithOverrule($defaultOptions, $options);
212 
213  ‪$validator = GeneralUtility::makeInstance(ObjectManager::class)
214  ->get($implementationClassName, $defaultOptions);
215  $this->‪addValidator($validator);
216  return ‪$validator;
217  }
218  throw new ‪ValidatorPresetNotFoundException('The validator preset identified by "' . $validatorIdentifier . '" could not be found, or the implementationClassName was not specified.', 1328710202);
219  }
220 
227  {
228  $formDefinition = $this->‪getRootForm();
229  $formDefinition->getProcessingRule($this->‪getIdentifier())->addValidator($validator);
230  }
231 
238  public function ‪getValidators(): \SplObjectStorage
239  {
240  $formDefinition = $this->‪getRootForm();
241  return $formDefinition->getProcessingRule($this->‪getIdentifier())->getValidators();
242  }
243 
249  public function ‪setDataType(string $dataType)
250  {
251  $formDefinition = $this->‪getRootForm();
252  $formDefinition->getProcessingRule($this->‪getIdentifier())->setDataType($dataType);
253  }
254 
260  public function ‪getRendererClassName(): string
261  {
263  }
264 
270  public function ‪getRenderingOptions(): array
271  {
273  }
274 
282  public function ‪setRenderingOption(string $key, $value)
283  {
284  if (is_array($value) && isset($this->renderingOptions[$key]) && is_array($this->renderingOptions[$key])) {
285  ‪ArrayUtility::mergeRecursiveWithOverrule($this->renderingOptions[$key], $value);
286  $this->renderingOptions[$key] = ‪ArrayUtility::removeNullValuesRecursive($this->renderingOptions[$key]);
287  } elseif ($value === null) {
288  unset($this->renderingOptions[$key]);
289  } else {
290  $this->renderingOptions[$key] = $value;
291  }
292  }
293 
299  public function ‪getParentRenderable()
300  {
302  }
303 
310  {
311  $this->parentRenderable = ‪$parentRenderable;
313  }
314 
321  public function ‪getRootForm(): ‪FormDefinition
322  {
323  $rootRenderable = ‪$this->parentRenderable;
324  while ($rootRenderable !== null && !($rootRenderable instanceof ‪FormDefinition)) {
325  $rootRenderable = $rootRenderable->‪getParentRenderable();
326  }
327  if ($rootRenderable === null) {
328  throw new ‪FormDefinitionConsistencyException(sprintf('The form element "%s" is not attached to a parent form.', $this->identifier), 1326803398);
329  }
330 
331  return $rootRenderable;
332  }
333 
339  public function ‪registerInFormIfPossible()
340  {
341  try {
342  $rootForm = $this->‪getRootForm();
343  $rootForm->registerRenderable($this);
344  } catch (‪FormDefinitionConsistencyException $exception) {
345  }
346  }
347 
353  public function ‪onRemoveFromParentRenderable()
354  {
355  foreach (‪$GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['ext/form']['beforeRemoveFromParentRenderable'] ?? [] as $className) {
356  $hookObj = GeneralUtility::makeInstance($className);
357  if (method_exists($hookObj, 'beforeRemoveFromParentRenderable')) {
358  $hookObj->beforeRemoveFromParentRenderable(
359  $this
360  );
361  }
362  }
363 
364  try {
365  $rootForm = $this->‪getRootForm();
366  $rootForm->unregisterRenderable($this);
367  } catch (FormDefinitionConsistencyException $exception) {
368  }
369  $this->parentRenderable = null;
370  }
371 
378  public function ‪getIndex(): int
379  {
380  return ‪$this->index;
381  }
382 
389  public function ‪setIndex(int ‪$index)
390  {
391  $this->index = ‪$index;
392  }
393 
399  public function ‪getLabel(): string
400  {
402  }
403 
409  public function ‪setLabel(string ‪$label)
410  {
411  $this->label = ‪$label;
412  }
413 
419  public function ‪getTemplateName(): string
420  {
421  return empty($this->renderingOptions['templateName'])
422  ? $this->type
423  : $this->renderingOptions['templateName'];
424  }
425 
431  public function ‪isEnabled(): bool
432  {
433  return !isset($this->renderingOptions['enabled']) || (bool)$this->renderingOptions['enabled'] === true;
434  }
435 
441  public function ‪getVariants(): array
442  {
443  return ‪$this->variants;
444  }
445 
450  public function ‪createVariant(array $options): ‪RenderableVariantInterface
451  {
452  ‪$identifier = $options['identifier'] ?? '';
453  unset($options['identifier']);
454 
455  $variant = GeneralUtility::makeInstance(ObjectManager::class)
456  ->get(RenderableVariant::class, ‪$identifier, $options, $this);
457 
458  $this->‪addVariant($variant);
459  return $variant;
460  }
461 
467  public function ‪addVariant(‪RenderableVariantInterface $variant)
468  {
469  $this->variants[$variant->‪getIdentifier()] = $variant;
470  }
471 
478  public function ‪applyVariant(‪RenderableVariantInterface $variant)
479  {
480  $variant->‪apply();
481  }
482 }
‪TYPO3\CMS\Form\Domain\Model\Renderable\AbstractRenderable\$index
‪int $index
Definition: AbstractRenderable.php:76
‪TYPO3\CMS\Form\Domain\Model\Exception\FormDefinitionConsistencyException
Definition: FormDefinitionConsistencyException.php:25
‪TYPO3\CMS\Form\Domain\Model\Renderable\AbstractRenderable\$label
‪string $label
Definition: AbstractRenderable.php:64
‪TYPO3\CMS\Form\Domain\Model\Renderable\AbstractRenderable\setRenderingOption
‪mixed setRenderingOption(string $key, $value)
Definition: AbstractRenderable.php:274
‪TYPO3\CMS\Form\Domain\Model\Renderable\AbstractRenderable\isEnabled
‪bool isEnabled()
Definition: AbstractRenderable.php:423
‪TYPO3\CMS\Form\Domain\Model\Renderable\AbstractRenderable\onRemoveFromParentRenderable
‪onRemoveFromParentRenderable()
Definition: AbstractRenderable.php:345
‪TYPO3\CMS\Form\Domain\Model\Renderable\AbstractRenderable\setLabel
‪setLabel(string $label)
Definition: AbstractRenderable.php:401
‪TYPO3\CMS\Form\Domain\Model\Renderable
Definition: AbstractCompositeRenderable.php:3
‪TYPO3\CMS\Form\Domain\Model\Renderable\AbstractRenderable\setOptions
‪setOptions(array $options, bool $resetValidators=false)
Definition: AbstractRenderable.php:128
‪TYPO3\CMS\Form\Domain\Model\Renderable\AbstractRenderable\getParentRenderable
‪CompositeRenderableInterface null getParentRenderable()
Definition: AbstractRenderable.php:291
‪TYPO3\CMS\Form\Domain\Model\Renderable\RenderableVariantInterface\apply
‪apply()
‪TYPO3\CMS\Form\Mvc\ProcessingRule\removeValidator
‪removeValidator(ValidatorInterface $validator)
Definition: ProcessingRule.php:147
‪TYPO3\CMS\Form\Domain\Model\Renderable\AbstractRenderable\$parentRenderable
‪CompositeRenderableInterface $parentRenderable
Definition: AbstractRenderable.php:58
‪TYPO3\CMS\Core\Utility\ArrayUtility\mergeRecursiveWithOverrule
‪static mergeRecursiveWithOverrule(array &$original, array $overrule, $addKeys=true, $includeEmptyValues=true, $enableUnsetFeature=true)
Definition: ArrayUtility.php:614
‪TYPO3\CMS\Form\Domain\Model\Renderable\AbstractRenderable
Definition: AbstractRenderable.php:38
‪TYPO3\CMS\Form\Domain\Model\Renderable\AbstractRenderable\$type
‪string $type
Definition: AbstractRenderable.php:46
‪TYPO3\CMS\Form\Domain\Model\Renderable\AbstractRenderable\$renderingOptions
‪array $renderingOptions
Definition: AbstractRenderable.php:70
‪TYPO3\CMS\Form\Domain\Model\Renderable\AbstractRenderable\addVariant
‪addVariant(RenderableVariantInterface $variant)
Definition: AbstractRenderable.php:459
‪TYPO3\CMS\Form\Domain\Model\Renderable\AbstractRenderable\getTemplateName
‪string getTemplateName()
Definition: AbstractRenderable.php:411
‪TYPO3\CMS\Form\Domain\Model\Renderable\AbstractRenderable\getIndex
‪int getIndex()
Definition: AbstractRenderable.php:370
‪TYPO3\CMS\Form\Domain\Model\Renderable\AbstractRenderable\getRootForm
‪FormDefinition getRootForm()
Definition: AbstractRenderable.php:313
‪TYPO3\CMS\Form\Domain\Model\Renderable\RenderableInterface
Definition: RenderableInterface.php:28
‪TYPO3\CMS\Form\Domain\Model\Renderable\AbstractRenderable\getIdentifier
‪string getIdentifier()
Definition: AbstractRenderable.php:105
‪TYPO3\CMS\Form\Domain\Model\Renderable\AbstractRenderable\getVariants
‪RenderableVariantInterface[] getVariants()
Definition: AbstractRenderable.php:433
‪$validator
‪if(isset($args['d'])) $validator
Definition: validateRstFiles.php:218
‪TYPO3\CMS\Form\Domain\Model\Renderable\AbstractRenderable\$templateName
‪string $templateName
Definition: AbstractRenderable.php:82
‪TYPO3\CMS\Form\Domain\Model\Renderable\AbstractRenderable\$variants
‪array $variants
Definition: AbstractRenderable.php:88
‪TYPO3\CMS\Form\Domain\Model\Renderable\AbstractRenderable\createValidator
‪mixed createValidator(string $validatorIdentifier, array $options=[])
Definition: AbstractRenderable.php:196
‪TYPO3\CMS\Form\Domain\Model\FormDefinition\getProcessingRule
‪ProcessingRule getProcessingRule(string $propertyPath)
Definition: FormDefinition.php:638
‪TYPO3\CMS\Form\Domain\Model\Renderable\AbstractRenderable\getValidators
‪SplObjectStorage getValidators()
Definition: AbstractRenderable.php:230
‪TYPO3\CMS\Core\Cache\CacheManager
Definition: CacheManager.php:34
‪TYPO3\CMS\Form\Domain\Model\Renderable\AbstractRenderable\setIdentifier
‪setIdentifier(string $identifier)
Definition: AbstractRenderable.php:115
‪TYPO3\CMS\Form\Domain\Model\Renderable\AbstractRenderable\getLabel
‪string getLabel()
Definition: AbstractRenderable.php:391
‪TYPO3\CMS\Form\Domain\Model\Renderable\RenderableInterface\getParentRenderable
‪CompositeRenderableInterface null getParentRenderable()
‪TYPO3\CMS\Form\Domain\Model\FormDefinition\getValidatorsDefinition
‪array getValidatorsDefinition()
Definition: FormDefinition.php:670
‪TYPO3\CMS\Form\Domain\Model\Renderable\RenderableVariantInterface\getIdentifier
‪string getIdentifier()
‪TYPO3\CMS\Form\Domain\Model\FormDefinition
Definition: FormDefinition.php:218
‪TYPO3\CMS\Form\Domain\Model\Renderable\AbstractRenderable\getType
‪string getType()
Definition: AbstractRenderable.php:95
‪TYPO3\CMS\Form\Domain\Model\Renderable\AbstractRenderable\registerInFormIfPossible
‪registerInFormIfPossible()
Definition: AbstractRenderable.php:331
‪TYPO3\CMS\Form\Domain\Model\Renderable\AbstractRenderable\setIndex
‪setIndex(int $index)
Definition: AbstractRenderable.php:381
‪TYPO3\CMS\Form\Domain\Model\Renderable\VariableRenderableInterface
Definition: VariableRenderableInterface.php:25
‪TYPO3\CMS\Form\Domain\Model\FormDefinition\getRendererClassName
‪string getRendererClassName()
Definition: FormDefinition.php:710
‪TYPO3\CMS\Core\Utility\ArrayUtility
Definition: ArrayUtility.php:23
‪TYPO3\CMS\Form\Domain\Model\Renderable\AbstractRenderable\getRendererClassName
‪string getRendererClassName()
Definition: AbstractRenderable.php:252
‪$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:262
‪TYPO3\CMS\Core\Utility\ArrayUtility\assertAllArrayKeysAreValid
‪static assertAllArrayKeysAreValid(array $arrayToTest, array $allowedArrayKeys)
Definition: ArrayUtility.php:32
‪TYPO3\CMS\Form\Domain\Model\Renderable\AbstractRenderable\createVariant
‪RenderableVariantInterface createVariant(array $options)
Definition: AbstractRenderable.php:442
‪TYPO3\CMS\Core\Utility\ArrayUtility\removeNullValuesRecursive
‪static array removeNullValuesRecursive(array $array)
Definition: ArrayUtility.php:232
‪TYPO3\CMS\Form\Domain\Model\Renderable\AbstractRenderable\$identifier
‪string $identifier
Definition: AbstractRenderable.php:52
‪TYPO3\CMS\Form\Domain\Model\Renderable\CompositeRenderableInterface
Definition: CompositeRenderableInterface.php:28
‪TYPO3\CMS\Form\Domain\Model\Renderable\AbstractRenderable\addValidator
‪addValidator(ValidatorInterface $validator)
Definition: AbstractRenderable.php:218
‪TYPO3\CMS\Form\Domain\Model\Renderable\AbstractRenderable\setDataType
‪setDataType(string $dataType)
Definition: AbstractRenderable.php:241
‪TYPO3\CMS\Core\Utility\GeneralUtility
Definition: GeneralUtility.php:45
‪TYPO3\CMS\Extbase\Validation\Validator\ValidatorInterface
Definition: ValidatorInterface.php:21
‪TYPO3\CMS\Extbase\Object\ObjectManager
Definition: ObjectManager.php:25
‪TYPO3\CMS\Form\Domain\Model\Renderable\AbstractRenderable\setParentRenderable
‪setParentRenderable(CompositeRenderableInterface $parentRenderable)
Definition: AbstractRenderable.php:301
‪TYPO3\CMS\Form\Domain\Model\Exception\ValidatorPresetNotFoundException
Definition: ValidatorPresetNotFoundException.php:25
‪TYPO3\CMS\Form\Domain\Model\Renderable\AbstractRenderable\applyVariant
‪applyVariant(RenderableVariantInterface $variant)
Definition: AbstractRenderable.php:470
‪TYPO3\CMS\Form\Domain\Model\Renderable\RenderableVariantInterface
Definition: RenderableVariantInterface.php:26