‪TYPO3CMS  ‪main
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 {
54  protected ‪$properties = [];
55 
63  public function ‪__construct(string ‪$identifier, string ‪$type)
64  {
65  if (!is_string(‪$identifier) || strlen(‪$identifier) === 0) {
66  throw new ‪IdentifierNotValidException('The given identifier was not a string or the string was empty.', 1477082502);
67  }
68  $this->identifier = ‪$identifier;
69  $this->type = ‪$type;
70  }
71 
75  public function ‪initializeFormElement()
76  {
77  foreach (‪$GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['ext/form']['initializeFormElement'] ?? [] as $className) {
78  $hookObj = GeneralUtility::makeInstance($className);
79  if (method_exists($hookObj, 'initializeFormElement')) {
80  $hookObj->initializeFormElement(
81  $this
82  );
83  }
84  }
85  }
86 
90  public function ‪getUniqueIdentifier(): string
91  {
92  $formDefinition = $this->‪getRootForm();
93  $uniqueIdentifier = sprintf('%s-%s', $formDefinition->getIdentifier(), $this->identifier);
94  $uniqueIdentifier = (string)preg_replace('/[^a-zA-Z0-9_-]/', '_', $uniqueIdentifier);
95  return lcfirst($uniqueIdentifier);
96  }
97 
98  public function ‪setOptions(array $options, bool $resetValidators = false)
99  {
100  if (isset($options['defaultValue'])) {
101  $this->‪setDefaultValue($options['defaultValue']);
102  }
103 
104  if (isset($options['properties'])) {
105  foreach ($options['properties'] as $key => $value) {
106  $this->‪setProperty($key, $value);
107  }
108  }
109 
110  parent::setOptions($options, $resetValidators);
111  }
112 
118  public function ‪getDefaultValue()
119  {
120  $formDefinition = $this->‪getRootForm();
121  return $formDefinition->getElementDefaultValueByIdentifier($this->identifier);
122  }
123 
129  public function ‪setDefaultValue($defaultValue)
130  {
131  $formDefinition = $this->‪getRootForm();
132  $currentDefaultValue = $formDefinition->getElementDefaultValueByIdentifier($this->identifier);
133  if (is_array($currentDefaultValue) && is_array($defaultValue)) {
134  ArrayUtility::mergeRecursiveWithOverrule($currentDefaultValue, $defaultValue);
135  $defaultValue = ‪ArrayUtility::removeNullValuesRecursive($currentDefaultValue);
136  }
137  $formDefinition->addElementDefaultValue($this->identifier, $defaultValue);
138  }
139 
143  public function ‪isRequired(): bool
144  {
145  foreach ($this->‪getValidators() as ‪$validator) {
146  if (‪$validator instanceof NotEmptyValidator) {
147  return true;
148  }
149  }
150  return false;
151  }
152 
158  public function ‪setProperty(string $key, $value)
159  {
160  if (is_array($value) && isset($this->properties[$key]) && is_array($this->properties[$key])) {
161  ArrayUtility::mergeRecursiveWithOverrule($this->properties[$key], $value);
162  $this->properties[$key] = ‪ArrayUtility::removeNullValuesRecursive($this->properties[$key]);
163  } elseif ($value === null) {
164  unset($this->properties[$key]);
165  } else {
166  $this->properties[$key] = $value;
167  }
168  }
169 
173  public function ‪getProperties(): array
174  {
175  return ‪$this->properties;
176  }
177 }
‪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:157
‪TYPO3\CMS\Form\Domain\Exception\IdentifierNotValidException
Definition: IdentifierNotValidException.php:30
‪TYPO3\CMS\Form\Domain\Model\FormElements\AbstractFormElement\getProperties
‪getProperties()
Definition: AbstractFormElement.php:172
‪TYPO3\CMS\Form\Domain\Model\Renderable\AbstractRenderable
Definition: AbstractRenderable.php:42
‪TYPO3\CMS\Form\Domain\Model\FormElements\AbstractFormElement\isRequired
‪isRequired()
Definition: AbstractFormElement.php:142
‪TYPO3\CMS\Form\Domain\Model\Renderable\AbstractRenderable\$type
‪string $type
Definition: AbstractRenderable.php:49
‪TYPO3\CMS\Form\Domain\Model\FormElements\AbstractFormElement\__construct
‪__construct(string $identifier, string $type)
Definition: AbstractFormElement.php:62
‪TYPO3\CMS\Core\Utility\ArrayUtility\removeNullValuesRecursive
‪static removeNullValuesRecursive(array $array)
Definition: ArrayUtility.php:222
‪TYPO3\CMS\Form\Domain\Model\FormElements\AbstractFormElement\setOptions
‪setOptions(array $options, bool $resetValidators=false)
Definition: AbstractFormElement.php:97
‪TYPO3\CMS\Form\Domain\Model\Renderable\AbstractRenderable\getRootForm
‪getRootForm()
Definition: AbstractRenderable.php:286
‪TYPO3\CMS\Form\Domain\Model\FormElements\FormElementInterface
Definition: FormElementInterface.php:40
‪$validator
‪if(isset($args['d'])) $validator
Definition: validateRstFiles.php:262
‪TYPO3\CMS\Form\Domain\Model\FormElements\AbstractFormElement\getDefaultValue
‪mixed getDefaultValue()
Definition: AbstractFormElement.php:117
‪TYPO3\CMS\Form\Domain\Model\FormElements\AbstractFormElement\getUniqueIdentifier
‪getUniqueIdentifier()
Definition: AbstractFormElement.php:89
‪TYPO3\CMS\Core\Utility\ArrayUtility
Definition: ArrayUtility.php:26
‪$GLOBALS
‪$GLOBALS['TYPO3_CONF_VARS']['EXTCONF']['adminpanel']['modules']
Definition: ext_localconf.php:25
‪TYPO3\CMS\Form\Domain\Model\FormElements\AbstractFormElement\$properties
‪array $properties
Definition: AbstractFormElement.php:53
‪TYPO3\CMS\Form\Domain\Model\FormElements\AbstractFormElement\initializeFormElement
‪initializeFormElement()
Definition: AbstractFormElement.php:74
‪TYPO3\CMS\Form\Domain\Model\Renderable\AbstractRenderable\$identifier
‪string $identifier
Definition: AbstractRenderable.php:55
‪TYPO3\CMS\Extbase\Validation\Validator\NotEmptyValidator
Definition: NotEmptyValidator.php:24
‪TYPO3\CMS\Core\Utility\GeneralUtility
Definition: GeneralUtility.php:52
‪TYPO3\CMS\Form\Domain\Model\FormElements\FormElementInterface\getValidators
‪SplObjectStorage< ValidatorInterface > getValidators()
‪TYPO3\CMS\Form\Domain\Model\FormElements\AbstractFormElement\setDefaultValue
‪setDefaultValue($defaultValue)
Definition: AbstractFormElement.php:128
‪TYPO3\CMS\Form\Domain\Model\FormElements
Definition: AbstractFormElement.php:22