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