[TASK] Rename InputElement class to InputTextElement
[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\FlexFormLanguageContainer::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 // rsaInput is defined with a fallback so extensions can use it even if ext:rsaauth is not loaded
53 'rsaInput' => Element\InputTextElement::class,
54 'imageManipulation' => Element\ImageManipulationElement::class,
55 'none' => Element\NoneElement::class,
56 'radio' => Element\RadioElement::class,
57 'selectCheckBox' => Element\SelectCheckBoxElement::class,
58 'selectMultipleSideBySide' => Element\SelectMultipleSideBySideElement::class,
59 'selectTree' => Element\SelectTreeElement::class,
60 'selectSingle' => Element\SelectSingleElement::class,
61 'selectSingleBox' => Element\SelectSingleBoxElement::class,
62 // t3editor is defined with a fallback so extensions can use it even if ext:t3editor is not loaded
63 't3editor' => Element\TextElement::class,
64 'text' => Element\TextElement::class,
65 'unknown' => Element\UnknownElement::class,
66 'user' => Element\UserElement::class,
67 );
68
69 /**
70 * Node resolver classes
71 * Nested array with nodeName as key, (sorted) priority as sub key and class as value
72 *
73 * @var array
74 */
75 protected $nodeResolver = array();
76
77 /**
78 * Set up factory. Initialize additionally registered nodes.
79 */
80 public function __construct() {
81 $this->registerAdditionalNodeTypesFromConfiguration();
82 $this->initializeNodeResolver();
83 }
84
85 /**
86 * Create a node depending on type
87 *
88 * @param array $data All information to decide which class should be instantiated and given down to sub nodes
89 * @return AbstractNode
90 * @throws Exception
91 */
92 public function create(array $data) {
93 if (empty($data['renderType'])) {
94 throw new Exception('No renderType definition found', 1431452406);
95 }
96 $type = $data['renderType'];
97
98 if ($type === 'select') {
99 $config = $data['parameterArray']['fieldConf']['config'];
100 $maxItems = (int)$config['maxitems'];
101 if (isset($config['renderMode']) && $config['renderMode'] === 'tree') {
102 $type = 'selectTree';
103 } elseif ($maxItems <= 1) {
104 $type = 'selectSingle';
105 } elseif (isset($config['renderMode']) && $config['renderMode'] === 'singlebox') {
106 $type = 'selectSingleBox';
107 } elseif (isset($config['renderMode']) && $config['renderMode'] === 'checkbox') {
108 $type = 'selectCheckBox';
109 } else {
110 // @todo: This "catch all" else should be removed to allow registration of own renderTypes for type=select
111 $type = 'selectMultipleSideBySide';
112 }
113 }
114
115 $className = isset($this->nodeTypes[$type]) ? $this->nodeTypes[$type] : $this->nodeTypes['unknown'];
116
117 if (!empty($this->nodeResolver[$type])) {
118 // Resolver with highest priority is called first. If it returns with a new class name,
119 // it will be taken and loop is aborted, otherwise resolver with next lower priority is called.
120 foreach ($this->nodeResolver[$type] as $priority => $resolverClassName) {
121 /** @var NodeResolverInterface $resolver */
122 $resolver = $this->instantiate($resolverClassName, $data);
123 if (!$resolver instanceof NodeResolverInterface) {
124 throw new Exception(
125 'Node resolver for type ' . $type . ' at priority ' . $priority . ' must implement NodeResolverInterface',
126 1433157422
127 );
128 }
129 // Resolver classes do NOT receive the name of the already resolved class. Single
130 // resolvers should not have dependencies to each other or the default implementation,
131 // so they also shouldn't know the output of a different resolving class.
132 // Additionally, the globalOptions array is NOT given by reference here, changing config is a
133 // task of container classes alone and must not be abused here.
134 $newClassName = $resolver->resolve();
135 if ($newClassName !== NULL) {
136 $className = $newClassName;
137 break;
138 }
139 }
140 }
141
142 /** @var AbstractNode $nodeInstance */
143 $nodeInstance = $this->instantiate($className, $data);
144 if (!$nodeInstance instanceof NodeInterface) {
145 throw new Exception('Node of type ' . get_class($nodeInstance) . ' must implement NodeInterface', 1431872546);
146 }
147 return $nodeInstance;
148 }
149
150 /**
151 * Add node types from nodeRegistry to $this->nodeTypes.
152 * This can be used to add new render types or to overwrite existing node types. The registered class must
153 * implement the NodeInterface and will be called if a node with this renderType is rendered.
154 *
155 * @throws Exception if configuration is incomplete or two nodes with identical priorities are registered
156 */
157 protected function registerAdditionalNodeTypesFromConfiguration() {
158 // List of additional or override nodes
159 $registeredTypeOverrides = $GLOBALS['TYPO3_CONF_VARS']['SYS']['formEngine']['nodeRegistry'];
160 // Sanitize input array
161 $registeredPrioritiesForNodeNames = array();
162 foreach ($registeredTypeOverrides as $override) {
163 if (!isset($override['nodeName']) || !isset($override['class']) || !isset($override['priority'])) {
164 throw new Exception(
165 'Key class, nodeName or priority missing for an entry in $GLOBALS[\'TYPO3_CONF_VARS\'][\'SYS\'][\'formEngine\'][\'nodeRegistry\']',
166 1432207533
167 );
168 }
169 if ($override['priority'] < 0 || $override['priority'] > 100) {
170 throw new Exception(
171 'Priority of element ' . $override['nodeName'] . ' with class ' . $override['class'] . ' is ' . $override['priority'] . ', but must between 0 and 100',
172 1432223531
173 );
174 }
175 if (isset($registeredPrioritiesForNodeNames[$override['nodeName']][$override['priority']])) {
176 throw new Exception(
177 'Element ' . $override['nodeName'] . ' already has an override registered with priority ' . $override['priority'],
178 1432223893
179 );
180 }
181 $registeredPrioritiesForNodeNames[$override['nodeName']][$override['priority']] = '';
182 }
183 // Add element with highest priority to registry
184 $highestPriority = array();
185 foreach ($registeredTypeOverrides as $override) {
186 if (!isset($highestPriority[$override['nodeName']]) || $override['priority'] > $highestPriority[$override['nodeName']]) {
187 $highestPriority[$override['nodeName']] = $override['priority'];
188 $this->nodeTypes[$override['nodeName']] = $override['class'];
189 }
190 }
191 }
192
193 /**
194 * Add resolver and add them sorted to a local property.
195 * This can be used to manipulate the nodeName to class resolution with own code.
196 *
197 * @throws Exception if configuration is incomplete or two resolver with identical priorities are registered
198 */
199 protected function initializeNodeResolver() {
200 // List of node resolver
201 $registeredNodeResolvers = $GLOBALS['TYPO3_CONF_VARS']['SYS']['formEngine']['nodeResolver'];
202 $resolversByType = array();
203 foreach ($registeredNodeResolvers as $nodeResolver) {
204 if (!isset($nodeResolver['nodeName']) || !isset($nodeResolver['class']) || !isset($nodeResolver['priority'])) {
205 throw new Exception(
206 'Key class, nodeName or priority missing for an entry in $GLOBALS[\'TYPO3_CONF_VARS\'][\'SYS\'][\'formEngine\'][\'nodeResolver\']',
207 1433155522
208 );
209 }
210 if ($nodeResolver['priority'] < 0 || $nodeResolver['priority'] > 100) {
211 throw new Exception(
212 'Priority of element ' . $nodeResolver['nodeName'] . ' with class ' . $nodeResolver['class'] . ' is ' . $nodeResolver['priority'] . ', but must between 0 and 100',
213 1433155563
214 );
215 }
216 if (isset($resolversByType[$nodeResolver['nodeName']][$nodeResolver['priority']])) {
217 throw new Exception(
218 'Element ' . $nodeResolver['nodeName'] . ' already has a resolver registered with priority ' . $nodeResolver['priority'],
219 1433155705
220 );
221 }
222 $resolversByType[$nodeResolver['nodeName']][$nodeResolver['priority']] = $nodeResolver['class'];
223 }
224 $sortedResolversByType = array();
225 foreach ($resolversByType as $nodeName => $prioritiesAndClasses) {
226 krsort($prioritiesAndClasses);
227 $sortedResolversByType[$nodeName] = $prioritiesAndClasses;
228 }
229 $this->nodeResolver = $sortedResolversByType;
230 }
231
232 /**
233 * Instantiate given class name
234 *
235 * @param string $className Given class name
236 * @param array $data Main data array
237 * @return object
238 */
239 protected function instantiate($className, array $data) {
240 return GeneralUtility::makeInstance($className, $this, $data);
241 }
242
243 }