TYPO3  7.6
backend/Classes/Form/AbstractNode.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 
19 
23 abstract class AbstractNode implements NodeInterface
24 {
30  protected $data = array();
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 array(
66  'additionalJavaScriptPost' => array(),
67  'additionalJavaScriptSubmit' => array(),
68  'additionalHiddenFields' => array(),
69  'stylesheetFiles' => array(),
70  // can hold strings or arrays, string = requireJS module, array = requireJS module + callback e.g. array('TYPO3/Foo/Bar', 'function() {}')
71  'requireJsModules' => array(),
72  'extJSCODE' => '',
73  'inlineData' => array(),
74  'html' => '',
75  );
76  }
77 
85  protected function mergeChildReturnIntoExistingResult(array $existing, array $childReturn)
86  {
87  if (!empty($childReturn['html'])) {
88  $existing['html'] .= LF . $childReturn['html'];
89  }
90  if (!empty($childReturn['extJSCODE'])) {
91  $existing['extJSCODE'] .= LF . $childReturn['extJSCODE'];
92  }
93  foreach ($childReturn['additionalJavaScriptPost'] as $value) {
94  $existing['additionalJavaScriptPost'][] = $value;
95  }
96  foreach ($childReturn['additionalJavaScriptSubmit'] as $value) {
97  $existing['additionalJavaScriptSubmit'][] = $value;
98  }
99  foreach ($childReturn['additionalHiddenFields'] as $value) {
100  $existing['additionalHiddenFields'][] = $value;
101  }
102  foreach ($childReturn['stylesheetFiles'] as $value) {
103  $existing['stylesheetFiles'][] = $value;
104  }
105  if (!empty($childReturn['requireJsModules'])) {
106  foreach ($childReturn['requireJsModules'] as $module) {
107  $existing['requireJsModules'][] = $module;
108  }
109  }
110  if (!empty($childReturn['inlineData'])) {
111  $existingInlineData = $existing['inlineData'];
112  $childInlineData = $childReturn['inlineData'];
113  ArrayUtility::mergeRecursiveWithOverrule($existingInlineData, $childInlineData);
114  $existing['inlineData'] = $existingInlineData;
115  }
116  return $existing;
117  }
118 
126  protected function getValidationDataAsDataAttribute(array $config)
127  {
128  return sprintf(' data-formengine-validation-rules="%s" ', htmlspecialchars($this->getValidationDataAsJsonString($config)));
129  }
130 
137  protected function getValidationDataAsJsonString(array $config)
138  {
139  $validationRules = array();
140  if (!empty($config['eval'])) {
141  $evalList = GeneralUtility::trimExplode(',', $config['eval'], true);
142  unset($config['eval']);
143  foreach ($evalList as $evalType) {
144  $validationRules[] = array(
145  'type' => $evalType,
146  'config' => $config
147  );
148  }
149  }
150  if (!empty($config['range'])) {
151  $validationRules[] = array(
152  'type' => 'range',
153  'config' => $config['range']
154  );
155  }
156  if (!empty($config['maxitems']) || !empty($config['minitems'])) {
157  $minItems = (isset($config['minitems'])) ? (int)$config['minitems'] : 0;
158  $maxItems = (isset($config['maxitems'])) ? (int)$config['maxitems'] : 10000;
159  $type = ($config['type']) ?: 'range';
160  if ($config['renderType'] !== 'selectTree' && $maxItems <= 1 && $minItems > 0) {
161  $validationRules[] = array(
162  'type' => $type,
163  'minItems' => 1,
164  'maxItems' => 100000
165  );
166  } else {
167  $validationRules[] = array(
168  'type' => $type,
169  'minItems' => $minItems,
170  'maxItems' => $maxItems
171  );
172  }
173  }
174  if (!empty($config['required'])) {
175  $validationRules[] = array('type' => 'required');
176  }
177  return json_encode($validationRules);
178  }
179 }