‪TYPO3CMS  10.4
FormDefinition.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 
41 use ‪TYPO3\CMS\Form\Exception as FormException;
43 
223 {
224 
228  protected ‪$objectManager;
229 
235  protected ‪$finishers = [];
236 
242  protected ‪$processingRules = [];
243 
250  protected ‪$elementsByIdentifier = [];
251 
257  protected ‪$elementDefaultValues = [];
258 
264  protected ‪$rendererClassName = '';
265 
269  protected ‪$typeDefinitions;
270 
275 
279  protected ‪$finishersDefinition;
280 
285 
291  protected ‪$persistenceIdentifier;
292 
298  {
299  $this->objectManager = ‪$objectManager;
300  }
301 
311  public function ‪__construct(
312  string ‪$identifier,
313  array $prototypeConfiguration = [],
314  string ‪$type = 'Form',
315  string ‪$persistenceIdentifier = null
316  ) {
317  $this->typeDefinitions = $prototypeConfiguration['formElementsDefinition'] ?? [];
318  $this->validatorsDefinition = $prototypeConfiguration['validatorsDefinition'] ?? [];
319  $this->finishersDefinition = $prototypeConfiguration['finishersDefinition'] ?? [];
320  $this->conditionContextDefinition = $prototypeConfiguration['conditionContextDefinition'] ?? [];
321 
322  if (!is_string(‪$identifier) || strlen(‪$identifier) === 0) {
323  throw new ‪IdentifierNotValidException('The given identifier was not a string or the string was empty.', 1477082503);
324  }
325 
326  $this->identifier = ‪$identifier;
327  $this->type = ‪$type;
328  $this->persistenceIdentifier = ‪$persistenceIdentifier;
329 
330  if ($prototypeConfiguration !== []) {
332  }
333  }
334 
341  protected function ‪initializeFromFormDefaults()
342  {
343  if (!isset($this->typeDefinitions[$this->type])) {
344  throw new TypeDefinitionNotFoundException(sprintf('Type "%s" not found. Probably some configuration is missing.', $this->type), 1474905835);
345  }
346  $typeDefinition = $this->typeDefinitions[‪$this->type];
347  $this->‪setOptions($typeDefinition);
348  }
349 
359  public function ‪setOptions(array $options, bool $resetFinishers = false)
360  {
361  if (isset($options['rendererClassName'])) {
362  $this->‪setRendererClassName($options['rendererClassName']);
363  }
364  if (isset($options['label'])) {
365  $this->‪setLabel($options['label']);
366  }
367  if (isset($options['renderingOptions'])) {
368  foreach ($options['renderingOptions'] as $key => $value) {
369  $this->‪setRenderingOption($key, $value);
370  }
371  }
372  if (isset($options['finishers'])) {
373  if ($resetFinishers) {
374  $this->finishers = [];
375  }
376  foreach ($options['finishers'] as $finisherConfiguration) {
377  $this->‪createFinisher($finisherConfiguration['identifier'], $finisherConfiguration['options'] ?? []);
378  }
379  }
380 
381  if (isset($options['variants'])) {
382  foreach ($options['variants'] as $variantConfiguration) {
383  $this->‪createVariant($variantConfiguration);
384  }
385  }
386 
388  $options,
389  ['rendererClassName', 'renderingOptions', 'finishers', 'formEditor', 'label', 'variants']
390  );
391  }
392 
406  public function ‪createPage(string ‪$identifier, string $typeName = 'Page'): Page
407  {
408  if (!isset($this->typeDefinitions[$typeName])) {
409  throw new TypeDefinitionNotFoundException(sprintf('Type "%s" not found. Probably some configuration is missing.', $typeName), 1474905953);
410  }
411 
412  $typeDefinition = $this->typeDefinitions[$typeName];
413 
414  if (!isset($typeDefinition['implementationClassName'])) {
415  throw new TypeDefinitionNotFoundException(sprintf('The "implementationClassName" was not set in type definition "%s".', $typeName), 1477083126);
416  }
417  $implementationClassName = $typeDefinition['implementationClassName'];
419  $page = $this->objectManager->get($implementationClassName, ‪$identifier, $typeName);
420 
421  if (isset($typeDefinition['label'])) {
422  $page->setLabel($typeDefinition['label']);
423  }
424 
425  if (isset($typeDefinition['renderingOptions'])) {
426  foreach ($typeDefinition['renderingOptions'] as $key => $value) {
427  $page->setRenderingOption($key, $value);
428  }
429  }
430 
432  $typeDefinition,
433  ['implementationClassName', 'label', 'renderingOptions', 'formEditor']
434  );
435 
436  $this->‪addPage($page);
437  return $page;
438  }
439 
449  public function ‪addPage(‪Page $page)
450  {
451  $this->‪addRenderable($page);
452  }
453 
459  public function ‪getPages(): array
460  {
461  return ‪$this->renderables;
462  }
463 
470  public function ‪hasPageWithIndex(int ‪$index): bool
471  {
472  return isset($this->renderables[‪$index]);
473  }
474 
484  public function ‪getPageByIndex(int ‪$index)
485  {
486  if (!$this->‪hasPageWithIndex($index)) {
487  throw new FormException(sprintf('There is no page with an index of %d', ‪$index), 1329233627);
488  }
489  return $this->renderables[‪$index];
490  }
491 
497  public function ‪addFinisher(‪FinisherInterface $finisher)
498  {
499  $this->finishers[] = $finisher;
500  }
501 
508  public function ‪createFinisher(string $finisherIdentifier, array $options = []): ‪FinisherInterface
509  {
510  if (isset($this->finishersDefinition[$finisherIdentifier]) && is_array($this->finishersDefinition[$finisherIdentifier]) && isset($this->finishersDefinition[$finisherIdentifier]['implementationClassName'])) {
511  $implementationClassName = $this->finishersDefinition[$finisherIdentifier]['implementationClassName'];
512  $defaultOptions = $this->finishersDefinition[$finisherIdentifier]['options'] ?? [];
513  ‪ArrayUtility::mergeRecursiveWithOverrule($defaultOptions, $options);
514 
516  $finisher = $this->objectManager->get($implementationClassName, $finisherIdentifier);
517  $finisher->setOptions($defaultOptions);
518  $this->‪addFinisher($finisher);
519  return $finisher;
520  }
521  throw new ‪FinisherPresetNotFoundException('The finisher preset identified by "' . $finisherIdentifier . '" could not be found, or the implementationClassName was not specified.', 1328709784);
522  }
523 
529  public function ‪getFinishers(): array
530  {
531  return ‪$this->finishers;
532  }
533 
541  public function ‪registerRenderable(‪RenderableInterface $renderable)
542  {
543  if ($renderable instanceof ‪FormElementInterface) {
544  if (isset($this->elementsByIdentifier[$renderable->‪getIdentifier()])) {
545  throw new ‪DuplicateFormElementException(sprintf('A form element with identifier "%s" is already part of the form.', $renderable->‪getIdentifier()), 1325663761);
546  }
547  $this->elementsByIdentifier[$renderable->‪getIdentifier()] = $renderable;
548  }
549  }
550 
557  public function ‪unregisterRenderable(RenderableInterface $renderable)
558  {
559  if ($renderable instanceof ‪FormElementInterface) {
560  unset($this->elementsByIdentifier[$renderable->getIdentifier()]);
561  }
562  }
563 
569  public function ‪getElements(): array
570  {
572  }
573 
582  public function ‪getElementByIdentifier(string $elementIdentifier)
583  {
584  return $this->elementsByIdentifier[$elementIdentifier] ?? null;
585  }
586 
594  public function ‪addElementDefaultValue(string $elementIdentifier, $defaultValue)
595  {
596  $this->elementDefaultValues = ‪ArrayUtility::setValueByPath(
597  $this->elementDefaultValues,
598  $elementIdentifier,
599  $defaultValue,
600  '.'
601  );
602  }
603 
612  public function ‪getElementDefaultValueByIdentifier(string $elementIdentifier)
613  {
614  return ‪ObjectAccess::getPropertyPath($this->elementDefaultValues, $elementIdentifier);
615  }
616 
623  public function ‪movePageBefore(‪Page $pageToMove, ‪Page $referencePage)
624  {
625  $this->‪moveRenderableBefore($pageToMove, $referencePage);
626  }
627 
634  public function ‪movePageAfter(‪Page $pageToMove, ‪Page $referencePage)
635  {
636  $this->‪moveRenderableAfter($pageToMove, $referencePage);
637  }
638 
644  public function ‪removePage(Page $pageToRemove)
645  {
646  $this->‪removeRenderable($pageToRemove);
647  }
648 
657  public function ‪bind(‪Request $request, ‪Response $response): ‪FormRuntime
658  {
659  return $this->objectManager->get(FormRuntime::class, $this, $request, $response);
660  }
661 
666  public function ‪getProcessingRule(string $propertyPath): ProcessingRule
667  {
668  if (!isset($this->processingRules[$propertyPath])) {
669  $this->processingRules[$propertyPath] = $this->objectManager->get(ProcessingRule::class);
670  }
671  return $this->processingRules[$propertyPath];
672  }
673 
680  public function ‪getProcessingRules(): array
681  {
683  }
684 
689  public function ‪getTypeDefinitions(): array
690  {
692  }
693 
698  public function ‪getValidatorsDefinition(): array
699  {
701  }
702 
707  public function ‪getConditionContextDefinition(): array
708  {
710  }
711 
718  public function ‪getPersistenceIdentifier(): string
719  {
721  }
722 
728  public function ‪setRendererClassName(string ‪$rendererClassName)
729  {
730  $this->rendererClassName = ‪$rendererClassName;
731  }
732 
738  public function ‪getRendererClassName(): string
739  {
741  }
742 }
‪TYPO3\CMS\Form\Domain\Model\Renderable\AbstractRenderable\$index
‪int $index
Definition: AbstractRenderable.php:80
‪TYPO3\CMS\Form\Domain\Model\Exception\FinisherPresetNotFoundException
Definition: FinisherPresetNotFoundException.php:27
‪TYPO3\CMS\Form\Domain\Model\FormDefinition\$finishersDefinition
‪array $finishersDefinition
Definition: FormDefinition.php:270
‪TYPO3\CMS\Form\Domain\Model\Exception\FormDefinitionConsistencyException
Definition: FormDefinitionConsistencyException.php:27
‪TYPO3\CMS\Form\Domain\Model\FormDefinition\createFinisher
‪FinisherInterface createFinisher(string $finisherIdentifier, array $options=[])
Definition: FormDefinition.php:497
‪TYPO3\CMS\Form\Domain\Runtime\FormRuntime
Definition: FormRuntime.php:103
‪TYPO3\CMS\Form\Domain\Exception\TypeDefinitionNotFoundException
Definition: TypeDefinitionNotFoundException.php:31
‪TYPO3\CMS\Form\Domain\Model\FormDefinition\addFinisher
‪addFinisher(FinisherInterface $finisher)
Definition: FormDefinition.php:486
‪TYPO3\CMS\Form\Domain\Model\FormDefinition\removePage
‪removePage(Page $pageToRemove)
Definition: FormDefinition.php:633
‪TYPO3\CMS\Form\Domain\Model\Renderable\AbstractCompositeRenderable
Definition: AbstractCompositeRenderable.php:34
‪TYPO3\CMS\Form\Domain\Model\Renderable\AbstractCompositeRenderable\moveRenderableAfter
‪moveRenderableAfter(RenderableInterface $renderableToMove, RenderableInterface $referenceRenderable)
Definition: AbstractCompositeRenderable.php:109
‪TYPO3\CMS\Form\Domain\Model\FormDefinition\$conditionContextDefinition
‪array $conditionContextDefinition
Definition: FormDefinition.php:274
‪TYPO3\CMS\Form\Domain\Model\Renderable\AbstractRenderable\setRenderingOption
‪mixed setRenderingOption(string $key, $value)
Definition: AbstractRenderable.php:279
‪TYPO3\CMS\Form\Domain\Finishers\FinisherInterface
Definition: FinisherInterface.php:31
‪TYPO3\CMS\Form\Domain\Model\FormDefinition\getElements
‪FormElementInterface[] getElements()
Definition: FormDefinition.php:558
‪TYPO3\CMS\Form\Domain\Model\FormDefinition\setOptions
‪setOptions(array $options, bool $resetFinishers=false)
Definition: FormDefinition.php:348
‪TYPO3\CMS\Form\Domain\Model\FormDefinition\$elementsByIdentifier
‪TYPO3 CMS Form Domain Model FormElements FormElementInterface[] $elementsByIdentifier
Definition: FormDefinition.php:246
‪TYPO3\CMS\Form\Domain\Model\FormDefinition\movePageAfter
‪movePageAfter(Page $pageToMove, Page $referencePage)
Definition: FormDefinition.php:623
‪TYPO3\CMS\Form\Domain\Model\Renderable\AbstractCompositeRenderable\removeRenderable
‪removeRenderable(RenderableInterface $renderableToRemove)
Definition: AbstractCompositeRenderable.php:163
‪TYPO3\CMS\Form\Domain\Model\Renderable\AbstractRenderable\setLabel
‪setLabel(string $label)
Definition: AbstractRenderable.php:406
‪TYPO3\CMS\Form\Domain\Exception\IdentifierNotValidException
Definition: IdentifierNotValidException.php:31
‪TYPO3\CMS\Form\Domain\Model\Renderable\AbstractCompositeRenderable\moveRenderableBefore
‪moveRenderableBefore(RenderableInterface $renderableToMove, RenderableInterface $referenceRenderable)
Definition: AbstractCompositeRenderable.php:73
‪TYPO3\CMS\Form\Domain\Model\FormDefinition\getFinishers
‪TYPO3 CMS Form Domain Finishers FinisherInterface[] getFinishers()
Definition: FormDefinition.php:518
‪TYPO3\CMS\Form\Domain\Model\FormDefinition\setRendererClassName
‪setRendererClassName(string $rendererClassName)
Definition: FormDefinition.php:717
‪TYPO3\CMS\Form\Domain\Model\FormDefinition\createPage
‪Page createPage(string $identifier, string $typeName='Page')
Definition: FormDefinition.php:395
‪TYPO3\CMS\Form\Domain\Model\FormDefinition\getPersistenceIdentifier
‪string getPersistenceIdentifier()
Definition: FormDefinition.php:707
‪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\FormDefinition\$rendererClassName
‪string $rendererClassName
Definition: FormDefinition.php:258
‪TYPO3\CMS\Form\Domain\Model\FormDefinition\getElementByIdentifier
‪FormElementInterface getElementByIdentifier(string $elementIdentifier)
Definition: FormDefinition.php:571
‪TYPO3\CMS\Form\Domain\Model\Renderable\AbstractRenderable\$type
‪string $type
Definition: AbstractRenderable.php:50
‪TYPO3\CMS\Form\Domain\Model\FormDefinition\hasPageWithIndex
‪bool hasPageWithIndex(int $index)
Definition: FormDefinition.php:459
‪TYPO3\CMS\Form\Domain\Model\FormDefinition\__construct
‪__construct(string $identifier, array $prototypeConfiguration=[], string $type='Form', string $persistenceIdentifier=null)
Definition: FormDefinition.php:300
‪TYPO3\CMS\Form\Domain\Model\Renderable\RootRenderableInterface\getIdentifier
‪string getIdentifier()
‪TYPO3\CMS\Extbase\Reflection\ObjectAccess
Definition: ObjectAccess.php:38
‪TYPO3\CMS\Extbase\Object\ObjectManagerInterface
Definition: ObjectManagerInterface.php:26
‪TYPO3\CMS\Form\Domain\Model\FormDefinition\getElementDefaultValueByIdentifier
‪mixed getElementDefaultValueByIdentifier(string $elementIdentifier)
Definition: FormDefinition.php:601
‪TYPO3\CMS\Form\Domain\Model\FormElements\Page
Definition: Page.php:44
‪TYPO3\CMS\Form\Domain\Model\Renderable\RenderableInterface
Definition: RenderableInterface.php:32
‪TYPO3\CMS\Form\Domain\Model\FormDefinition\bind
‪FormRuntime bind(Request $request, Response $response)
Definition: FormDefinition.php:646
‪TYPO3\CMS\Form\Domain\Model
‪TYPO3\CMS\Form\Exception
Definition: Exception.php:26
‪TYPO3\CMS\Form\Domain\Model\FormDefinition\unregisterRenderable
‪unregisterRenderable(RenderableInterface $renderable)
Definition: FormDefinition.php:546
‪TYPO3\CMS\Form\Domain\Model\FormElements\FormElementInterface
Definition: FormElementInterface.php:40
‪TYPO3\CMS\Extbase\Mvc\Web\Response
Definition: Response.php:26
‪TYPO3\CMS\Form\Domain\Model\FormDefinition\getConditionContextDefinition
‪array getConditionContextDefinition()
Definition: FormDefinition.php:696
‪TYPO3\CMS\Form\Domain\Model\FormDefinition\$validatorsDefinition
‪array $validatorsDefinition
Definition: FormDefinition.php:266
‪TYPO3\CMS\Form\Domain\Model\FormDefinition\$typeDefinitions
‪array $typeDefinitions
Definition: FormDefinition.php:262
‪TYPO3\CMS\Form\Domain\Model\FormDefinition\getProcessingRule
‪ProcessingRule getProcessingRule(string $propertyPath)
Definition: FormDefinition.php:655
‪TYPO3\CMS\Form\Domain\Model\FormDefinition\$finishers
‪TYPO3 CMS Form Domain Finishers FinisherInterface[] $finishers
Definition: FormDefinition.php:233
‪TYPO3\CMS\Form\Domain\Model\FormDefinition\addElementDefaultValue
‪addElementDefaultValue(string $elementIdentifier, $defaultValue)
Definition: FormDefinition.php:583
‪TYPO3\CMS\Form\Domain\Model\FormDefinition\$persistenceIdentifier
‪string $persistenceIdentifier
Definition: FormDefinition.php:280
‪TYPO3\CMS\Form\Domain\Model\Renderable\AbstractCompositeRenderable\$renderables
‪TYPO3 CMS Form Domain Model Renderable RenderableInterface[] $renderables
Definition: AbstractCompositeRenderable.php:40
‪TYPO3\CMS\Extbase\Mvc\Web\Request
Definition: Request.php:23
‪TYPO3\CMS\Form\Domain\Model\FormDefinition\getTypeDefinitions
‪array getTypeDefinitions()
Definition: FormDefinition.php:678
‪TYPO3\CMS\Form\Domain\Model\FormDefinition\getValidatorsDefinition
‪array getValidatorsDefinition()
Definition: FormDefinition.php:687
‪TYPO3\CMS\Core\Utility\ArrayUtility\setValueByPath
‪static array setValueByPath(array $array, $path, $value, $delimiter='/')
Definition: ArrayUtility.php:272
‪TYPO3\CMS\Form\Domain\Model\FormDefinition
Definition: FormDefinition.php:223
‪TYPO3\CMS\Form\Domain\Model\FormDefinition\getProcessingRules
‪TYPO3 CMS Form Mvc ProcessingRule[] getProcessingRules()
Definition: FormDefinition.php:669
‪TYPO3\CMS\Form\Domain\Model\Renderable\VariableRenderableInterface
Definition: VariableRenderableInterface.php:29
‪TYPO3\CMS\Form\Domain\Runtime\FormRuntime
Definition: FormSession.php:18
‪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\FormDefinition\addPage
‪addPage(Page $page)
Definition: FormDefinition.php:438
‪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\Extbase\Reflection\ObjectAccess\getPropertyPath
‪static mixed getPropertyPath($subject, string $propertyPath)
Definition: ObjectAccess.php:139
‪TYPO3\CMS\Form\Domain\Model\FormDefinition\getPages
‪array Page[] getPages()
Definition: FormDefinition.php:448
‪TYPO3\CMS\Form\Domain\Model\Renderable\AbstractRenderable\$identifier
‪string $identifier
Definition: AbstractRenderable.php:56
‪TYPO3\CMS\Form\Domain\Model\FormDefinition\$elementDefaultValues
‪array $elementDefaultValues
Definition: FormDefinition.php:252
‪TYPO3\CMS\Form\Domain\Model\FormDefinition\$objectManager
‪TYPO3 CMS Extbase Object ObjectManagerInterface $objectManager
Definition: FormDefinition.php:227
‪TYPO3\CMS\Form\Domain\Model\Renderable\AbstractCompositeRenderable\addRenderable
‪addRenderable(RenderableInterface $renderable)
Definition: AbstractCompositeRenderable.php:52
‪TYPO3\CMS\Form\Domain\Model\Exception\DuplicateFormElementException
Definition: DuplicateFormElementException.php:27
‪TYPO3\CMS\Form\Domain\Model\FormDefinition\movePageBefore
‪movePageBefore(Page $pageToMove, Page $referencePage)
Definition: FormDefinition.php:612
‪TYPO3\CMS\Form\Domain\Model\FormDefinition\getPageByIndex
‪Page getPageByIndex(int $index)
Definition: FormDefinition.php:473
‪TYPO3\CMS\Form\Domain\Model\FormDefinition\registerRenderable
‪registerRenderable(RenderableInterface $renderable)
Definition: FormDefinition.php:530
‪TYPO3\CMS\Form\Mvc\ProcessingRule
Definition: ProcessingRule.php:36
‪TYPO3\CMS\Form\Domain\Model\FormDefinition\injectObjectManager
‪injectObjectManager(ObjectManagerInterface $objectManager)
Definition: FormDefinition.php:286
‪TYPO3\CMS\Form\Domain\Model\FormDefinition\initializeFromFormDefaults
‪initializeFromFormDefaults()
Definition: FormDefinition.php:330
‪TYPO3\CMS\Form\Domain\Model\FormDefinition\$processingRules
‪TYPO3 CMS Form Mvc ProcessingRule[] $processingRules
Definition: FormDefinition.php:239