[BUGFIX] Fix path to language file in SelectTreeElement
[Packages/TYPO3.CMS.git] / typo3 / sysext / backend / Classes / Form / NodeFactory.php
1 <?php
2 namespace TYPO3\CMS\Backend\Form;
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
17 use TYPO3\CMS\Core\Utility\GeneralUtility;
18
19 /**
20 * Create an element object depending on renderType.
21 *
22 * This is the main factory to instantiate any node within the render
23 * chain of FormEngine. All nodes must implement NodeInterface.
24 *
25 * Nodes are "container" classes of the render chain, "element" classes that
26 * render single elements, as well as "fieldWizard", "fieldInformation" and
27 * "fieldControl" classes which are called by single elements to enrich them.
28 *
29 * This factory gets a string "renderType" and then looks up in a list which
30 * specific class should handle this renderType. This list can be extended with
31 * own renderTypes by extensions, existing renderTypes can be overridden, and
32 * - for complex cases - it is possible to register own resolver classes for single
33 * renderTypes that can return a node class name to override the default lookup list.
34 */
35 class NodeFactory
36 {
37 /**
38 * Node resolver classes
39 * Nested array with nodeName as key, (sorted) priority as sub key and class as value
40 *
41 * @var array
42 */
43 protected $nodeResolver = [];
44
45 /**
46 * Default registry of node name to handling class
47 *
48 * @var array
49 */
50 protected $nodeTypes = [
51 // Default container classes
52 'flex' => Container\FlexFormEntryContainer::class,
53 'flexFormContainerContainer' => Container\FlexFormContainerContainer::class,
54 'flexFormElementContainer' => Container\FlexFormElementContainer::class,
55 'flexFormNoTabsContainer' => Container\FlexFormNoTabsContainer::class,
56 'flexFormSectionContainer' => Container\FlexFormSectionContainer::class,
57 'flexFormTabsContainer' => Container\FlexFormTabsContainer::class,
58 'fullRecordContainer' => Container\FullRecordContainer::class,
59 'inline' => Container\InlineControlContainer::class,
60 'inlineRecordContainer' => Container\InlineRecordContainer::class,
61 'listOfFieldsContainer' => Container\ListOfFieldsContainer::class,
62 'noTabsContainer' => Container\NoTabsContainer::class,
63 'outerWrapContainer' => Container\OuterWrapContainer::class,
64 'paletteAndSingleContainer' => Container\PaletteAndSingleContainer::class,
65 'singleFieldContainer' => Container\SingleFieldContainer::class,
66 'tabsContainer' => Container\TabsContainer::class,
67
68 // Default single element classes
69 'check' => Element\CheckboxElement::class,
70 'group' => Element\GroupElement::class,
71 'input' => Element\InputTextElement::class,
72 'inputDateTime' => Element\InputDateTimeElement::class,
73 'inputLink' => Element\InputLinkElement::class,
74 'hidden' => Element\InputHiddenElement::class,
75 // rsaInput is defined with a fallback so extensions can use it even if ext:rsaauth is not loaded
76 'rsaInput' => Element\InputTextElement::class,
77 'imageManipulation' => Element\ImageManipulationElement::class,
78 'none' => Element\NoneElement::class,
79 'radio' => Element\RadioElement::class,
80 'selectCheckBox' => Element\SelectCheckBoxElement::class,
81 'selectMultipleSideBySide' => Element\SelectMultipleSideBySideElement::class,
82 'selectTree' => Element\SelectTreeElement::class,
83 'selectSingle' => Element\SelectSingleElement::class,
84 'selectSingleBox' => Element\SelectSingleBoxElement::class,
85 'colorpicker' => Element\InputColorPickerElement::class,
86 // t3editor is defined with a fallback so extensions can use it even if ext:t3editor is not loaded
87 't3editor' => Element\TextElement::class,
88 'text' => Element\TextElement::class,
89 'textTable' => Element\TextTableElement::class,
90 'unknown' => Element\UnknownElement::class,
91 'user' => Element\UserElement::class,
92 'fileInfo' => Element\FileInfoElement::class,
93
94 // Default classes to enrich single elements
95 'fieldControl' => NodeExpansion\FieldControl::class,
96 'fieldInformation' => NodeExpansion\FieldInformation::class,
97 'fieldWizard' => NodeExpansion\FieldWizard::class,
98
99 // Element wizards
100 'defaultLanguageDifferences' => FieldWizard\DefaultLanguageDifferences::class,
101 'fileThumbnails' => FieldWizard\FileThumbnails::class,
102 'fileTypeList' => FieldWizard\FileTypeList::class,
103 'fileUpload' => FieldWizard\FileUpload::class,
104 'localizationStateSelector' => FieldWizard\LocalizationStateSelector::class,
105 'otherLanguageContent' => FieldWizard\OtherLanguageContent::class,
106 'recordsOverview' => FieldWizard\RecordsOverview::class,
107 'selectIcons' => FieldWizard\SelectIcons::class,
108 'tableList' => FieldWizard\TableList::class,
109
110 // Element controls
111 'addRecord' => FieldControl\AddRecord::class,
112 'editPopup' => FieldControl\EditPopup::class,
113 'elementBrowser' => FieldControl\ElementBrowser::class,
114 'insertClipboard' => FieldControl\InsertClipboard::class,
115 'linkPopup' => FieldControl\LinkPopup::class,
116 'listModule' => FieldControl\ListModule::class,
117 'resetSelection' => FieldControl\ResetSelection::class,
118 'tableWizard' => FieldControl\TableWizard::class,
119 ];
120
121 /**
122 * Set up factory. Initialize additionally registered nodes.
123 */
124 public function __construct()
125 {
126 $this->registerAdditionalNodeTypesFromConfiguration();
127 $this->initializeNodeResolver();
128 }
129
130 /**
131 * Create a node depending on type
132 *
133 * @param array $data All information to decide which class should be instantiated and given down to sub nodes
134 * @return AbstractNode
135 * @throws Exception
136 */
137 public function create(array $data)
138 {
139 if (empty($data['renderType'])) {
140 throw new Exception(
141 'Missing "renderType" in TCA of field "[' . $data['tableName'] . '][' . $data['fieldName'] . ']".',
142 1431452406
143 );
144 }
145 $type = $data['renderType'];
146
147 $className = isset($this->nodeTypes[$type]) ? $this->nodeTypes[$type] : $this->nodeTypes['unknown'];
148
149 if (!empty($this->nodeResolver[$type])) {
150 // Resolver with highest priority is called first. If it returns with a new class name,
151 // it will be taken and loop is aborted, otherwise resolver with next lower priority is called.
152 foreach ($this->nodeResolver[$type] as $priority => $resolverClassName) {
153 /** @var NodeResolverInterface $resolver */
154 $resolver = $this->instantiate($resolverClassName, $data);
155 if (!$resolver instanceof NodeResolverInterface) {
156 throw new Exception(
157 'Node resolver for type ' . $type . ' at priority ' . $priority . ' must implement NodeResolverInterface',
158 1433157422
159 );
160 }
161 // Resolver classes do NOT receive the name of the already resolved class. Single
162 // resolvers should not have dependencies to each other or the default implementation,
163 // so they also shouldn't know the output of a different resolving class.
164 // Additionally, the globalOptions array is NOT given by reference here, changing config is a
165 // task of container classes alone and must not be abused here.
166 $newClassName = $resolver->resolve();
167 if ($newClassName !== null) {
168 $className = $newClassName;
169 break;
170 }
171 }
172 }
173
174 /** @var AbstractNode $nodeInstance */
175 $nodeInstance = $this->instantiate($className, $data);
176 if (!$nodeInstance instanceof NodeInterface) {
177 throw new Exception('Node of type ' . get_class($nodeInstance) . ' must implement NodeInterface', 1431872546);
178 }
179 return $nodeInstance;
180 }
181
182 /**
183 * Add node types from nodeRegistry to $this->nodeTypes.
184 * This can be used to add new render types or to overwrite existing node types. The registered class must
185 * implement the NodeInterface and will be called if a node with this renderType is rendered.
186 *
187 * @throws Exception if configuration is incomplete or two nodes with identical priorities are registered
188 */
189 protected function registerAdditionalNodeTypesFromConfiguration()
190 {
191 // List of additional or override nodes
192 $registeredTypeOverrides = $GLOBALS['TYPO3_CONF_VARS']['SYS']['formEngine']['nodeRegistry'];
193 // Sanitize input array
194 $registeredPrioritiesForNodeNames = [];
195 foreach ($registeredTypeOverrides as $override) {
196 if (!isset($override['nodeName']) || !isset($override['class']) || !isset($override['priority'])) {
197 throw new Exception(
198 'Key class, nodeName or priority missing for an entry in $GLOBALS[\'TYPO3_CONF_VARS\'][\'SYS\'][\'formEngine\'][\'nodeRegistry\']',
199 1432207533
200 );
201 }
202 if ($override['priority'] < 0 || $override['priority'] > 100) {
203 throw new Exception(
204 'Priority of element ' . $override['nodeName'] . ' with class ' . $override['class'] . ' is ' . $override['priority'] . ', but must between 0 and 100',
205 1432223531
206 );
207 }
208 if (isset($registeredPrioritiesForNodeNames[$override['nodeName']][$override['priority']])) {
209 throw new Exception(
210 'Element ' . $override['nodeName'] . ' already has an override registered with priority ' . $override['priority'],
211 1432223893
212 );
213 }
214 $registeredPrioritiesForNodeNames[$override['nodeName']][$override['priority']] = '';
215 }
216 // Add element with highest priority to registry
217 $highestPriority = [];
218 foreach ($registeredTypeOverrides as $override) {
219 if (!isset($highestPriority[$override['nodeName']]) || $override['priority'] > $highestPriority[$override['nodeName']]) {
220 $highestPriority[$override['nodeName']] = $override['priority'];
221 $this->nodeTypes[$override['nodeName']] = $override['class'];
222 }
223 }
224 }
225
226 /**
227 * Add resolver and add them sorted to a local property.
228 * This can be used to manipulate the nodeName to class resolution with own code.
229 *
230 * @throws Exception if configuration is incomplete or two resolver with identical priorities are registered
231 */
232 protected function initializeNodeResolver()
233 {
234 // List of node resolver
235 $registeredNodeResolvers = $GLOBALS['TYPO3_CONF_VARS']['SYS']['formEngine']['nodeResolver'];
236 $resolversByType = [];
237 foreach ($registeredNodeResolvers as $nodeResolver) {
238 if (!isset($nodeResolver['nodeName']) || !isset($nodeResolver['class']) || !isset($nodeResolver['priority'])) {
239 throw new Exception(
240 'Key class, nodeName or priority missing for an entry in $GLOBALS[\'TYPO3_CONF_VARS\'][\'SYS\'][\'formEngine\'][\'nodeResolver\']',
241 1433155522
242 );
243 }
244 if ($nodeResolver['priority'] < 0 || $nodeResolver['priority'] > 100) {
245 throw new Exception(
246 'Priority of element ' . $nodeResolver['nodeName'] . ' with class ' . $nodeResolver['class'] . ' is ' . $nodeResolver['priority'] . ', but must between 0 and 100',
247 1433155563
248 );
249 }
250 if (isset($resolversByType[$nodeResolver['nodeName']][$nodeResolver['priority']])) {
251 throw new Exception(
252 'Element ' . $nodeResolver['nodeName'] . ' already has a resolver registered with priority ' . $nodeResolver['priority'],
253 1433155705
254 );
255 }
256 $resolversByType[$nodeResolver['nodeName']][$nodeResolver['priority']] = $nodeResolver['class'];
257 }
258 $sortedResolversByType = [];
259 foreach ($resolversByType as $nodeName => $prioritiesAndClasses) {
260 krsort($prioritiesAndClasses);
261 $sortedResolversByType[$nodeName] = $prioritiesAndClasses;
262 }
263 $this->nodeResolver = $sortedResolversByType;
264 }
265
266 /**
267 * Instantiate given class name
268 *
269 * @param string $className Given class name
270 * @param array $data Main data array
271 * @return object
272 */
273 protected function instantiate($className, array $data)
274 {
275 return GeneralUtility::makeInstance($className, $this, $data);
276 }
277 }