TYPO3 CMS  TYPO3_7-6
AbstractNode.php
Go to the documentation of this file.
1 <?php
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 
19 
23 abstract class AbstractNode implements NodeInterface
24 {
30  protected $data = [];
31 
44  public function __construct(NodeFactory $nodeFactory, array $data)
45  {
46  $this->data = $data;
47  }
48 
54  abstract public function render();
55 
63  protected function initializeResultArray()
64  {
65  return [
66  'additionalJavaScriptPost' => [],
67  'additionalJavaScriptSubmit' => [],
68  'additionalHiddenFields' => [],
69  'additionalInlineLanguageLabelFiles' => [],
70  'stylesheetFiles' => [],
71  // can hold strings or arrays, string = requireJS module, array = requireJS module + callback e.g. array('TYPO3/Foo/Bar', 'function() {}')
72  'requireJsModules' => [],
73  'extJSCODE' => '',
74  'inlineData' => [],
75  'html' => '',
76  ];
77  }
78 
86  protected function mergeChildReturnIntoExistingResult(array $existing, array $childReturn)
87  {
88  if (!empty($childReturn['html'])) {
89  $existing['html'] .= LF . $childReturn['html'];
90  }
91  if (!empty($childReturn['extJSCODE'])) {
92  $existing['extJSCODE'] .= LF . $childReturn['extJSCODE'];
93  }
94  foreach ($childReturn['additionalJavaScriptPost'] as $value) {
95  $existing['additionalJavaScriptPost'][] = $value;
96  }
97  foreach ($childReturn['additionalJavaScriptSubmit'] as $value) {
98  $existing['additionalJavaScriptSubmit'][] = $value;
99  }
100  foreach ($childReturn['additionalHiddenFields'] as $value) {
101  $existing['additionalHiddenFields'][] = $value;
102  }
103  foreach ($childReturn['stylesheetFiles'] as $value) {
104  $existing['stylesheetFiles'][] = $value;
105  }
106  if (!empty($childReturn['requireJsModules'])) {
107  foreach ($childReturn['requireJsModules'] as $module) {
108  $existing['requireJsModules'][] = $module;
109  }
110  }
111  if (!empty($childReturn['additionalInlineLanguageLabelFiles'])) {
112  foreach ($childReturn['additionalInlineLanguageLabelFiles'] as $inlineLanguageLabelFile) {
113  $existing['additionalInlineLanguageLabelFiles'][] = $inlineLanguageLabelFile;
114  }
115  }
116  if (!empty($childReturn['inlineData'])) {
117  $existingInlineData = $existing['inlineData'];
118  $childInlineData = $childReturn['inlineData'];
119  ArrayUtility::mergeRecursiveWithOverrule($existingInlineData, $childInlineData);
120  $existing['inlineData'] = $existingInlineData;
121  }
122  return $existing;
123  }
124 
132  protected function getValidationDataAsDataAttribute(array $config)
133  {
134  return sprintf(' data-formengine-validation-rules="%s" ', htmlspecialchars($this->getValidationDataAsJsonString($config)));
135  }
136 
143  protected function getValidationDataAsJsonString(array $config)
144  {
145  $validationRules = [];
146  if (!empty($config['eval'])) {
147  $evalList = GeneralUtility::trimExplode(',', $config['eval'], true);
148  unset($config['eval']);
149  foreach ($evalList as $evalType) {
150  $validationRules[] = [
151  'type' => $evalType,
152  'config' => $config
153  ];
154  }
155  }
156  if (!empty($config['range'])) {
157  $validationRules[] = [
158  'type' => 'range',
159  'config' => $config['range']
160  ];
161  }
162  if (!empty($config['maxitems']) || !empty($config['minitems'])) {
163  $minItems = (isset($config['minitems'])) ? (int)$config['minitems'] : 0;
164  $maxItems = (isset($config['maxitems'])) ? (int)$config['maxitems'] : 10000;
165  $type = ($config['type']) ?: 'range';
166  if ($config['type'] === 'select' && $config['renderType'] !== 'selectTree' && $maxItems <= 1 && $minItems > 0) {
167  $validationRules[] = [
168  'type' => $type,
169  'minItems' => 1,
170  'maxItems' => 100000
171  ];
172  } else {
173  $validationRules[] = [
174  'type' => $type,
175  'minItems' => $minItems,
176  'maxItems' => $maxItems
177  ];
178  }
179  }
180  if (!empty($config['required'])) {
181  $validationRules[] = ['type' => 'required'];
182  }
183  return json_encode($validationRules);
184  }
185 }
mergeChildReturnIntoExistingResult(array $existing, array $childReturn)
static trimExplode($delim, $string, $removeEmptyValues=false, $limit=0)
getValidationDataAsDataAttribute(array $config)
static mergeRecursiveWithOverrule(array &$original, array $overrule, $addKeys=true, $includeEmptyValues=true, $enableUnsetFeature=true)
__construct(NodeFactory $nodeFactory, array $data)