‪TYPO3CMS  10.4
AbstractFormElement.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 
29 
50 {
51 
55  protected ‪$properties = [];
56 
64  public function ‪__construct(string ‪$identifier, string ‪$type)
65  {
66  if (!is_string(‪$identifier) || strlen(‪$identifier) === 0) {
67  throw new ‪IdentifierNotValidException('The given identifier was not a string or the string was empty.', 1477082502);
68  }
69  $this->identifier = ‪$identifier;
70  $this->type = ‪$type;
71  }
72 
76  public function ‪initializeFormElement()
77  {
78  foreach (‪$GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['ext/form']['initializeFormElement'] ?? [] as $className) {
79  $hookObj = GeneralUtility::makeInstance($className);
80  if (method_exists($hookObj, 'initializeFormElement')) {
81  $hookObj->initializeFormElement(
82  $this
83  );
84  }
85  }
86  }
87 
93  public function ‪getUniqueIdentifier(): string
94  {
95  $formDefinition = $this->‪getRootForm();
96  $uniqueIdentifier = sprintf('%s-%s', $formDefinition->getIdentifier(), $this->identifier);
97  $uniqueIdentifier = (string)preg_replace('/[^a-zA-Z0-9_-]/', '_', $uniqueIdentifier);
98  return lcfirst($uniqueIdentifier);
99  }
100 
106  public function ‪getDefaultValue()
107  {
108  $formDefinition = $this->‪getRootForm();
109  return $formDefinition->getElementDefaultValueByIdentifier($this->identifier);
110  }
111 
117  public function ‪setDefaultValue($defaultValue)
118  {
119  $formDefinition = $this->‪getRootForm();
120  $currentDefaultValue = $formDefinition->getElementDefaultValueByIdentifier($this->identifier);
121  if (is_array($currentDefaultValue) && is_array($defaultValue)) {
122  ‪ArrayUtility::mergeRecursiveWithOverrule($currentDefaultValue, $defaultValue);
123  $defaultValue = ‪ArrayUtility::removeNullValuesRecursive($currentDefaultValue);
124  }
125  $formDefinition->addElementDefaultValue($this->identifier, $defaultValue);
126  }
127 
133  public function ‪isRequired(): bool
134  {
135  foreach ($this->‪getValidators() as ‪$validator) {
136  if (‪$validator instanceof ‪NotEmptyValidator) {
137  return true;
138  }
139  }
140  return false;
141  }
142 
149  public function ‪setProperty(string $key, $value)
150  {
151  if (is_array($value) && isset($this->properties[$key]) && is_array($this->properties[$key])) {
152  ‪ArrayUtility::mergeRecursiveWithOverrule($this->properties[$key], $value);
153  $this->properties[$key] = ‪ArrayUtility::removeNullValuesRecursive($this->properties[$key]);
154  } elseif ($value === null) {
155  unset($this->properties[$key]);
156  } else {
157  $this->properties[$key] = $value;
158  }
159  }
160 
166  public function ‪getProperties(): array
167  {
168  return ‪$this->properties;
169  }
170 }
‪TYPO3\CMS\Form\Domain\Model\FormElements\AbstractFormElement
Definition: AbstractFormElement.php:50
‪TYPO3\CMS\Form\Domain\Model\FormElements\AbstractFormElement\setProperty
‪setProperty(string $key, $value)
Definition: AbstractFormElement.php:148
‪TYPO3\CMS\Form\Domain\Exception\IdentifierNotValidException
Definition: IdentifierNotValidException.php:31
‪TYPO3\CMS\Form\Domain\Model\FormElements\AbstractFormElement\getProperties
‪array getProperties()
Definition: AbstractFormElement.php:165
‪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\Renderable\AbstractRenderable
Definition: AbstractRenderable.php:42
‪TYPO3\CMS\Form\Domain\Model\Renderable\AbstractRenderable\$type
‪string $type
Definition: AbstractRenderable.php:50
‪TYPO3\CMS\Form\Domain\Model\FormElements\AbstractFormElement\__construct
‪__construct(string $identifier, string $type)
Definition: AbstractFormElement.php:63
‪TYPO3\CMS\Form\Domain\Model\Renderable\AbstractRenderable\getRootForm
‪FormDefinition getRootForm()
Definition: AbstractRenderable.php:318
‪TYPO3\CMS\Form\Domain\Model\FormElements\FormElementInterface
Definition: FormElementInterface.php:40
‪$validator
‪if(isset($args['d'])) $validator
Definition: validateRstFiles.php:218
‪TYPO3\CMS\Form\Domain\Model\FormElements\AbstractFormElement\getDefaultValue
‪mixed getDefaultValue()
Definition: AbstractFormElement.php:105
‪TYPO3\CMS\Form\Domain\Model\FormElements\AbstractFormElement\isRequired
‪bool isRequired()
Definition: AbstractFormElement.php:132
‪TYPO3\CMS\Core\Utility\ArrayUtility
Definition: ArrayUtility.php:24
‪TYPO3\CMS\Form\Domain\Model\FormElements\AbstractFormElement\getUniqueIdentifier
‪string getUniqueIdentifier()
Definition: AbstractFormElement.php:92
‪$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:54
‪TYPO3\CMS\Form\Domain\Model\FormElements\AbstractFormElement\initializeFormElement
‪initializeFormElement()
Definition: AbstractFormElement.php:75
‪TYPO3\CMS\Core\Utility\ArrayUtility\removeNullValuesRecursive
‪static array removeNullValuesRecursive(array $array)
Definition: ArrayUtility.php:233
‪TYPO3\CMS\Form\Domain\Model\Renderable\AbstractRenderable\$identifier
‪string $identifier
Definition: AbstractRenderable.php:56
‪TYPO3\CMS\Extbase\Validation\Validator\NotEmptyValidator
Definition: NotEmptyValidator.php:22
‪TYPO3\CMS\Core\Utility\GeneralUtility
Definition: GeneralUtility.php:46
‪TYPO3\CMS\Form\Domain\Model\FormElements\FormElementInterface\getValidators
‪SplObjectStorage< ValidatorInterface > getValidators()
‪TYPO3\CMS\Form\Domain\Model\FormElements\AbstractFormElement\setDefaultValue
‪setDefaultValue($defaultValue)
Definition: AbstractFormElement.php:116
‪TYPO3\CMS\Form\Domain\Model\FormElements
Definition: AbstractFormElement.php:22