‪TYPO3CMS  9.5
AbstractFormElement.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 
25 
46 {
47 
51  protected ‪$properties = [];
52 
60  public function ‪__construct(string ‪$identifier, string ‪$type)
61  {
62  if (!is_string(‪$identifier) || strlen(‪$identifier) === 0) {
63  throw new ‪IdentifierNotValidException('The given identifier was not a string or the string was empty.', 1477082502);
64  }
65  $this->identifier = ‪$identifier;
66  $this->type = ‪$type;
67  }
68 
72  public function ‪initializeFormElement()
73  {
74  foreach (‪$GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['ext/form']['initializeFormElement'] ?? [] as $className) {
75  $hookObj = GeneralUtility::makeInstance($className);
76  if (method_exists($hookObj, 'initializeFormElement')) {
77  $hookObj->initializeFormElement(
78  $this
79  );
80  }
81  }
82  }
83 
89  public function ‪getUniqueIdentifier(): string
90  {
91  $formDefinition = $this->‪getRootForm();
92  $uniqueIdentifier = sprintf('%s-%s', $formDefinition->getIdentifier(), $this->identifier);
93  $uniqueIdentifier = preg_replace('/[^a-zA-Z0-9_-]/', '_', $uniqueIdentifier);
94  return lcfirst($uniqueIdentifier);
95  }
96 
102  public function ‪getDefaultValue()
103  {
104  $formDefinition = $this->‪getRootForm();
105  return $formDefinition->getElementDefaultValueByIdentifier($this->identifier);
106  }
107 
113  public function ‪setDefaultValue($defaultValue)
114  {
115  $formDefinition = $this->‪getRootForm();
116  $currentDefaultValue = $formDefinition->getElementDefaultValueByIdentifier($this->identifier);
117  if (is_array($currentDefaultValue) && is_array($defaultValue)) {
118  ‪ArrayUtility::mergeRecursiveWithOverrule($currentDefaultValue, $defaultValue);
119  $defaultValue = ‪ArrayUtility::removeNullValuesRecursive($currentDefaultValue);
120  }
121  $formDefinition->addElementDefaultValue($this->identifier, $defaultValue);
122  }
123 
129  public function ‪isRequired(): bool
130  {
131  foreach ($this->‪getValidators() as ‪$validator) {
132  if (‪$validator instanceof ‪NotEmptyValidator) {
133  return true;
134  }
135  }
136  return false;
137  }
138 
145  public function ‪setProperty(string $key, $value)
146  {
147  if (is_array($value) && isset($this->properties[$key]) && is_array($this->properties[$key])) {
148  ‪ArrayUtility::mergeRecursiveWithOverrule($this->properties[$key], $value);
149  $this->properties[$key] = ‪ArrayUtility::removeNullValuesRecursive($this->properties[$key]);
150  } elseif ($value === null) {
151  unset($this->properties[$key]);
152  } else {
153  $this->properties[$key] = $value;
154  }
155  }
156 
162  public function ‪getProperties(): array
163  {
164  return ‪$this->properties;
165  }
166 }
‪TYPO3\CMS\Form\Domain\Model\FormElements\AbstractFormElement
Definition: AbstractFormElement.php:46
‪TYPO3\CMS\Form\Domain\Model\FormElements\AbstractFormElement\setProperty
‪setProperty(string $key, $value)
Definition: AbstractFormElement.php:144
‪TYPO3\CMS\Form\Domain\Exception\IdentifierNotValidException
Definition: IdentifierNotValidException.php:27
‪TYPO3\CMS\Form\Domain\Model\FormElements\AbstractFormElement\getProperties
‪array getProperties()
Definition: AbstractFormElement.php:161
‪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\FormElements\AbstractFormElement\__construct
‪__construct(string $identifier, string $type)
Definition: AbstractFormElement.php:59
‪TYPO3\CMS\Form\Domain\Model\Renderable\AbstractRenderable\getRootForm
‪FormDefinition getRootForm()
Definition: AbstractRenderable.php:313
‪TYPO3\CMS\Form\Domain\Model\FormElements\FormElementInterface
Definition: FormElementInterface.php:36
‪$validator
‪if(isset($args['d'])) $validator
Definition: validateRstFiles.php:218
‪TYPO3\CMS\Form\Domain\Model\FormElements\AbstractFormElement\getDefaultValue
‪mixed getDefaultValue()
Definition: AbstractFormElement.php:101
‪TYPO3\CMS\Form\Domain\Model\FormElements\AbstractFormElement\isRequired
‪bool isRequired()
Definition: AbstractFormElement.php:128
‪TYPO3\CMS\Core\Utility\ArrayUtility
Definition: ArrayUtility.php:23
‪TYPO3\CMS\Form\Domain\Model\FormElements\AbstractFormElement\getUniqueIdentifier
‪string getUniqueIdentifier()
Definition: AbstractFormElement.php:88
‪$GLOBALS
‪$GLOBALS['TYPO3_CONF_VARS']['EXTCONF']['adminpanel']['modules']
Definition: ext_localconf.php:5
‪TYPO3\CMS\Form\Domain\Model\FormElements\AbstractFormElement\$properties
‪array $properties
Definition: AbstractFormElement.php:50
‪TYPO3\CMS\Form\Domain\Model\FormElements\AbstractFormElement\initializeFormElement
‪initializeFormElement()
Definition: AbstractFormElement.php:71
‪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\Extbase\Validation\Validator\NotEmptyValidator
Definition: NotEmptyValidator.php:21
‪TYPO3\CMS\Core\Utility\GeneralUtility
Definition: GeneralUtility.php:45
‪TYPO3\CMS\Form\Domain\Model\FormElements\FormElementInterface\getValidators
‪SplObjectStorage< ValidatorInterface > getValidators()
‪TYPO3\CMS\Form\Domain\Model\FormElements\AbstractFormElement\setDefaultValue
‪setDefaultValue($defaultValue)
Definition: AbstractFormElement.php:112
‪TYPO3\CMS\Form\Domain\Model\FormElements
Definition: AbstractFormElement.php:3