TYPO3 CMS  TYPO3_8-7
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 
37 
217 {
218 
222  protected $objectManager;
223 
229  protected $finishers = [];
230 
236  protected $processingRules = [];
237 
244  protected $elementsByIdentifier = [];
245 
251  protected $elementDefaultValues = [];
252 
258  protected $rendererClassName = '';
259 
263  protected $typeDefinitions;
264 
269 
274 
280  protected $persistenceIdentifier = null;
281 
286  public function injectObjectManager(\TYPO3\CMS\Extbase\Object\ObjectManagerInterface $objectManager)
287  {
288  $this->objectManager = $objectManager;
289  }
290 
301  public function __construct(
302  string $identifier,
303  array $prototypeConfiguration = [],
304  string $type = 'Form',
305  string $persistenceIdentifier = null
306  ) {
307  $this->typeDefinitions = isset($prototypeConfiguration['formElementsDefinition']) ? $prototypeConfiguration['formElementsDefinition'] : [];
308  $this->validatorsDefinition = isset($prototypeConfiguration['validatorsDefinition']) ? $prototypeConfiguration['validatorsDefinition'] : [];
309  $this->finishersDefinition = isset($prototypeConfiguration['finishersDefinition']) ? $prototypeConfiguration['finishersDefinition'] : [];
310 
311  if (!is_string($identifier) || strlen($identifier) === 0) {
312  throw new IdentifierNotValidException('The given identifier was not a string or the string was empty.', 1477082503);
313  }
314 
315  $this->identifier = $identifier;
316  $this->type = $type;
317  $this->persistenceIdentifier = $persistenceIdentifier;
318 
319  if ($prototypeConfiguration !== []) {
321  }
322  }
323 
330  protected function initializeFromFormDefaults()
331  {
332  if (!isset($this->typeDefinitions[$this->type])) {
333  throw new TypeDefinitionNotFoundException(sprintf('Type "%s" not found. Probably some configuration is missing.', $this->type), 1474905835);
334  }
335  $typeDefinition = $this->typeDefinitions[$this->type];
336  $this->setOptions($typeDefinition);
337  }
338 
347  public function setOptions(array $options)
348  {
349  if (isset($options['rendererClassName'])) {
350  $this->setRendererClassName($options['rendererClassName']);
351  }
352  if (isset($options['label'])) {
353  $this->setLabel($options['label']);
354  }
355  if (isset($options['renderingOptions'])) {
356  foreach ($options['renderingOptions'] as $key => $value) {
357  if (is_array($value)) {
358  $currentValue = isset($this->getRenderingOptions()[$key]) ? $this->getRenderingOptions()[$key] : [];
359  ArrayUtility::mergeRecursiveWithOverrule($currentValue, $value);
360  $this->setRenderingOption($key, $currentValue);
361  } else {
362  $this->setRenderingOption($key, $value);
363  }
364  }
365  }
366  if (isset($options['finishers'])) {
367  foreach ($options['finishers'] as $finisherConfiguration) {
368  $this->createFinisher($finisherConfiguration['identifier'], isset($finisherConfiguration['options']) ? $finisherConfiguration['options'] : []);
369  }
370  }
371 
373  $options,
374  ['rendererClassName', 'renderingOptions', 'finishers', 'formEditor', 'label']
375  );
376  }
377 
392  public function createPage(string $identifier, string $typeName = 'Page'): Page
393  {
394  if (!isset($this->typeDefinitions[$typeName])) {
395  throw new TypeDefinitionNotFoundException(sprintf('Type "%s" not found. Probably some configuration is missing.', $typeName), 1474905953);
396  }
397 
398  $typeDefinition = $this->typeDefinitions[$typeName];
399 
400  if (!isset($typeDefinition['implementationClassName'])) {
401  throw new TypeDefinitionNotFoundException(sprintf('The "implementationClassName" was not set in type definition "%s".', $typeName), 1477083126);
402  }
403  $implementationClassName = $typeDefinition['implementationClassName'];
404  $page = $this->objectManager->get($implementationClassName, $identifier, $typeName);
405 
406  if (isset($typeDefinition['label'])) {
407  $page->setLabel($typeDefinition['label']);
408  }
409 
410  if (isset($typeDefinition['renderingOptions'])) {
411  foreach ($typeDefinition['renderingOptions'] as $key => $value) {
412  $page->setRenderingOption($key, $value);
413  }
414  }
415 
417  $typeDefinition,
418  ['implementationClassName', 'label', 'renderingOptions', 'formEditor']
419  );
420 
421  $this->addPage($page);
422  return $page;
423  }
424 
435  public function addPage(Page $page)
436  {
437  $this->addRenderable($page);
438  }
439 
446  public function getPages(): array
447  {
448  return $this->renderables;
449  }
450 
458  public function hasPageWithIndex(int $index): bool
459  {
460  return isset($this->renderables[$index]);
461  }
462 
473  public function getPageByIndex(int $index)
474  {
475  if (!$this->hasPageWithIndex($index)) {
476  throw new FormException(sprintf('There is no page with an index of %d', $index), 1329233627);
477  }
478  return $this->renderables[$index];
479  }
480 
487  public function addFinisher(FinisherInterface $finisher)
488  {
489  $this->finishers[] = $finisher;
490  }
491 
499  public function createFinisher(string $finisherIdentifier, array $options = []): FinisherInterface
500  {
501  if (isset($this->finishersDefinition[$finisherIdentifier]) && is_array($this->finishersDefinition[$finisherIdentifier]) && isset($this->finishersDefinition[$finisherIdentifier]['implementationClassName'])) {
502  $implementationClassName = $this->finishersDefinition[$finisherIdentifier]['implementationClassName'];
503  $defaultOptions = isset($this->finishersDefinition[$finisherIdentifier]['options']) ? $this->finishersDefinition[$finisherIdentifier]['options'] : [];
504  ArrayUtility::mergeRecursiveWithOverrule($defaultOptions, $options);
505 
506  $finisher = $this->objectManager->get($implementationClassName, $finisherIdentifier);
507  $finisher->setOptions($defaultOptions);
508  $this->addFinisher($finisher);
509  return $finisher;
510  }
511  throw new FinisherPresetNotFoundException('The finisher preset identified by "' . $finisherIdentifier . '" could not be found, or the implementationClassName was not specified.', 1328709784);
512  }
513 
520  public function getFinishers(): array
521  {
522  return $this->finishers;
523  }
524 
532  public function registerRenderable(RenderableInterface $renderable)
533  {
534  if ($renderable instanceof FormElementInterface) {
535  if (isset($this->elementsByIdentifier[$renderable->getIdentifier()])) {
536  throw new DuplicateFormElementException(sprintf('A form element with identifier "%s" is already part of the form.', $renderable->getIdentifier()), 1325663761);
537  }
538  $this->elementsByIdentifier[$renderable->getIdentifier()] = $renderable;
539  }
540  }
541 
548  public function unregisterRenderable(RenderableInterface $renderable)
549  {
550  if ($renderable instanceof FormElementInterface) {
551  unset($this->elementsByIdentifier[$renderable->getIdentifier()]);
552  }
553  }
554 
564  public function getElementByIdentifier(string $elementIdentifier)
565  {
566  return isset($this->elementsByIdentifier[$elementIdentifier]) ? $this->elementsByIdentifier[$elementIdentifier] : null;
567  }
568 
576  public function addElementDefaultValue(string $elementIdentifier, $defaultValue)
577  {
578  $this->elementDefaultValues = ArrayUtility::setValueByPath(
579  $this->elementDefaultValues,
580  $elementIdentifier,
581  $defaultValue,
582  '.'
583  );
584  }
585 
594  public function getElementDefaultValueByIdentifier(string $elementIdentifier)
595  {
596  return ObjectAccess::getPropertyPath($this->elementDefaultValues, $elementIdentifier);
597  }
598 
606  public function movePageBefore(Page $pageToMove, Page $referencePage)
607  {
608  $this->moveRenderableBefore($pageToMove, $referencePage);
609  }
610 
618  public function movePageAfter(Page $pageToMove, Page $referencePage)
619  {
620  $this->moveRenderableAfter($pageToMove, $referencePage);
621  }
622 
629  public function removePage(Page $pageToRemove)
630  {
631  $this->removeRenderable($pageToRemove);
632  }
633 
643  public function bind(Request $request, Response $response): FormRuntime
644  {
645  return $this->objectManager->get(FormRuntime::class, $this, $request, $response);
646  }
647 
653  public function getProcessingRule(string $propertyPath): ProcessingRule
654  {
655  if (!isset($this->processingRules[$propertyPath])) {
656  $this->processingRules[$propertyPath] = $this->objectManager->get(ProcessingRule::class);
657  }
658  return $this->processingRules[$propertyPath];
659  }
660 
667  public function getProcessingRules(): array
668  {
669  return $this->processingRules;
670  }
671 
676  public function getTypeDefinitions(): array
677  {
678  return $this->typeDefinitions;
679  }
680 
685  public function getValidatorsDefinition(): array
686  {
688  }
689 
696  public function getPersistenceIdentifier(): string
697  {
699  }
700 
707  public function setRendererClassName(string $rendererClassName)
708  {
709  $this->rendererClassName = $rendererClassName;
710  }
711 
718  public function getRendererClassName(): string
719  {
721  }
722 }
movePageAfter(Page $pageToMove, Page $referencePage)
moveRenderableAfter(RenderableInterface $renderableToMove, RenderableInterface $referenceRenderable)
moveRenderableBefore(RenderableInterface $renderableToMove, RenderableInterface $referenceRenderable)
static setValueByPath(array $array, $path, $value, $delimiter='/')
movePageBefore(Page $pageToMove, Page $referencePage)
createFinisher(string $finisherIdentifier, array $options=[])
bind(Request $request, Response $response)
addElementDefaultValue(string $elementIdentifier, $defaultValue)
static assertAllArrayKeysAreValid(array $arrayToTest, array $allowedArrayKeys)
addFinisher(FinisherInterface $finisher)
setRendererClassName(string $rendererClassName)
createPage(string $identifier, string $typeName='Page')
__construct(string $identifier, array $prototypeConfiguration=[], string $type='Form', string $persistenceIdentifier=null)
injectObjectManager(\TYPO3\CMS\Extbase\Object\ObjectManagerInterface $objectManager)
unregisterRenderable(RenderableInterface $renderable)
static getPropertyPath($subject, $propertyPath)
getElementByIdentifier(string $elementIdentifier)
static mergeRecursiveWithOverrule(array &$original, array $overrule, $addKeys=true, $includeEmptyValues=true, $enableUnsetFeature=true)
registerRenderable(RenderableInterface $renderable)
getElementDefaultValueByIdentifier(string $elementIdentifier)