[TASK] Cleanup TCA select field configuration
[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 use TYPO3\CMS\Backend\Form\Container;
19 use TYPO3\CMS\Backend\Form\Element;
20
21 /**
22 * Create an element object depending on type.
23 */
24 class NodeFactory {
25
26 /**
27 * Default registry of node name to handling class
28 *
29 * @var array
30 */
31 protected $nodeTypes = array(
32 'flex' => Container\FlexFormEntryContainer::class,
33 'flexFormContainerContainer' => Container\FlexFormContainerContainer::class,
34 'flexFormElementContainer' => Container\FlexFormElementContainer::class,
35 'flexFormNoTabsContainer' => Container\FlexFormNoTabsContainer::class,
36 'flexFormSectionContainer' => Container\FlexFormSectionContainer::class,
37 'flexFormTabsContainer' => Container\FlexFormTabsContainer::class,
38 'fullRecordContainer' => Container\FullRecordContainer::class,
39 'inline' => Container\InlineControlContainer::class,
40 'inlineRecordContainer' => Container\InlineRecordContainer::class,
41 'listOfFieldsContainer' => Container\ListOfFieldsContainer::class,
42 'noTabsContainer' => Container\NoTabsContainer::class,
43 'outerWrapContainer' => Container\OuterWrapContainer::class,
44 'paletteAndSingleContainer' => Container\PaletteAndSingleContainer::class,
45 'singleFieldContainer' => Container\SingleFieldContainer::class,
46 'soloFieldContainer' => Container\SoloFieldContainer::class,
47 'tabsContainer' => Container\TabsContainer::class,
48
49 'check' => Element\CheckboxElement::class,
50 'group' => Element\GroupElement::class,
51 'input' => Element\InputTextElement::class,
52 'hidden' => Element\InputHiddenElement::class,
53 // rsaInput is defined with a fallback so extensions can use it even if ext:rsaauth is not loaded
54 'rsaInput' => Element\InputTextElement::class,
55 'imageManipulation' => Element\ImageManipulationElement::class,
56 'none' => Element\NoneElement::class,
57 'radio' => Element\RadioElement::class,
58 'selectCheckBox' => Element\SelectCheckBoxElement::class,
59 'selectMultipleSideBySide' => Element\SelectMultipleSideBySideElement::class,
60 'selectTree' => Element\SelectTreeElement::class,
61 'selectSingle' => Element\SelectSingleElement::class,
62 'selectSingleBox' => Element\SelectSingleBoxElement::class,
63 // t3editor is defined with a fallback so extensions can use it even if ext:t3editor is not loaded
64 't3editor' => Element\TextElement::class,
65 'text' => Element\TextElement::class,
66 'unknown' => Element\UnknownElement::class,
67 'user' => Element\UserElement::class,
68 );
69
70 /**
71 * Node resolver classes
72 * Nested array with nodeName as key, (sorted) priority as sub key and class as value
73 *
74 * @var array
75 */
76 protected $nodeResolver = array();
77
78 /**
79 * Set up factory. Initialize additionally registered nodes.
80 */
81 public function __construct() {
82 $this->registerAdditionalNodeTypesFromConfiguration();
83 $this->initializeNodeResolver();
84 }
85
86 /**
87 * Create a node depending on type
88 *
89 * @param array $data All information to decide which class should be instantiated and given down to sub nodes
90 * @return AbstractNode
91 * @throws Exception
92 */
93 public function create(array $data) {
94 if (empty($data['renderType'])) {
95 throw new Exception('No renderType definition found', 1431452406);
96 }
97 $type = $data['renderType'];
98
99 $className = isset($this->nodeTypes[$type]) ? $this->nodeTypes[$type] : $this->nodeTypes['unknown'];
100
101 if (!empty($this->nodeResolver[$type])) {
102 // Resolver with highest priority is called first. If it returns with a new class name,
103 // it will be taken and loop is aborted, otherwise resolver with next lower priority is called.
104 foreach ($this->nodeResolver[$type] as $priority => $resolverClassName) {
105 /** @var NodeResolverInterface $resolver */
106 $resolver = $this->instantiate($resolverClassName, $data);
107 if (!$resolver instanceof NodeResolverInterface) {
108 throw new Exception(
109 'Node resolver for type ' . $type . ' at priority ' . $priority . ' must implement NodeResolverInterface',
110 1433157422
111 );
112 }
113 // Resolver classes do NOT receive the name of the already resolved class. Single
114 // resolvers should not have dependencies to each other or the default implementation,
115 // so they also shouldn't know the output of a different resolving class.
116 // Additionally, the globalOptions array is NOT given by reference here, changing config is a
117 // task of container classes alone and must not be abused here.
118 $newClassName = $resolver->resolve();
119 if ($newClassName !== NULL) {
120 $className = $newClassName;
121 break;
122 }
123 }
124 }
125
126 /** @var AbstractNode $nodeInstance */
127 $nodeInstance = $this->instantiate($className, $data);
128 if (!$nodeInstance instanceof NodeInterface) {
129 throw new Exception('Node of type ' . get_class($nodeInstance) . ' must implement NodeInterface', 1431872546);
130 }
131 return $nodeInstance;
132 }
133
134 /**
135 * Add node types from nodeRegistry to $this->nodeTypes.
136 * This can be used to add new render types or to overwrite existing node types. The registered class must
137 * implement the NodeInterface and will be called if a node with this renderType is rendered.
138 *
139 * @throws Exception if configuration is incomplete or two nodes with identical priorities are registered
140 */
141 protected function registerAdditionalNodeTypesFromConfiguration() {
142 // List of additional or override nodes
143 $registeredTypeOverrides = $GLOBALS['TYPO3_CONF_VARS']['SYS']['formEngine']['nodeRegistry'];
144 // Sanitize input array
145 $registeredPrioritiesForNodeNames = array();
146 foreach ($registeredTypeOverrides as $override) {
147 if (!isset($override['nodeName']) || !isset($override['class']) || !isset($override['priority'])) {
148 throw new Exception(
149 'Key class, nodeName or priority missing for an entry in $GLOBALS[\'TYPO3_CONF_VARS\'][\'SYS\'][\'formEngine\'][\'nodeRegistry\']',
150 1432207533
151 );
152 }
153 if ($override['priority'] < 0 || $override['priority'] > 100) {
154 throw new Exception(
155 'Priority of element ' . $override['nodeName'] . ' with class ' . $override['class'] . ' is ' . $override['priority'] . ', but must between 0 and 100',
156 1432223531
157 );
158 }
159 if (isset($registeredPrioritiesForNodeNames[$override['nodeName']][$override['priority']])) {
160 throw new Exception(
161 'Element ' . $override['nodeName'] . ' already has an override registered with priority ' . $override['priority'],
162 1432223893
163 );
164 }
165 $registeredPrioritiesForNodeNames[$override['nodeName']][$override['priority']] = '';
166 }
167 // Add element with highest priority to registry
168 $highestPriority = array();
169 foreach ($registeredTypeOverrides as $override) {
170 if (!isset($highestPriority[$override['nodeName']]) || $override['priority'] > $highestPriority[$override['nodeName']]) {
171 $highestPriority[$override['nodeName']] = $override['priority'];
172 $this->nodeTypes[$override['nodeName']] = $override['class'];
173 }
174 }
175 }
176
177 /**
178 * Add resolver and add them sorted to a local property.
179 * This can be used to manipulate the nodeName to class resolution with own code.
180 *
181 * @throws Exception if configuration is incomplete or two resolver with identical priorities are registered
182 */
183 protected function initializeNodeResolver() {
184 // List of node resolver
185 $registeredNodeResolvers = $GLOBALS['TYPO3_CONF_VARS']['SYS']['formEngine']['nodeResolver'];
186 $resolversByType = array();
187 foreach ($registeredNodeResolvers as $nodeResolver) {
188 if (!isset($nodeResolver['nodeName']) || !isset($nodeResolver['class']) || !isset($nodeResolver['priority'])) {
189 throw new Exception(
190 'Key class, nodeName or priority missing for an entry in $GLOBALS[\'TYPO3_CONF_VARS\'][\'SYS\'][\'formEngine\'][\'nodeResolver\']',
191 1433155522
192 );
193 }
194 if ($nodeResolver['priority'] < 0 || $nodeResolver['priority'] > 100) {
195 throw new Exception(
196 'Priority of element ' . $nodeResolver['nodeName'] . ' with class ' . $nodeResolver['class'] . ' is ' . $nodeResolver['priority'] . ', but must between 0 and 100',
197 1433155563
198 );
199 }
200 if (isset($resolversByType[$nodeResolver['nodeName']][$nodeResolver['priority']])) {
201 throw new Exception(
202 'Element ' . $nodeResolver['nodeName'] . ' already has a resolver registered with priority ' . $nodeResolver['priority'],
203 1433155705
204 );
205 }
206 $resolversByType[$nodeResolver['nodeName']][$nodeResolver['priority']] = $nodeResolver['class'];
207 }
208 $sortedResolversByType = array();
209 foreach ($resolversByType as $nodeName => $prioritiesAndClasses) {
210 krsort($prioritiesAndClasses);
211 $sortedResolversByType[$nodeName] = $prioritiesAndClasses;
212 }
213 $this->nodeResolver = $sortedResolversByType;
214 }
215
216 /**
217 * Instantiate given class name
218 *
219 * @param string $className Given class name
220 * @param array $data Main data array
221 * @return object
222 */
223 protected function instantiate($className, array $data) {
224 return GeneralUtility::makeInstance($className, $this, $data);
225 }
226
227 }