TYPO3 CMS  TYPO3_8-7
AbstractNode.php
Go to the documentation of this file.
1 <?php
2 declare(strict_types = 1);
4 
5 /*
6  * This file is part of the TYPO3 CMS project.
7  *
8  * It is free software; you can redistribute it and/or modify it under
9  * the terms of the GNU General Public License, either version 2
10  * of the License, or any later version.
11  *
12  * For the full copyright and license information, please read the
13  * LICENSE.txt file that was distributed with this source code.
14  *
15  * The TYPO3 project - inspiring people to share!
16  */
17 
20 
24 abstract class AbstractNode implements NodeInterface
25 {
31  protected $nodeFactory;
32 
38  protected $data = [];
39 
45  protected $defaultFieldInformation = [];
46 
53  protected $defaultFieldControl = [];
54 
61  protected $defaultFieldWizard = [];
62 
69  public function __construct(NodeFactory $nodeFactory, array $data)
70  {
71  $this->data = $data;
72  $this->nodeFactory = $nodeFactory;
73  }
74 
80  abstract public function render();
81 
89  protected function initializeResultArray(): array
90  {
91  return [
92  'additionalJavaScriptPost' => [],
93  'additionalJavaScriptSubmit' => [],
94  'additionalHiddenFields' => [],
95  'additionalInlineLanguageLabelFiles' => [],
96  'stylesheetFiles' => [],
97  // can hold strings or arrays,
98  // string = requireJS module,
99  // array = requireJS module + callback e.g. array('TYPO3/Foo/Bar', 'function() {}')
100  'requireJsModules' => [],
101  'inlineData' => [],
102  'html' => '',
103  ];
104  }
105 
116  protected function mergeChildReturnIntoExistingResult(array $existing, array $childReturn, bool $mergeHtml = true): array
117  {
118  if ($mergeHtml && !empty($childReturn['html'])) {
119  $existing['html'] .= LF . $childReturn['html'];
120  }
121  if (!empty($childReturn['extJSCODE'])) {
122  // @deprecated since TYPO3 CMS 8, will be removed in TYPO3 CMS 9.
124  $existing['extJSCODE'] .= LF . $childReturn['extJSCODE'];
125  }
126  foreach ($childReturn['additionalJavaScriptPost'] ?? [] as $value) {
127  $existing['additionalJavaScriptPost'][] = $value;
128  }
129  foreach ($childReturn['additionalJavaScriptSubmit'] ?? [] as $value) {
130  $existing['additionalJavaScriptSubmit'][] = $value;
131  }
132  foreach ($childReturn['additionalHiddenFields'] ?? [] as $value) {
133  $existing['additionalHiddenFields'][] = $value;
134  }
135  foreach ($childReturn['stylesheetFiles'] ?? [] as $value) {
136  $existing['stylesheetFiles'][] = $value;
137  }
138  foreach ($childReturn['requireJsModules'] ?? [] as $module) {
139  $existing['requireJsModules'][] = $module;
140  }
141  foreach ($childReturn['additionalInlineLanguageLabelFiles'] ?? [] as $inlineLanguageLabelFile) {
142  $existing['additionalInlineLanguageLabelFiles'][] = $inlineLanguageLabelFile;
143  }
144  if (!empty($childReturn['inlineData'])) {
145  $existingInlineData = $existing['inlineData'];
146  $childInlineData = $childReturn['inlineData'];
147  ArrayUtility::mergeRecursiveWithOverrule($existingInlineData, $childInlineData);
148  $existing['inlineData'] = $existingInlineData;
149  }
150  return $existing;
151  }
152 
161  protected function getValidationDataAsDataAttribute(array $config): string
162  {
164  return sprintf(' data-formengine-validation-rules="%s" ', htmlspecialchars($this->getValidationDataAsJsonString($config)));
165  }
166 
173  protected function getValidationDataAsJsonString(array $config): string
174  {
175  $validationRules = [];
176  if (!empty($config['eval'])) {
177  $evalList = GeneralUtility::trimExplode(',', $config['eval'], true);
178  foreach ($evalList as $evalType) {
179  $validationRules[] = [
180  'type' => $evalType,
181  ];
182  }
183  }
184  if (!empty($config['range'])) {
185  $newValidationRule = [
186  'type' => 'range',
187  ];
188  if (!empty($config['range']['lower'])) {
189  $newValidationRule['lower'] = $config['range']['lower'];
190  }
191  if (!empty($config['range']['upper'])) {
192  $newValidationRule['upper'] = $config['range']['upper'];
193  }
194  $validationRules[] = $newValidationRule;
195  }
196  if (!empty($config['maxitems']) || !empty($config['minitems'])) {
197  $minItems = (isset($config['minitems'])) ? (int)$config['minitems'] : 0;
198  $maxItems = (isset($config['maxitems'])) ? (int)$config['maxitems'] : 99999;
199  $type = ($config['type']) ?: 'range';
200  $validationRules[] = [
201  'type' => $type,
202  'minItems' => $minItems,
203  'maxItems' => $maxItems
204  ];
205  }
206  if (!empty($config['required'])) {
207  $validationRules[] = ['type' => 'required'];
208  }
209  return json_encode($validationRules);
210  }
211 }
static trimExplode($delim, $string, $removeEmptyValues=false, $limit=0)
getValidationDataAsDataAttribute(array $config)
static mergeRecursiveWithOverrule(array &$original, array $overrule, $addKeys=true, $includeEmptyValues=true, $enableUnsetFeature=true)
mergeChildReturnIntoExistingResult(array $existing, array $childReturn, bool $mergeHtml=true)
__construct(NodeFactory $nodeFactory, array $data)