‪TYPO3CMS  10.4
NodeFactory.php
Go to the documentation of this file.
1 <?php
2 
3 /*
4  * This file is part of the TYPO3 CMS project.
5  *
6  * It is free software; you can redistribute it and/or modify it under
7  * the terms of the GNU General Public License, either version 2
8  * of the License, or any later version.
9  *
10  * For the full copyright and license information, please read the
11  * LICENSE.txt file that was distributed with this source code.
12  *
13  * The TYPO3 project - inspiring people to share!
14  */
15 
17 
19 
37 {
44  protected ‪$nodeResolver = [];
45 
51  protected ‪$nodeTypes = [
52  // Default container classes
53  'flex' => Container\FlexFormEntryContainer::class,
54  'flexFormContainerContainer' => Container\FlexFormContainerContainer::class,
55  'flexFormElementContainer' => Container\FlexFormElementContainer::class,
56  'flexFormNoTabsContainer' => Container\FlexFormNoTabsContainer::class,
57  'flexFormSectionContainer' => Container\FlexFormSectionContainer::class,
58  'flexFormTabsContainer' => Container\FlexFormTabsContainer::class,
59  'fullRecordContainer' => Container\FullRecordContainer::class,
60  'inline' => Container\InlineControlContainer::class,
61  'inlineRecordContainer' => Container\InlineRecordContainer::class,
62  'listOfFieldsContainer' => Container\ListOfFieldsContainer::class,
63  'noTabsContainer' => Container\NoTabsContainer::class,
64  'outerWrapContainer' => Container\OuterWrapContainer::class,
65  'paletteAndSingleContainer' => Container\PaletteAndSingleContainer::class,
66  'singleFieldContainer' => Container\SingleFieldContainer::class,
67  'tabsContainer' => Container\TabsContainer::class,
68 
69  // Default single element classes
70  'check' => Element\CheckboxElement::class,
71  'checkboxToggle' => Element\CheckboxToggleElement::class,
72  'checkboxLabeledToggle' => Element\CheckboxLabeledToggleElement::class,
73  'group' => Element\GroupElement::class,
74  'input' => Element\InputTextElement::class,
75  'inputDateTime' => Element\InputDateTimeElement::class,
76  'inputLink' => Element\InputLinkElement::class,
77  'hidden' => Element\InputHiddenElement::class,
78  'imageManipulation' => Element\ImageManipulationElement::class,
79  'none' => Element\NoneElement::class,
80  'radio' => Element\RadioElement::class,
81  'selectCheckBox' => Element\SelectCheckBoxElement::class,
82  'selectMultipleSideBySide' => Element\SelectMultipleSideBySideElement::class,
83  'selectTree' => Element\SelectTreeElement::class,
84  'selectSingle' => Element\SelectSingleElement::class,
85  'selectSingleBox' => Element\SelectSingleBoxElement::class,
86  'colorpicker' => Element\InputColorPickerElement::class,
87  // t3editor is defined with a fallback so extensions can use it even if ext:t3editor is not loaded
88  't3editor' => Element\TextElement::class,
89  'text' => Element\TextElement::class,
90  'textTable' => Element\TextTableElement::class,
91  'unknown' => Element\UnknownElement::class,
92  'user' => Element\UserElement::class,
93  // special renderType for type="user" on sys_file_storage is_public column
94  'userSysFileStorageIsPublic' => Element\UserSysFileStorageIsPublicElement::class,
95  'fileInfo' => Element\FileInfoElement::class,
96  'slug' => Element\InputSlugElement::class,
97  'passthrough' => Element\PassThroughElement::class,
98 
99  // Default classes to enrich single elements
100  'fieldControl' => NodeExpansion\FieldControl::class,
101  'fieldInformation' => NodeExpansion\FieldInformation::class,
102  'fieldWizard' => NodeExpansion\FieldWizard::class,
103 
104  // Element information
105  'tcaDescription' => FieldInformation\TcaDescription::class,
106  'adminIsSystemMaintainer' => FieldInformation\AdminIsSystemMaintainer::class,
107 
108  // Element wizards
109  'defaultLanguageDifferences' => FieldWizard\DefaultLanguageDifferences::class,
110  'localizationStateSelector' => FieldWizard\LocalizationStateSelector::class,
111  'otherLanguageContent' => FieldWizard\OtherLanguageContent::class,
112  'otherLanguageThumbnails' => FieldWizard\OtherLanguageThumbnails::class,
113  'recordsOverview' => FieldWizard\RecordsOverview::class,
114  'selectIcons' => FieldWizard\SelectIcons::class,
115  'tableList' => FieldWizard\TableList::class,
116 
117  // Element controls
118  'addRecord' => FieldControl\AddRecord::class,
119  'editPopup' => FieldControl\EditPopup::class,
120  'elementBrowser' => FieldControl\ElementBrowser::class,
121  'insertClipboard' => FieldControl\InsertClipboard::class,
122  'linkPopup' => FieldControl\LinkPopup::class,
123  'listModule' => FieldControl\ListModule::class,
124  'resetSelection' => FieldControl\ResetSelection::class,
125  'tableWizard' => FieldControl\TableWizard::class,
126  ];
127 
131  public function ‪__construct()
132  {
134  $this->‪initializeNodeResolver();
135  }
136 
144  public function ‪create(array $data)
145  {
146  if (empty($data['renderType'])) {
147  throw new ‪Exception(
148  'Missing "renderType" in TCA of field "[' . ($data['tableName'] ?? 'unknown') . '][' . ($data['fieldName'] ?? 'unknown') . ']".',
149  1431452406
150  );
151  }
152  $type = $data['renderType'];
153 
154  $className = $this->nodeTypes[$type] ?? $this->nodeTypes['unknown'];
155 
156  if (!empty($this->nodeResolver[$type])) {
157  // Resolver with highest priority is called first. If it returns with a new class name,
158  // it will be taken and loop is aborted, otherwise resolver with next lower priority is called.
159  foreach ($this->nodeResolver[$type] as $priority => $resolverClassName) {
161  $resolver = $this->‪instantiate($resolverClassName, $data);
162  if (!$resolver instanceof ‪NodeResolverInterface) {
163  throw new ‪Exception(
164  'Node resolver for type ' . $type . ' at priority ' . $priority . ' must implement NodeResolverInterface',
165  1433157422
166  );
167  }
168  // Resolver classes do NOT receive the name of the already resolved class. Single
169  // resolvers should not have dependencies to each other or the default implementation,
170  // so they also shouldn't know the output of a different resolving class.
171  // Additionally, the globalOptions array is NOT given by reference here, changing config is a
172  // task of container classes alone and must not be abused here.
173  $newClassName = $resolver->resolve();
174  if ($newClassName !== null) {
175  $className = $newClassName;
176  break;
177  }
178  }
179  }
180 
182  $nodeInstance = $this->‪instantiate($className, $data);
183  if (!$nodeInstance instanceof ‪NodeInterface) {
184  throw new ‪Exception('Node of type ' . get_class($nodeInstance) . ' must implement NodeInterface', 1431872546);
185  }
186  return $nodeInstance;
187  }
188 
197  {
198  // List of additional or override nodes
199  $registeredTypeOverrides = ‪$GLOBALS['TYPO3_CONF_VARS']['SYS']['formEngine']['nodeRegistry'];
200  // Sanitize input array
201  $registeredPrioritiesForNodeNames = [];
202  foreach ($registeredTypeOverrides as $override) {
203  if (!isset($override['nodeName']) || !isset($override['class']) || !isset($override['priority'])) {
204  throw new ‪Exception(
205  'Key class, nodeName or priority missing for an entry in $GLOBALS[\'TYPO3_CONF_VARS\'][\'SYS\'][\'formEngine\'][\'nodeRegistry\']',
206  1432207533
207  );
208  }
209  if ($override['priority'] < 0 || $override['priority'] > 100) {
210  throw new ‪Exception(
211  'Priority of element ' . $override['nodeName'] . ' with class ' . $override['class'] . ' is ' . $override['priority'] . ', but must between 0 and 100',
212  1432223531
213  );
214  }
215  if (isset($registeredPrioritiesForNodeNames[$override['nodeName']][$override['priority']])) {
216  throw new ‪Exception(
217  'Element ' . $override['nodeName'] . ' already has an override registered with priority ' . $override['priority'],
218  1432223893
219  );
220  }
221  $registeredPrioritiesForNodeNames[$override['nodeName']][$override['priority']] = '';
222  }
223  // Add element with highest priority to registry
224  $highestPriority = [];
225  foreach ($registeredTypeOverrides as $override) {
226  if (!isset($highestPriority[$override['nodeName']]) || $override['priority'] > $highestPriority[$override['nodeName']]) {
227  $highestPriority[$override['nodeName']] = $override['priority'];
228  $this->nodeTypes[$override['nodeName']] = $override['class'];
229  }
230  }
231  }
232 
239  protected function ‪initializeNodeResolver()
240  {
241  // List of node resolver
242  $registeredNodeResolvers = ‪$GLOBALS['TYPO3_CONF_VARS']['SYS']['formEngine']['nodeResolver'];
243  $resolversByType = [];
244  foreach ($registeredNodeResolvers as ‪$nodeResolver) {
245  if (!isset(‪$nodeResolver['nodeName']) || !isset(‪$nodeResolver['class']) || !isset(‪$nodeResolver['priority'])) {
246  throw new ‪Exception(
247  'Key class, nodeName or priority missing for an entry in $GLOBALS[\'TYPO3_CONF_VARS\'][\'SYS\'][\'formEngine\'][\'nodeResolver\']',
248  1433155522
249  );
250  }
251  if (‪$nodeResolver['priority'] < 0 || ‪$nodeResolver['priority'] > 100) {
252  throw new ‪Exception(
253  'Priority of element ' . ‪$nodeResolver['nodeName'] . ' with class ' . ‪$nodeResolver['class'] . ' is ' . ‪$nodeResolver['priority'] . ', but must between 0 and 100',
254  1433155563
255  );
256  }
257  if (isset($resolversByType[‪$nodeResolver['nodeName']][‪$nodeResolver['priority']])) {
258  throw new ‪Exception(
259  'Element ' . ‪$nodeResolver['nodeName'] . ' already has a resolver registered with priority ' . ‪$nodeResolver['priority'],
260  1433155705
261  );
262  }
263  $resolversByType[‪$nodeResolver['nodeName']][‪$nodeResolver['priority']] = ‪$nodeResolver['class'];
264  }
265  $sortedResolversByType = [];
266  foreach ($resolversByType as $nodeName => $prioritiesAndClasses) {
267  krsort($prioritiesAndClasses);
268  $sortedResolversByType[$nodeName] = $prioritiesAndClasses;
269  }
270  $this->nodeResolver = $sortedResolversByType;
271  }
272 
280  protected function ‪instantiate($className, array $data)
281  {
282  return GeneralUtility::makeInstance($className, $this, $data);
283  }
284 }
‪TYPO3\CMS\Backend\Form
Definition: AbstractNode.php:18
‪TYPO3\CMS\Backend\Form\NodeFactory\$nodeResolver
‪array $nodeResolver
Definition: NodeFactory.php:43
‪TYPO3\CMS\Backend\Form\NodeFactory\initializeNodeResolver
‪initializeNodeResolver()
Definition: NodeFactory.php:237
‪TYPO3\CMS\Backend\Form\Exception
Definition: Exception.php:22
‪TYPO3\CMS\Backend\Form\NodeFactory\registerAdditionalNodeTypesFromConfiguration
‪registerAdditionalNodeTypesFromConfiguration()
Definition: NodeFactory.php:194
‪TYPO3\CMS\Backend\Form\NodeInterface
Definition: NodeInterface.php:22
‪TYPO3\CMS\Backend\Form\NodeFactory
Definition: NodeFactory.php:37
‪TYPO3\CMS\Backend\Form\NodeResolverInterface
Definition: NodeResolverInterface.php:22
‪$GLOBALS
‪$GLOBALS['TYPO3_CONF_VARS']['EXTCONF']['adminpanel']['modules']
Definition: ext_localconf.php:5
‪TYPO3\CMS\Backend\Form\NodeFactory\instantiate
‪object instantiate($className, array $data)
Definition: NodeFactory.php:278
‪TYPO3\CMS\Backend\Form\AbstractNode
Definition: AbstractNode.php:29
‪TYPO3\CMS\Core\Utility\GeneralUtility
Definition: GeneralUtility.php:46
‪TYPO3\CMS\Backend\Form\NodeFactory\$nodeTypes
‪array $nodeTypes
Definition: NodeFactory.php:49
‪TYPO3\CMS\Backend\Form\NodeFactory\create
‪AbstractNode create(array $data)
Definition: NodeFactory.php:142
‪TYPO3\CMS\Backend\Form\NodeFactory\__construct
‪__construct()
Definition: NodeFactory.php:129