‪TYPO3CMS  ‪main
FieldControl.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 
19 
23 use TYPO3\CMS\Core\Imaging\IconSize;
27 
42 {
43  public function ‪__construct(
44  private readonly ‪NodeFactory $nodeFactory,
45  private readonly ‪IconFactory $iconFactory,
46  private readonly ‪DependencyOrderingService $dependencyOrderingService,
47  ) {}
48 
56  public function ‪render(): array
57  {
58  $result = $this->‪initializeResultArray();
59  if (!isset($this->data['renderData']['fieldControl'])) {
60  return $result;
61  }
62 
63  $languageService = $this->‪getLanguageService();
64 
65  $fieldControl = $this->data['renderData']['fieldControl'];
66  $orderedFieldControl = $this->dependencyOrderingService->orderByDependencies($fieldControl);
67 
68  foreach ($orderedFieldControl as $anOrderedFieldControl => $orderedFieldControlConfiguration) {
69  if (isset($orderedFieldControlConfiguration['disabled']) && $orderedFieldControlConfiguration['disabled']
70  || !isset($fieldControl[$anOrderedFieldControl]['renderType'])
71  ) {
72  // Don't consider this control if disabled.
73  // Also ignore if renderType is not given.
74  // Missing renderType may happen if an element registers a default field control
75  // as disabled, and TCA enabled that. If then additionally for instance the
76  // element renderType is changed to an element that doesn't register the control
77  // by default anymore, this would then fatal if we don't continue here.
78  // @todo: the above scenario indicates a small configuration flaw, maybe log an error somewhere?
79  continue;
80  }
81 
82  $options = ‪$this->data;
83  $options['renderType'] = $fieldControl[$anOrderedFieldControl]['renderType'];
84  $options['renderData']['fieldControlOptions'] = $orderedFieldControlConfiguration['options'] ?? [];
85  $controlResult = $this->nodeFactory->create($options)->render();
86 
87  // If the controlResult is empty (this control rendered nothing), continue to next one
88  if (empty($controlResult)) {
89  continue;
90  }
91 
92  if (empty($controlResult['iconIdentifier'])) {
93  throw new \RuntimeException(
94  'Field controls must return an iconIdentifier',
95  1483890332
96  );
97  }
98  if (empty($controlResult['title'])) {
99  throw new \RuntimeException(
100  'Field controls must return a title',
101  1483890482
102  );
103  }
104  if (empty($controlResult['linkAttributes'])) {
105  throw new \RuntimeException(
106  'Field controls must return link attributes',
107  1483891272
108  );
109  }
110 
111  $icon = $controlResult['iconIdentifier'];
112  $title = $languageService->sL($controlResult['title']);
113  $linkAttributes = $controlResult['linkAttributes'];
114  if (!isset($linkAttributes['class'])) {
115  $linkAttributes['class'] = 'btn btn-default';
116  } else {
117  $linkAttributes['class'] .= ' btn btn-default';
118  }
119  if (!isset($linkAttributes['href'])) {
120  $linkAttributes['href'] = '#';
121  }
122 
123  unset($controlResult['iconIdentifier']);
124  unset($controlResult['title']);
125  unset($controlResult['linkAttributes']);
126 
127  $html = [];
128  $html[] = '<a ' . GeneralUtility::implodeAttributes($linkAttributes, true) . '>';
129  $html[] = $this->iconFactory->getIcon($icon, IconSize::SMALL)->setTitle($title)->render();
130  $html[] = '</a>';
131 
132  $finalControlResult = $this->‪initializeResultArray();
133  $finalControlResult = array_merge($finalControlResult, $controlResult);
134  $finalControlResult['html'] = implode(LF, $html);
135 
136  $result = $this->‪mergeChildReturnIntoExistingResult($result, $finalControlResult);
137  }
138  return $result;
139  }
140 
142  {
143  return ‪$GLOBALS['LANG'];
144  }
145 }
‪TYPO3\CMS\Backend\Form\AbstractNode\mergeChildReturnIntoExistingResult
‪array mergeChildReturnIntoExistingResult(array $existing, array $childReturn, bool $mergeHtml=true)
Definition: AbstractNode.php:104
‪TYPO3\CMS\Backend\Form\NodeExpansion\FieldControl
Definition: FieldControl.php:42
‪TYPO3\CMS\Core\Imaging\IconFactory
Definition: IconFactory.php:34
‪TYPO3\CMS\Backend\Form\NodeExpansion\FieldControl\render
‪array render()
Definition: FieldControl.php:56
‪TYPO3\CMS\Backend\Form\AbstractNode\$data
‪array $data
Definition: AbstractNode.php:35
‪TYPO3\CMS\Core\Service\DependencyOrderingService
Definition: DependencyOrderingService.php:32
‪TYPO3\CMS\Backend\Form\NodeExpansion
Definition: FieldControl.php:18
‪TYPO3\CMS\Backend\Form\NodeFactory
Definition: NodeFactory.php:40
‪$GLOBALS
‪$GLOBALS['TYPO3_CONF_VARS']['EXTCONF']['adminpanel']['modules']
Definition: ext_localconf.php:25
‪TYPO3\CMS\Core\Localization\LanguageService
Definition: LanguageService.php:46
‪TYPO3\CMS\Backend\Form\AbstractNode
Definition: AbstractNode.php:29
‪TYPO3\CMS\Backend\Form\NodeExpansion\FieldControl\__construct
‪__construct(private readonly NodeFactory $nodeFactory, private readonly IconFactory $iconFactory, private readonly DependencyOrderingService $dependencyOrderingService,)
Definition: FieldControl.php:43
‪TYPO3\CMS\Backend\Form\NodeExpansion\FieldControl\getLanguageService
‪getLanguageService()
Definition: FieldControl.php:141
‪TYPO3\CMS\Core\Utility\GeneralUtility
Definition: GeneralUtility.php:52
‪TYPO3\CMS\Backend\Form\AbstractNode\initializeResultArray
‪initializeResultArray()
Definition: AbstractNode.php:77