TYPO3 CMS  TYPO3_7-6
NodeFactory.php
Go to the documentation of this file.
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 
18 
23 {
29  protected $nodeTypes = [
30  'flex' => Container\FlexFormEntryContainer::class,
31  'flexFormContainerContainer' => Container\FlexFormContainerContainer::class,
32  'flexFormElementContainer' => Container\FlexFormElementContainer::class,
33  'flexFormNoTabsContainer' => Container\FlexFormNoTabsContainer::class,
34  'flexFormSectionContainer' => Container\FlexFormSectionContainer::class,
35  'flexFormTabsContainer' => Container\FlexFormTabsContainer::class,
36  'fullRecordContainer' => Container\FullRecordContainer::class,
37  'inline' => Container\InlineControlContainer::class,
38  'inlineRecordContainer' => Container\InlineRecordContainer::class,
39  'listOfFieldsContainer' => Container\ListOfFieldsContainer::class,
40  'noTabsContainer' => Container\NoTabsContainer::class,
41  'outerWrapContainer' => Container\OuterWrapContainer::class,
42  'paletteAndSingleContainer' => Container\PaletteAndSingleContainer::class,
43  'singleFieldContainer' => Container\SingleFieldContainer::class,
44  'soloFieldContainer' => Container\SoloFieldContainer::class,
45  'tabsContainer' => Container\TabsContainer::class,
46 
47  'check' => Element\CheckboxElement::class,
48  'group' => Element\GroupElement::class,
49  'input' => Element\InputTextElement::class,
50  'hidden' => Element\InputHiddenElement::class,
51  // rsaInput is defined with a fallback so extensions can use it even if ext:rsaauth is not loaded
52  'rsaInput' => Element\InputTextElement::class,
53  'imageManipulation' => Element\ImageManipulationElement::class,
54  'none' => Element\NoneElement::class,
55  'radio' => Element\RadioElement::class,
56  'selectCheckBox' => Element\SelectCheckBoxElement::class,
57  'selectMultipleSideBySide' => Element\SelectMultipleSideBySideElement::class,
58  'selectTree' => Element\SelectTreeElement::class,
59  'selectSingle' => Element\SelectSingleElement::class,
60  'selectSingleBox' => Element\SelectSingleBoxElement::class,
61  // t3editor is defined with a fallback so extensions can use it even if ext:t3editor is not loaded
62  't3editor' => Element\TextElement::class,
63  'text' => Element\TextElement::class,
64  'unknown' => Element\UnknownElement::class,
65  'user' => Element\UserElement::class,
66  ];
67 
74  protected $nodeResolver = [];
75 
79  public function __construct()
80  {
82  $this->initializeNodeResolver();
83  }
84 
92  public function create(array $data)
93  {
94  if (empty($data['renderType'])) {
95  throw new Exception(
96  'Missing "renderType" in TCA of field "[' . $data['tableName'] . '][' . $data['fieldName'] . ']".',
97  1431452406
98  );
99  }
100  $type = $data['renderType'];
101 
102  $className = isset($this->nodeTypes[$type]) ? $this->nodeTypes[$type] : $this->nodeTypes['unknown'];
103 
104  if (!empty($this->nodeResolver[$type])) {
105  // Resolver with highest priority is called first. If it returns with a new class name,
106  // it will be taken and loop is aborted, otherwise resolver with next lower priority is called.
107  foreach ($this->nodeResolver[$type] as $priority => $resolverClassName) {
109  $resolver = $this->instantiate($resolverClassName, $data);
110  if (!$resolver instanceof NodeResolverInterface) {
111  throw new Exception(
112  'Node resolver for type ' . $type . ' at priority ' . $priority . ' must implement NodeResolverInterface',
113  1433157422
114  );
115  }
116  // Resolver classes do NOT receive the name of the already resolved class. Single
117  // resolvers should not have dependencies to each other or the default implementation,
118  // so they also shouldn't know the output of a different resolving class.
119  // Additionally, the globalOptions array is NOT given by reference here, changing config is a
120  // task of container classes alone and must not be abused here.
121  $newClassName = $resolver->resolve();
122  if ($newClassName !== null) {
123  $className = $newClassName;
124  break;
125  }
126  }
127  }
128 
130  $nodeInstance = $this->instantiate($className, $data);
131  if (!$nodeInstance instanceof NodeInterface) {
132  throw new Exception('Node of type ' . get_class($nodeInstance) . ' must implement NodeInterface', 1431872546);
133  }
134  return $nodeInstance;
135  }
136 
145  {
146  // List of additional or override nodes
147  $registeredTypeOverrides = $GLOBALS['TYPO3_CONF_VARS']['SYS']['formEngine']['nodeRegistry'];
148  // Sanitize input array
149  $registeredPrioritiesForNodeNames = [];
150  foreach ($registeredTypeOverrides as $override) {
151  if (!isset($override['nodeName']) || !isset($override['class']) || !isset($override['priority'])) {
152  throw new Exception(
153  'Key class, nodeName or priority missing for an entry in $GLOBALS[\'TYPO3_CONF_VARS\'][\'SYS\'][\'formEngine\'][\'nodeRegistry\']',
154  1432207533
155  );
156  }
157  if ($override['priority'] < 0 || $override['priority'] > 100) {
158  throw new Exception(
159  'Priority of element ' . $override['nodeName'] . ' with class ' . $override['class'] . ' is ' . $override['priority'] . ', but must between 0 and 100',
160  1432223531
161  );
162  }
163  if (isset($registeredPrioritiesForNodeNames[$override['nodeName']][$override['priority']])) {
164  throw new Exception(
165  'Element ' . $override['nodeName'] . ' already has an override registered with priority ' . $override['priority'],
166  1432223893
167  );
168  }
169  $registeredPrioritiesForNodeNames[$override['nodeName']][$override['priority']] = '';
170  }
171  // Add element with highest priority to registry
172  $highestPriority = [];
173  foreach ($registeredTypeOverrides as $override) {
174  if (!isset($highestPriority[$override['nodeName']]) || $override['priority'] > $highestPriority[$override['nodeName']]) {
175  $highestPriority[$override['nodeName']] = $override['priority'];
176  $this->nodeTypes[$override['nodeName']] = $override['class'];
177  }
178  }
179  }
180 
187  protected function initializeNodeResolver()
188  {
189  // List of node resolver
190  $registeredNodeResolvers = $GLOBALS['TYPO3_CONF_VARS']['SYS']['formEngine']['nodeResolver'];
191  $resolversByType = [];
192  foreach ($registeredNodeResolvers as $nodeResolver) {
193  if (!isset($nodeResolver['nodeName']) || !isset($nodeResolver['class']) || !isset($nodeResolver['priority'])) {
194  throw new Exception(
195  'Key class, nodeName or priority missing for an entry in $GLOBALS[\'TYPO3_CONF_VARS\'][\'SYS\'][\'formEngine\'][\'nodeResolver\']',
196  1433155522
197  );
198  }
199  if ($nodeResolver['priority'] < 0 || $nodeResolver['priority'] > 100) {
200  throw new Exception(
201  'Priority of element ' . $nodeResolver['nodeName'] . ' with class ' . $nodeResolver['class'] . ' is ' . $nodeResolver['priority'] . ', but must between 0 and 100',
202  1433155563
203  );
204  }
205  if (isset($resolversByType[$nodeResolver['nodeName']][$nodeResolver['priority']])) {
206  throw new Exception(
207  'Element ' . $nodeResolver['nodeName'] . ' already has a resolver registered with priority ' . $nodeResolver['priority'],
208  1433155705
209  );
210  }
211  $resolversByType[$nodeResolver['nodeName']][$nodeResolver['priority']] = $nodeResolver['class'];
212  }
213  $sortedResolversByType = [];
214  foreach ($resolversByType as $nodeName => $prioritiesAndClasses) {
215  krsort($prioritiesAndClasses);
216  $sortedResolversByType[$nodeName] = $prioritiesAndClasses;
217  }
218  $this->nodeResolver = $sortedResolversByType;
219  }
220 
228  protected function instantiate($className, array $data)
229  {
230  return GeneralUtility::makeInstance($className, $this, $data);
231  }
232 }
instantiate($className, array $data)
if(TYPO3_MODE==='BE') $GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['t3lib/class.t3lib_tsfebeuserauth.php']['frontendEditingController']['default']