TYPO3 CMS  TYPO3_7-6
ElementBuilder.php
Go to the documentation of this file.
1 <?php
3 
4 /*
5  * This file is part of the TYPO3 CMS project.
6  *
7  * It is free software; you can redistribute it and/or modify it under
8  * the terms of the GNU General Public License, either version 2
9  * of the License, or any later version.
10  *
11  * For the full copyright and license information, please read the
12  * LICENSE.txt file that was distributed with this source code.
13  *
14  * The TYPO3 project - inspiring people to share!
15  */
16 
19 
24 {
31  public static function create(FormBuilder $formBuilder, Element $element, array $userDefinedTypoScript)
32  {
34  $elementBuilder = \TYPO3\CMS\Form\Utility\FormUtility::getObjectManager()->get(self::class);
35  $elementBuilder->setFormBuilder($formBuilder);
36  $elementBuilder->setElement($element);
37  $elementBuilder->setUserConfiguredElementTyposcript($userDefinedTypoScript);
38  return $elementBuilder;
39  }
40 
45 
49  protected $typoScriptService;
50 
55 
59  protected $htmlAttributes = [];
60 
64  protected $additionalArguments = [];
65 
69  protected $wildcardPrefixes = [];
70 
74  protected $formBuilder;
75 
79  protected $element;
80 
85  public function injectTypoScriptRepository(\TYPO3\CMS\Form\Domain\Repository\TypoScriptRepository $typoScriptRepository)
86  {
87  $this->typoScriptRepository = $typoScriptRepository;
88  }
89 
94  public function injectTypoScriptService(\TYPO3\CMS\Extbase\Service\TypoScriptService $typoScriptService)
95  {
96  $this->typoScriptService = $typoScriptService;
97  }
98 
102  public function setFormBuilder(FormBuilder $formBuilder)
103  {
104  $this->formBuilder = $formBuilder;
105  }
106 
110  public function setElement(Element $element)
111  {
112  $this->element = $element;
113  }
114 
120  public function setPartialPaths()
121  {
122  $this->setElementPartialPath();
123  }
124 
130  protected function setElementPartialPath()
131  {
132  if (!isset($this->userConfiguredElementTyposcript['partialPath'])) {
133  $partialPath = $this->typoScriptRepository->getDefaultFluidTemplate($this->element->getElementType());
134  } else {
135  $partialPath = $this->userConfiguredElementTyposcript['partialPath'];
136  unset($this->userConfiguredElementTyposcript['partialPath']);
137  }
138  $this->element->setPartialPath($partialPath);
139  }
140 
146  public function setVisibility()
147  {
148  $visibility = false;
149  if ($this->formBuilder->getControllerAction() === 'show') {
150  if (!isset($this->userConfiguredElementTyposcript['visibleInShowAction'])) {
151  $visibility = (bool)$this->typoScriptRepository->getModelConfigurationByScope($this->element->getElementType(), 'visibleInShowAction');
152  } else {
153  $visibility = (bool)$this->userConfiguredElementTyposcript['visibleInShowAction'];
154  }
155  } elseif ($this->formBuilder->getControllerAction() === 'confirmation') {
156  if (!isset($this->userConfiguredElementTyposcript['visibleInConfirmationAction'])) {
157  $visibility = (bool)$this->typoScriptRepository->getModelConfigurationByScope($this->element->getElementType(), 'visibleInConfirmationAction');
158  } else {
159  $visibility = (bool)$this->userConfiguredElementTyposcript['visibleInConfirmationAction'];
160  }
161  } elseif ($this->formBuilder->getControllerAction() === 'process') {
162  if (!isset($this->userConfiguredElementTyposcript['visibleInMail'])) {
163  $visibility = (bool)$this->typoScriptRepository->getModelConfigurationByScope($this->element->getElementType(), 'visibleInMail');
164  } else {
165  $visibility = (bool)$this->userConfiguredElementTyposcript['visibleInMail'];
166  }
167  }
168  $this->element->setShowElement($visibility);
169  }
170 
177  public function setHtmlAttributeWildcards()
178  {
179  foreach ($this->htmlAttributes as $attributeName => $attributeValue) {
180  if (strpos($attributeName, '-*') > 0) {
181  $prefix = substr($attributeName, 0, -1);
182  $this->wildcardPrefixes[] = $prefix;
183  unset($this->htmlAttributes[$attributeName]);
184  }
185  }
186  }
187 
197  {
198  foreach ($this->htmlAttributes as $attributeName => $attributeValue) {
199  $attributeNameWithoutDot = rtrim($attributeName, '.');
200  $attributeNameToSet = $attributeNameWithoutDot;
201  $rendered = false;
202  /* If the attribute exists in the user configured typoscript */
203  if ($this->arrayKeyExists($attributeName, $this->userConfiguredElementTyposcript)) {
204  if ($this->formBuilder->getConfiguration()->getCompatibility()) {
205  $newAttributeName = $this->formBuilder->getCompatibilityService()->getNewAttributeName(
206  $this->element->getElementType(),
207  $attributeNameWithoutDot
208  );
209  /* Should the attribute be renamed? */
210  if ($newAttributeName !== $attributeNameWithoutDot) {
211  $attributeNameToSet = $newAttributeName;
212  /* If the renamed attribute already exists in the user configured typoscript */
213  if ($this->arrayKeyExists($newAttributeName, $this->userConfiguredElementTyposcript)) {
214  $attributeValue = $this->formBuilder->getFormUtility()->renderItem(
215  $this->userConfiguredElementTyposcript[$newAttributeName . '.'],
216  $this->userConfiguredElementTyposcript[$newAttributeName]
217  );
218  /* set renamed attribute name with the value of the renamed attribute */
219  $this->htmlAttributes[$newAttributeName] = $attributeValue;
220  /* unset the renamed attribute */
221  unset($this->userConfiguredElementTyposcript[$newAttributeName . '.']);
222  unset($this->userConfiguredElementTyposcript[$newAttributeName]);
223  $rendered = true;
224  }
225  }
226  }
227  }
228  if ($rendered === false) {
229  if ($this->arrayKeyExists($attributeNameWithoutDot, $this->userConfiguredElementTyposcript)) {
230  $attributeValue = $this->formBuilder->getFormUtility()->renderItem(
231  $this->userConfiguredElementTyposcript[$attributeNameWithoutDot . '.'],
232  $this->userConfiguredElementTyposcript[$attributeNameWithoutDot]
233  );
234  $this->htmlAttributes[$attributeNameToSet] = $attributeValue;
235  }
236  }
237  unset($this->userConfiguredElementTyposcript[$attributeNameWithoutDot . '.']);
238  unset($this->userConfiguredElementTyposcript[$attributeNameWithoutDot]);
239  }
240 
241  // the prefix-* magic
242  $ignoreKeys = [];
243  foreach ($this->userConfiguredElementTyposcript as $attributeName => $attributeValue) {
244  // ignore child elements
245  if (
247  || isset($ignoreKeys[$attributeName])
248  ) {
249  $ignoreKeys[$attributeName . '.'] = true;
250  continue;
251  }
252 
253  foreach ($this->wildcardPrefixes as $wildcardPrefix) {
254  if (strpos($attributeName, $wildcardPrefix) !== 0) {
255  continue;
256  }
257  $attributeNameWithoutDot = rtrim($attributeName, '.');
258  $attributeValue = $this->formBuilder->getFormUtility()->renderItem(
259  $this->userConfiguredElementTyposcript[$attributeNameWithoutDot . '.'],
260  $this->userConfiguredElementTyposcript[$attributeNameWithoutDot]
261  );
262  $this->htmlAttributes[$attributeNameWithoutDot] = $attributeValue;
263  $ignoreKeys[$attributeNameWithoutDot . '.'] = true;
264  unset($this->userConfiguredElementTyposcript[$attributeNameWithoutDot . '.']);
265  unset($this->userConfiguredElementTyposcript[$attributeNameWithoutDot]);
266  break;
267  }
268  }
269  }
270 
278  {
279  $fixedHtmlAttributeValues = $this->typoScriptRepository->getModelConfigurationByScope($this->element->getElementType(), 'fixedHtmlAttributeValues.');
280  if (is_array($fixedHtmlAttributeValues)) {
281  foreach ($fixedHtmlAttributeValues as $attributeName => $attributeValue) {
282  $this->htmlAttributes[$attributeName] = $attributeValue;
283  }
284  }
285  }
286 
294  {
295  $htmlAttributesUsedByTheViewHelperDirectly = $this->typoScriptRepository->getModelConfigurationByScope($this->element->getElementType(), 'htmlAttributesUsedByTheViewHelperDirectly.');
296  if (is_array($htmlAttributesUsedByTheViewHelperDirectly)) {
297  foreach ($htmlAttributesUsedByTheViewHelperDirectly as $attributeName) {
298  if (array_key_exists($attributeName, $this->htmlAttributes)) {
299  $this->additionalArguments[$attributeName] = $this->htmlAttributes[$attributeName];
300  unset($this->htmlAttributes[$attributeName]);
301  }
302  }
303  }
304  }
305 
312  {
313  $viewHelperDefaultArguments = $this->typoScriptRepository->getModelConfigurationByScope($this->element->getElementType(), 'viewHelperDefaultArguments.');
314  if (is_array($viewHelperDefaultArguments)) {
315  foreach ($viewHelperDefaultArguments as $viewHelperDefaulArgumentName => $viewHelperDefaulArgumentValue) {
316  $viewHelperDefaulArgumentNameWithoutDot = rtrim($viewHelperDefaulArgumentName, '.');
317  $this->additionalArguments[$viewHelperDefaulArgumentNameWithoutDot] = $viewHelperDefaulArgumentValue;
318  }
319  }
320  unset($this->userConfiguredElementTyposcript['viewHelperDefaultArguments']);
321  }
322 
330  {
331  $viewHelperDefaultArguments = $this->typoScriptRepository->getModelConfigurationByScope($this->element->getElementType(), 'viewHelperDefaultArguments.');
332  $ignoreKeys = [];
333 
334  foreach ($this->userConfiguredElementTyposcript as $attributeName => $attributeValue) {
335  // ignore child elements
336  if (
338  || isset($ignoreKeys[$attributeName])
339  || $attributeName == 'postProcessor.'
340  || $attributeName == 'rules.'
341  || $attributeName == 'filters.'
342  || $attributeName == 'layout'
343  ) {
344  $ignoreKeys[$attributeName . '.'] = true;
345  continue;
346  }
347  $attributeNameWithoutDot = rtrim($attributeName, '.');
348  $attributeNameToSet = $attributeNameWithoutDot;
349  $rendered = false;
350  if ($this->formBuilder->getConfiguration()->getCompatibility()) {
351  $newAttributeName = $this->formBuilder->getCompatibilityService()->getNewAttributeName(
352  $this->element->getElementType(),
353  $attributeNameWithoutDot
354  );
355  /* Should the attribute be renamed? */
356  if ($newAttributeName !== $attributeNameWithoutDot) {
357  $attributeNameToSet = $newAttributeName;
358  /* If the renamed attribute already exists in the user configured typoscript */
359  if ($this->arrayKeyExists($newAttributeName, $this->userConfiguredElementTyposcript)) {
360  $attributeValue = $this->formBuilder->getFormUtility()->renderItem(
361  $this->userConfiguredElementTyposcript[$newAttributeName . '.'],
362  $this->userConfiguredElementTyposcript[$newAttributeName]
363  );
364  /* set renamed attribute name with the value of the renamed attribute */
365  $this->additionalArguments[$newAttributeName] = $attributeValue;
366  /* unset the renamed attribute */
367  $ignoreKeys[$newAttributeName . '.'] = true;
368  $ignoreKeys[$newAttributeName] = true;
369  unset($this->userConfiguredElementTyposcript[$newAttributeName . '.']);
370  unset($this->userConfiguredElementTyposcript[$newAttributeName]);
371  $rendered = true;
372  }
373  }
374  }
375  if ($rendered === false) {
376  if (
377  isset($viewHelperDefaultArguments[$attributeName])
378  || isset($viewHelperDefaultArguments[$attributeNameWithoutDot])
379  ) {
380  $this->formBuilder->getFormUtility()->renderArrayItems($attributeValue);
381  $attributeValue = $this->typoScriptService->convertTypoScriptArrayToPlainArray($attributeValue);
382  } else {
383  $attributeValue = $this->formBuilder->getFormUtility()->renderItem(
384  $this->userConfiguredElementTyposcript[$attributeNameWithoutDot . '.'],
385  $this->userConfiguredElementTyposcript[$attributeNameWithoutDot]
386  );
387  }
388  $this->additionalArguments[$attributeNameToSet] = $attributeValue;
389  $ignoreKeys[$attributeNameToSet . '.'] = true;
390  $ignoreKeys[$attributeNameToSet] = true;
391  }
392  unset($this->userConfiguredElementTyposcript[$attributeNameWithoutDot . '.']);
393  unset($this->userConfiguredElementTyposcript[$attributeNameWithoutDot]);
394  }
395  // remove "stdWrap." from "additionalArguments" on
396  // the FORM Element
397  if (
398  !$this->formBuilder->getConfiguration()->getContentElementRendering()
399  && $this->element->getElementType() == 'FORM'
400  ) {
401  unset($this->additionalArguments['stdWrap']);
402  unset($this->additionalArguments['stdWrap.']);
403  }
404  }
405 
411  public function setNameAndId()
412  {
413  if (
414  $this->element->getParentElement()
415  && (int)$this->typoScriptRepository->getModelConfigurationByScope($this->element->getParentElement()->getElementType(), 'childrenInheritName') == 1
416  ) {
417  $this->htmlAttributes['name'] = $this->element->getParentElement()->getName();
418  $this->additionalArguments['multiple'] = '1';
419  $name = $this->sanitizeNameAttribute($this->userConfiguredElementTyposcript['name']);
420  $this->element->setName($name);
421  } else {
422  $this->htmlAttributes['name'] = $this->sanitizeNameAttribute($this->htmlAttributes['name']);
423  $this->element->setName($this->htmlAttributes['name']);
424  }
425  $this->htmlAttributes['id'] = $this->sanitizeIdAttribute($this->htmlAttributes['id']);
426  $this->element->setId($this->htmlAttributes['id']);
427  }
428 
440  public function sanitizeNameAttribute($name)
441  {
442  $name = $this->formBuilder->getFormUtility()->sanitizeNameAttribute($name);
443  if (empty($name)) {
444  $name = 'id-' . $this->element->getElementCounter();
445  }
446  return $name;
447  }
448 
461  protected function sanitizeIdAttribute($id)
462  {
463  $id = $this->formBuilder->getFormUtility()->sanitizeIdAttribute($id);
464  if (empty($id)) {
465  $id = 'field-' . $this->element->getElementCounter();
466  }
467  return $id;
468  }
469 
477  protected function arrayKeyExists($needle, array $haystack = [])
478  {
479  return
480  isset($haystack[$needle]) || isset($haystack[$needle . '.'])
481  ;
482  }
483 
489  public function getHtmlAttributes()
490  {
491  return $this->htmlAttributes;
492  }
493 
499  public function setHtmlAttributes(array $htmlAttributes)
500  {
501  $this->htmlAttributes = $htmlAttributes;
502  }
503 
509  public function getAdditionalArguments()
510  {
512  }
513 
520  {
521  $this->additionalArguments = $additionalArguments;
522  }
523 
529  public function getWildcardPrefixes()
530  {
532  }
533 
539  public function setWildcardPrefixes(array $wildcardPrefixes)
540  {
541  $this->wildcardPrefixes = $wildcardPrefixes;
542  }
543 
550  {
552  }
553 
560  {
561  $this->userConfiguredElementTyposcript = $userConfiguredElementTyposcript;
562  }
563 }
arrayKeyExists($needle, array $haystack=[])
setAdditionalArguments(array $additionalArguments)
setUserConfiguredElementTyposcript(array $userConfiguredElementTyposcript)
injectTypoScriptService(\TYPO3\CMS\Extbase\Service\TypoScriptService $typoScriptService)
injectTypoScriptRepository(\TYPO3\CMS\Form\Domain\Repository\TypoScriptRepository $typoScriptRepository)