‪TYPO3CMS  9.5
FormDefinition.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 
36 use ‪TYPO3\CMS\Form\Exception as FormException;
38 
218 {
219 
223  protected ‪$objectManager;
224 
230  protected ‪$finishers = [];
231 
237  protected ‪$processingRules = [];
238 
245  protected ‪$elementsByIdentifier = [];
246 
252  protected ‪$elementDefaultValues = [];
253 
259  protected ‪$rendererClassName = '';
260 
264  protected ‪$typeDefinitions;
265 
270 
274  protected ‪$finishersDefinition;
275 
280 
286  protected ‪$persistenceIdentifier;
287 
292  public function ‪injectObjectManager(\‪TYPO3\CMS\‪Extbase\Object\ObjectManagerInterface ‪$objectManager)
293  {
294  $this->objectManager = ‪$objectManager;
295  }
296 
306  public function ‪__construct(
307  string ‪$identifier,
308  array $prototypeConfiguration = [],
309  string ‪$type = 'Form',
310  string ‪$persistenceIdentifier = null
311  ) {
312  $this->typeDefinitions = $prototypeConfiguration['formElementsDefinition'] ?? [];
313  $this->validatorsDefinition = $prototypeConfiguration['validatorsDefinition'] ?? [];
314  $this->finishersDefinition = $prototypeConfiguration['finishersDefinition'] ?? [];
315  $this->conditionContextDefinition = $prototypeConfiguration['conditionContextDefinition'] ?? [];
316 
317  if (!is_string(‪$identifier) || strlen(‪$identifier) === 0) {
318  throw new ‪IdentifierNotValidException('The given identifier was not a string or the string was empty.', 1477082503);
319  }
320 
321  $this->identifier = ‪$identifier;
322  $this->type = ‪$type;
323  $this->persistenceIdentifier = ‪$persistenceIdentifier;
324 
325  if ($prototypeConfiguration !== []) {
327  }
328  }
329 
336  protected function ‪initializeFromFormDefaults()
337  {
338  if (!isset($this->typeDefinitions[$this->type])) {
339  throw new TypeDefinitionNotFoundException(sprintf('Type "%s" not found. Probably some configuration is missing.', $this->type), 1474905835);
340  }
341  $typeDefinition = $this->typeDefinitions[‪$this->type];
342  $this->‪setOptions($typeDefinition);
343  }
344 
354  public function ‪setOptions(array $options, bool $resetFinishers = false)
355  {
356  if (isset($options['rendererClassName'])) {
357  $this->‪setRendererClassName($options['rendererClassName']);
358  }
359  if (isset($options['label'])) {
360  $this->‪setLabel($options['label']);
361  }
362  if (isset($options['renderingOptions'])) {
363  foreach ($options['renderingOptions'] as $key => $value) {
364  $this->‪setRenderingOption($key, $value);
365  }
366  }
367  if (isset($options['finishers'])) {
368  if ($resetFinishers) {
369  $this->finishers = [];
370  }
371  foreach ($options['finishers'] as $finisherConfiguration) {
372  $this->‪createFinisher($finisherConfiguration['identifier'], $finisherConfiguration['options'] ?? []);
373  }
374  }
375 
376  if (isset($options['variants'])) {
377  foreach ($options['variants'] as $variantConfiguration) {
378  $this->‪createVariant($variantConfiguration);
379  }
380  }
381 
383  $options,
384  ['rendererClassName', 'renderingOptions', 'finishers', 'formEditor', 'label', 'variants']
385  );
386  }
387 
401  public function ‪createPage(string ‪$identifier, string $typeName = 'Page'): Page
402  {
403  if (!isset($this->typeDefinitions[$typeName])) {
404  throw new TypeDefinitionNotFoundException(sprintf('Type "%s" not found. Probably some configuration is missing.', $typeName), 1474905953);
405  }
406 
407  $typeDefinition = $this->typeDefinitions[$typeName];
408 
409  if (!isset($typeDefinition['implementationClassName'])) {
410  throw new TypeDefinitionNotFoundException(sprintf('The "implementationClassName" was not set in type definition "%s".', $typeName), 1477083126);
411  }
412  $implementationClassName = $typeDefinition['implementationClassName'];
413  $page = $this->objectManager->get($implementationClassName, ‪$identifier, $typeName);
414 
415  if (isset($typeDefinition['label'])) {
416  $page->setLabel($typeDefinition['label']);
417  }
418 
419  if (isset($typeDefinition['renderingOptions'])) {
420  foreach ($typeDefinition['renderingOptions'] as $key => $value) {
421  $page->setRenderingOption($key, $value);
422  }
423  }
424 
426  $typeDefinition,
427  ['implementationClassName', 'label', 'renderingOptions', 'formEditor']
428  );
429 
430  $this->‪addPage($page);
431  return $page;
432  }
433 
443  public function ‪addPage(‪Page $page)
444  {
445  $this->‪addRenderable($page);
446  }
447 
453  public function ‪getPages(): array
454  {
455  return ‪$this->renderables;
456  }
457 
464  public function ‪hasPageWithIndex(int ‪$index): bool
465  {
466  return isset($this->renderables[‪$index]);
467  }
468 
478  public function ‪getPageByIndex(int ‪$index)
479  {
480  if (!$this->‪hasPageWithIndex($index)) {
481  throw new FormException(sprintf('There is no page with an index of %d', ‪$index), 1329233627);
482  }
483  return $this->renderables[‪$index];
484  }
485 
491  public function ‪addFinisher(‪FinisherInterface $finisher)
492  {
493  $this->finishers[] = $finisher;
494  }
495 
502  public function ‪createFinisher(string $finisherIdentifier, array $options = []): ‪FinisherInterface
503  {
504  if (isset($this->finishersDefinition[$finisherIdentifier]) && is_array($this->finishersDefinition[$finisherIdentifier]) && isset($this->finishersDefinition[$finisherIdentifier]['implementationClassName'])) {
505  $implementationClassName = $this->finishersDefinition[$finisherIdentifier]['implementationClassName'];
506  $defaultOptions = $this->finishersDefinition[$finisherIdentifier]['options'] ?? [];
507  ‪ArrayUtility::mergeRecursiveWithOverrule($defaultOptions, $options);
508 
509  $finisher = $this->objectManager->get($implementationClassName, $finisherIdentifier);
510  $finisher->setOptions($defaultOptions);
511  $this->‪addFinisher($finisher);
512  return $finisher;
513  }
514  throw new ‪FinisherPresetNotFoundException('The finisher preset identified by "' . $finisherIdentifier . '" could not be found, or the implementationClassName was not specified.', 1328709784);
515  }
516 
522  public function ‪getFinishers(): array
523  {
524  return ‪$this->finishers;
525  }
526 
534  public function ‪registerRenderable(‪RenderableInterface $renderable)
535  {
536  if ($renderable instanceof ‪FormElementInterface) {
537  if (isset($this->elementsByIdentifier[$renderable->‪getIdentifier()])) {
538  throw new ‪DuplicateFormElementException(sprintf('A form element with identifier "%s" is already part of the form.', $renderable->‪getIdentifier()), 1325663761);
539  }
540  $this->elementsByIdentifier[$renderable->‪getIdentifier()] = $renderable;
541  }
542  }
543 
550  public function ‪unregisterRenderable(RenderableInterface $renderable)
551  {
552  if ($renderable instanceof FormElementInterface) {
553  unset($this->elementsByIdentifier[$renderable->getIdentifier()]);
554  }
555  }
556 
565  public function ‪getElementByIdentifier(string $elementIdentifier)
566  {
567  return $this->elementsByIdentifier[$elementIdentifier] ?? null;
568  }
569 
577  public function ‪addElementDefaultValue(string $elementIdentifier, $defaultValue)
578  {
579  $this->elementDefaultValues = ‪ArrayUtility::setValueByPath(
580  $this->elementDefaultValues,
581  $elementIdentifier,
582  $defaultValue,
583  '.'
584  );
585  }
586 
595  public function ‪getElementDefaultValueByIdentifier(string $elementIdentifier)
596  {
597  return ‪ObjectAccess::getPropertyPath($this->elementDefaultValues, $elementIdentifier);
598  }
599 
606  public function ‪movePageBefore(‪Page $pageToMove, ‪Page $referencePage)
607  {
608  $this->‪moveRenderableBefore($pageToMove, $referencePage);
609  }
610 
617  public function ‪movePageAfter(‪Page $pageToMove, ‪Page $referencePage)
618  {
619  $this->‪moveRenderableAfter($pageToMove, $referencePage);
620  }
621 
627  public function ‪removePage(Page $pageToRemove)
628  {
629  $this->‪removeRenderable($pageToRemove);
630  }
631 
640  public function ‪bind(‪Request $request, ‪Response $response): ‪FormRuntime
641  {
642  return $this->objectManager->get(FormRuntime::class, $this, $request, $response);
643  }
644 
649  public function ‪getProcessingRule(string $propertyPath): ProcessingRule
650  {
651  if (!isset($this->processingRules[$propertyPath])) {
652  $this->processingRules[$propertyPath] = $this->objectManager->get(ProcessingRule::class);
653  }
654  return $this->processingRules[$propertyPath];
655  }
656 
663  public function ‪getProcessingRules(): array
664  {
666  }
667 
672  public function ‪getTypeDefinitions(): array
673  {
675  }
676 
681  public function ‪getValidatorsDefinition(): array
682  {
684  }
685 
690  public function ‪getConditionContextDefinition(): array
691  {
693  }
694 
701  public function ‪getPersistenceIdentifier(): string
702  {
704  }
705 
711  public function ‪setRendererClassName(string ‪$rendererClassName)
712  {
713  $this->rendererClassName = ‪$rendererClassName;
714  }
715 
721  public function ‪getRendererClassName(): string
722  {
724  }
725 }
‪TYPO3\CMS\Form\Domain\Model\Renderable\AbstractRenderable\$index
‪int $index
Definition: AbstractRenderable.php:76
‪TYPO3\CMS\Form\Domain\Model\Exception\FinisherPresetNotFoundException
Definition: FinisherPresetNotFoundException.php:25
‪TYPO3\CMS\Form\Domain\Model\FormDefinition\$finishersDefinition
‪array $finishersDefinition
Definition: FormDefinition.php:265
‪TYPO3\CMS\Form\Domain\Model\Exception\FormDefinitionConsistencyException
Definition: FormDefinitionConsistencyException.php:25
‪TYPO3\CMS\Form\Domain\Model\FormDefinition\createFinisher
‪FinisherInterface createFinisher(string $finisherIdentifier, array $options=[])
Definition: FormDefinition.php:491
‪TYPO3\CMS\Form\Domain\Runtime\FormRuntime
Definition: FormRuntime.php:97
‪TYPO3\CMS\Form\Domain\Exception\TypeDefinitionNotFoundException
Definition: TypeDefinitionNotFoundException.php:27
‪TYPO3\CMS\Form\Domain\Model\FormDefinition\addFinisher
‪addFinisher(FinisherInterface $finisher)
Definition: FormDefinition.php:480
‪TYPO3\CMS\Form\Domain\Model\FormDefinition\removePage
‪removePage(Page $pageToRemove)
Definition: FormDefinition.php:616
‪TYPO3\CMS\Form\Domain\Model\Renderable\AbstractCompositeRenderable
Definition: AbstractCompositeRenderable.php:30
‪TYPO3\CMS\Form\Domain\Model\Renderable\AbstractCompositeRenderable\moveRenderableAfter
‪moveRenderableAfter(RenderableInterface $renderableToMove, RenderableInterface $referenceRenderable)
Definition: AbstractCompositeRenderable.php:105
‪TYPO3\CMS\Form\Domain\Model\FormDefinition\$conditionContextDefinition
‪array $conditionContextDefinition
Definition: FormDefinition.php:269
‪TYPO3\CMS\Form\Domain\Model\Renderable\AbstractRenderable\setRenderingOption
‪mixed setRenderingOption(string $key, $value)
Definition: AbstractRenderable.php:274
‪TYPO3\CMS\Form\Domain\Finishers\FinisherInterface
Definition: FinisherInterface.php:27
‪TYPO3\CMS\Extbase\Annotation
Definition: IgnoreValidation.php:4
‪TYPO3\CMS\Form\Domain\Model\FormDefinition\setOptions
‪setOptions(array $options, bool $resetFinishers=false)
Definition: FormDefinition.php:343
‪TYPO3\CMS\Form\Domain\Model\FormDefinition\$elementsByIdentifier
‪TYPO3 CMS Form Domain Model FormElements FormElementInterface[] $elementsByIdentifier
Definition: FormDefinition.php:241
‪TYPO3\CMS\Form\Domain\Model\FormDefinition\movePageAfter
‪movePageAfter(Page $pageToMove, Page $referencePage)
Definition: FormDefinition.php:606
‪TYPO3\CMS\Form\Domain\Model\Renderable\AbstractCompositeRenderable\removeRenderable
‪removeRenderable(RenderableInterface $renderableToRemove)
Definition: AbstractCompositeRenderable.php:159
‪TYPO3\CMS\Form\Domain\Model\Renderable\AbstractRenderable\setLabel
‪setLabel(string $label)
Definition: AbstractRenderable.php:401
‪TYPO3\CMS\Form\Domain\Exception\IdentifierNotValidException
Definition: IdentifierNotValidException.php:27
‪TYPO3\CMS\Form\Domain\Model\FormDefinition\getPages
‪array< Page > getPages()
Definition: FormDefinition.php:442
‪TYPO3
‪TYPO3\CMS\Form\Domain\Model\Renderable\AbstractCompositeRenderable\moveRenderableBefore
‪moveRenderableBefore(RenderableInterface $renderableToMove, RenderableInterface $referenceRenderable)
Definition: AbstractCompositeRenderable.php:69
‪TYPO3\CMS\Form\Domain\Model\FormDefinition\getFinishers
‪TYPO3 CMS Form Domain Finishers FinisherInterface[] getFinishers()
Definition: FormDefinition.php:511
‪TYPO3\CMS\Form\Domain\Model\FormDefinition\setRendererClassName
‪setRendererClassName(string $rendererClassName)
Definition: FormDefinition.php:700
‪TYPO3\CMS\Form\Domain\Model\FormDefinition\createPage
‪Page createPage(string $identifier, string $typeName='Page')
Definition: FormDefinition.php:390
‪TYPO3\CMS\Form\Domain\Model\FormDefinition\getPersistenceIdentifier
‪string getPersistenceIdentifier()
Definition: FormDefinition.php:690
‪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\FormDefinition\$rendererClassName
‪string $rendererClassName
Definition: FormDefinition.php:253
‪TYPO3\CMS\Form\Domain\Model\FormDefinition\getElementByIdentifier
‪FormElementInterface getElementByIdentifier(string $elementIdentifier)
Definition: FormDefinition.php:554
‪TYPO3\CMS\Form\Domain\Model\Renderable\AbstractRenderable\$type
‪string $type
Definition: AbstractRenderable.php:46
‪TYPO3\CMS\Form\Domain\Model\FormDefinition\hasPageWithIndex
‪bool hasPageWithIndex(int $index)
Definition: FormDefinition.php:453
‪TYPO3\CMS\Form\Domain\Model\FormDefinition\__construct
‪__construct(string $identifier, array $prototypeConfiguration=[], string $type='Form', string $persistenceIdentifier=null)
Definition: FormDefinition.php:295
‪TYPO3\CMS\Form\Domain\Model\Renderable\RootRenderableInterface\getIdentifier
‪string getIdentifier()
‪TYPO3\CMS\Extbase\Reflection\ObjectAccess
Definition: ObjectAccess.php:29
‪TYPO3\CMS\Form\Domain\Model\FormDefinition\getElementDefaultValueByIdentifier
‪mixed getElementDefaultValueByIdentifier(string $elementIdentifier)
Definition: FormDefinition.php:584
‪TYPO3\CMS\Form\Domain\Model\FormElements\Page
Definition: Page.php:40
‪TYPO3\CMS\Form\Domain\Model\Renderable\RenderableInterface
Definition: RenderableInterface.php:28
‪TYPO3\CMS\Form\Domain\Model\FormDefinition\bind
‪FormRuntime bind(Request $request, Response $response)
Definition: FormDefinition.php:629
‪TYPO3\CMS\Form\Domain\Model
‪TYPO3\CMS\Form\Exception
Definition: Exception.php:24
‪TYPO3\CMS\Form\Domain\Model\FormDefinition\unregisterRenderable
‪unregisterRenderable(RenderableInterface $renderable)
Definition: FormDefinition.php:539
‪TYPO3\CMS\Form\Domain\Model\FormElements\FormElementInterface
Definition: FormElementInterface.php:36
‪TYPO3\CMS\Extbase\Mvc\Web\Response
Definition: Response.php:25
‪TYPO3\CMS\Form\Domain\Model\FormDefinition\getConditionContextDefinition
‪array getConditionContextDefinition()
Definition: FormDefinition.php:679
‪TYPO3\CMS\Form\Domain\Model\FormDefinition\$validatorsDefinition
‪array $validatorsDefinition
Definition: FormDefinition.php:261
‪TYPO3\CMS\Form\Domain\Model\FormDefinition\$typeDefinitions
‪array $typeDefinitions
Definition: FormDefinition.php:257
‪TYPO3\CMS\Form\Domain\Model\FormDefinition\getProcessingRule
‪ProcessingRule getProcessingRule(string $propertyPath)
Definition: FormDefinition.php:638
‪TYPO3\CMS\Form\Domain\Model\FormDefinition\$finishers
‪TYPO3 CMS Form Domain Finishers FinisherInterface[] $finishers
Definition: FormDefinition.php:228
‪TYPO3\CMS\Form\Domain\Model\FormDefinition\addElementDefaultValue
‪addElementDefaultValue(string $elementIdentifier, $defaultValue)
Definition: FormDefinition.php:566
‪TYPO3\CMS\Form\Domain\Model\FormDefinition\$persistenceIdentifier
‪string $persistenceIdentifier
Definition: FormDefinition.php:275
‪TYPO3\CMS\Form\Domain\Model\Renderable\AbstractCompositeRenderable\$renderables
‪TYPO3 CMS Form Domain Model Renderable RenderableInterface[] $renderables
Definition: AbstractCompositeRenderable.php:36
‪TYPO3\CMS\Extbase\Mvc\Web\Request
Definition: Request.php:21
‪TYPO3\CMS\Form\Domain\Model\FormDefinition\getTypeDefinitions
‪array getTypeDefinitions()
Definition: FormDefinition.php:661
‪TYPO3\CMS\Form\Domain\Model\FormDefinition\getValidatorsDefinition
‪array getValidatorsDefinition()
Definition: FormDefinition.php:670
‪TYPO3\CMS\Core\Utility\ArrayUtility\setValueByPath
‪static array setValueByPath(array $array, $path, $value, $delimiter='/')
Definition: ArrayUtility.php:271
‪TYPO3\CMS\Form\Domain\Model\FormDefinition
Definition: FormDefinition.php:218
‪TYPO3\CMS\Form\Domain\Model\FormDefinition\getProcessingRules
‪TYPO3 CMS Form Mvc ProcessingRule[] getProcessingRules()
Definition: FormDefinition.php:652
‪TYPO3\CMS\Extbase\Reflection\ObjectAccess\getPropertyPath
‪static mixed getPropertyPath($subject, $propertyPath)
Definition: ObjectAccess.php:144
‪TYPO3\CMS\Form\Domain\Model\Renderable\VariableRenderableInterface
Definition: VariableRenderableInterface.php:25
‪TYPO3\CMS\Form\Domain\Runtime\FormRuntime
Definition: FormSession.php:3
‪TYPO3\CMS\Form\Domain\Model\FormDefinition\injectObjectManager
‪injectObjectManager(\TYPO3\CMS\Extbase\Object\ObjectManagerInterface $objectManager)
Definition: FormDefinition.php:281
‪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\FormDefinition\addPage
‪addPage(Page $page)
Definition: FormDefinition.php:432
‪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\Form\Domain\Model\Renderable\AbstractRenderable\$identifier
‪string $identifier
Definition: AbstractRenderable.php:52
‪TYPO3\CMS\Form\Domain\Model\FormDefinition\$elementDefaultValues
‪array $elementDefaultValues
Definition: FormDefinition.php:247
‪TYPO3\CMS\Form\Domain\Model\FormDefinition\$objectManager
‪TYPO3 CMS Extbase Object ObjectManagerInterface $objectManager
Definition: FormDefinition.php:222
‪TYPO3\CMS\Form\Domain\Model\Renderable\AbstractCompositeRenderable\addRenderable
‪addRenderable(RenderableInterface $renderable)
Definition: AbstractCompositeRenderable.php:48
‪TYPO3\CMS\Form\Domain\Model\Exception\DuplicateFormElementException
Definition: DuplicateFormElementException.php:25
‪TYPO3\CMS\Form\Domain\Model\FormDefinition\movePageBefore
‪movePageBefore(Page $pageToMove, Page $referencePage)
Definition: FormDefinition.php:595
‪TYPO3\CMS\Form\Domain\Model\FormDefinition\getPageByIndex
‪Page getPageByIndex(int $index)
Definition: FormDefinition.php:467
‪TYPO3\CMS\Form\Domain\Model\FormDefinition\registerRenderable
‪registerRenderable(RenderableInterface $renderable)
Definition: FormDefinition.php:523
‪TYPO3\CMS\Form\Mvc\ProcessingRule
Definition: ProcessingRule.php:32
‪TYPO3\CMS\Form\Domain\Model\FormDefinition\initializeFromFormDefaults
‪initializeFromFormDefaults()
Definition: FormDefinition.php:325
‪TYPO3\CMS\Form\Domain\Model\FormDefinition\$processingRules
‪TYPO3 CMS Form Mvc ProcessingRule[] $processingRules
Definition: FormDefinition.php:234