‪TYPO3CMS  9.5
TypoScriptConstantsViewHelper.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 
21 use TYPO3Fluid\Fluid\Core\ViewHelper\AbstractTagBasedViewHelper;
22 use TYPO3Fluid\Fluid\Core\ViewHelper\TagBuilder;
23 
28 class ‪TypoScriptConstantsViewHelper extends AbstractTagBasedViewHelper
29 {
33  public ‪$viewHelperMapping = [
34  'int' => 'renderIntegerField',
35  'int+' => 'renderPositiveIntegerField',
36  'integer' => 'renderIntegerField',
37  'color' => 'renderColorPicker',
38  'wrap' => 'renderWrapField',
39  'offset' => 'renderOffsetField',
40  'options' => 'renderOptionSelect',
41  'boolean' => 'renderCheckbox',
42  'user' => 'renderUserFunction',
43  'small' => 'renderSmallTextField',
44  'string' => 'renderTextField',
45  'input' => 'renderTextField', // only for backwards compatibility, many extensions depend on that
46  'default' => 'renderTextField' // only for backwards compatibility, many extensions depend on that
47  ];
48 
52  public ‪$tagName = 'input';
53 
57  public function ‪initializeArguments()
58  {
59  parent::initializeArguments();
60  $this->registerArgument('name', 'string', 'Name of input tag');
61  $this->registerArgument('value', 'mixed', 'Value of input tag');
62  $this->registerArgument('configuration', 'array', '', true);
63  $this->registerUniversalTagAttributes();
64  }
65 
69  public function ‪initialize()
70  {
71  $this->setTagBuilder(new TagBuilder($this->tagName));
72  parent::initialize();
73  }
74 
80  public function ‪render(): string
81  {
83  $configuration = $this->arguments['configuration'];
84  if (isset($this->viewHelperMapping[$configuration['type']]) && method_exists($this, $this->viewHelperMapping[$configuration['type']])) {
85  $input = $this->{$this->viewHelperMapping[$configuration['type']]}($configuration);
86  } else {
87  $input = $this->{$this->viewHelperMapping['default']}($configuration);
88  }
89 
90  return $input;
91  }
92 
99  protected function ‪renderColorPicker(array $configuration): string
100  {
101  $elementName = $this->‪getName($configuration);
102 
103  // configure the field
104  $this->tag->setTagName('input');
105  $this->tag->addAttribute('type', 'text');
106  $this->‪addIdAttribute($configuration);
107  $this->tag->addAttribute('name', $elementName);
108  $this->tag->addAttribute('data-formengine-input-name', $elementName);
109  $this->tag->addAttribute('class', 'form-control');
110  if ($configuration['value'] !== null) {
111  $this->tag->addAttribute('value', $configuration['value']);
112  }
113 
114  ‪$output = '
115  <div class="form-wizards-element">
116  <input class="form-control t3js-color-input formengine-colorpickerelement t3js-color-picker" type="text"
117  name="' . htmlspecialchars($elementName) . '" value="' . $this->tag->getAttribute('value') . '"/>
118  <script type="text/javascript">
119  require([\'TYPO3/CMS/Backend/ColorPicker\'], function(ColorPicker){ColorPicker.initialize()});
120  </script>
121  </div>';
122 
123  return ‪$output;
124  }
125 
132  protected function ‪renderOffsetField(array $configuration): string
133  {
134  $this->tag->setTagName('input');
135  $this->tag->addAttribute('type', 'text');
136  $this->‪addIdAttribute($configuration);
137  $this->tag->addAttribute('name', $this->‪getName($configuration));
138  $this->tag->addAttribute('class', 'form-control t3js-emconf-offset');
139  if ($configuration['value'] !== null) {
140  $this->tag->addAttribute('value', $configuration['value']);
141  }
142  return $this->tag->render();
143  }
144 
151  protected function ‪renderWrapField(array $configuration): string
152  {
153  $this->tag->setTagName('input');
154  $this->tag->addAttribute('type', 'text');
155  $this->‪addIdAttribute($configuration);
156  $this->tag->addAttribute('name', $this->‪getName($configuration));
157  $this->tag->addAttribute('class', 'form-control t3js-emconf-wrap');
158  if ($configuration['value'] !== null) {
159  $this->tag->addAttribute('value', $configuration['value']);
160  }
161  return $this->tag->render();
162  }
163 
170  protected function ‪renderOptionSelect(array $configuration): string
171  {
172  $this->tag->setTagName('select');
173  $this->‪addIdAttribute($configuration);
174  $this->tag->addAttribute('name', $this->‪getName($configuration));
175  $this->tag->addAttribute('class', 'form-control');
176  $optionValueArray = $configuration['generic'];
177  ‪$output = '';
178  $languageService = $this->‪getLanguageService();
179  foreach ($optionValueArray as $label => $value) {
180  ‪$output .= '<option value="' . htmlspecialchars($value) . '"';
181  if ($configuration['value'] == $value) {
182  ‪$output .= ' selected="selected"';
183  }
184  ‪$output .= '>' . htmlspecialchars($languageService->sL($label)) . '</option>';
185  }
186  $this->tag->setContent(‪$output);
187  return $this->tag->render();
188  }
189 
196  protected function ‪renderPositiveIntegerField(array $configuration): string
197  {
198  $this->tag->setTagName('input');
199  $this->tag->addAttribute('type', 'number');
200  $this->‪addIdAttribute($configuration);
201  $this->tag->addAttribute('name', $this->‪getName($configuration));
202  $this->tag->addAttribute('class', 'form-control');
203  $this->tag->addAttribute('min', '0');
204  if ($configuration['value'] !== null) {
205  $this->tag->addAttribute('value', $configuration['value']);
206  }
207  return $this->tag->render();
208  }
209 
216  protected function ‪renderIntegerField(array $configuration): string
217  {
218  $this->tag->setTagName('input');
219  $this->tag->addAttribute('type', 'number');
220  $this->‪addIdAttribute($configuration);
221  $this->tag->addAttribute('name', $this->‪getName($configuration));
222  $this->tag->addAttribute('class', 'form-control');
223  if ($configuration['value'] !== null) {
224  $this->tag->addAttribute('value', $configuration['value']);
225  }
226  return $this->tag->render();
227  }
228 
235  protected function ‪renderTextField(array $configuration): string
236  {
237  $this->tag->setTagName('input');
238  $this->tag->addAttribute('type', 'text');
239  $this->‪addIdAttribute($configuration);
240  $this->tag->addAttribute('name', $this->‪getName($configuration));
241  $this->tag->addAttribute('class', 'form-control');
242  if ($configuration['value'] !== null) {
243  $this->tag->addAttribute('value', $configuration['value']);
244  }
245  return $this->tag->render();
246  }
247 
254  protected function ‪renderSmallTextField(array $configuration): string
255  {
256  return $this->‪renderTextField($configuration);
257  }
258 
265  public function ‪renderCheckbox(array $configuration): string
266  {
267  $this->tag->addAttribute('type', 'checkbox');
268  $this->tag->addAttribute('name', $this->‪getName($configuration));
269  $this->tag->addAttribute('value', 1);
270  $this->‪addIdAttribute($configuration);
271  if ($configuration['value'] == 1) {
272  $this->tag->addAttribute('checked', 'checked');
273  }
274  $hiddenField = $this->‪renderHiddenFieldForEmptyValue($configuration);
275  return '<div class="checkbox">' . $hiddenField . '<label>' . $this->tag->render() . '</label></div>';
276  }
277 
284  protected function ‪renderUserFunction(array $configuration): string
285  {
286  $userFunction = $configuration['generic'];
287  $userFunctionParams = [
288  'fieldName' => $this->‪getName($configuration),
289  'fieldValue' => $configuration['value'],
290  'propertyName' => $configuration['name']
291  ];
292  return (string)GeneralUtility::callUserFunction($userFunction, $userFunctionParams, $this);
293  }
294 
301  protected function ‪getName(array $configuration): string
302  {
303  return $configuration['name'];
304  }
305 
312  protected function ‪renderHiddenFieldForEmptyValue(array $configuration): string
313  {
314  $hiddenFieldNames = [];
315 
316  // check for already set hidden field within current extension
317  $variableKey = 'renderedHiddenFields-' . $configuration['extensionKey'];
318  if ($this->viewHelperVariableContainer->exists(FormViewHelper::class, $variableKey)) {
319  $hiddenFieldNames = $this->viewHelperVariableContainer->get(FormViewHelper::class, $variableKey);
320  }
321  $fieldName = $this->‪getName($configuration);
322  if (substr($fieldName, -2) === '[]') {
323  $fieldName = substr($fieldName, 0, -2);
324  }
325  if (!in_array($fieldName, $hiddenFieldNames)) {
326  $hiddenFieldNames[] = $fieldName;
327  $this->viewHelperVariableContainer->addOrUpdate(FormViewHelper::class, $variableKey, $hiddenFieldNames);
328  return '<input type="hidden" name="' . htmlspecialchars($fieldName) . '" value="0" />';
329  }
330  return '';
331  }
332 
336  protected function ‪getLanguageService()
337  {
338  return ‪$GLOBALS['LANG'];
339  }
340 
346  protected function ‪addIdAttribute(array $configuration): void
347  {
348  $this->tag->addAttribute(
349  'id',
350  'em-' . $configuration['extensionKey'] . '-' . $this->‪getName($configuration)
351  );
352  }
353 }
‪TYPO3\CMS\Install\ViewHelpers\Form\TypoScriptConstantsViewHelper\renderWrapField
‪string renderWrapField(array $configuration)
Definition: TypoScriptConstantsViewHelper.php:149
‪TYPO3\CMS\Install\ViewHelpers\Form\TypoScriptConstantsViewHelper\$tagName
‪string $tagName
Definition: TypoScriptConstantsViewHelper.php:50
‪TYPO3\CMS\Install\ViewHelpers\Form\TypoScriptConstantsViewHelper\renderIntegerField
‪string renderIntegerField(array $configuration)
Definition: TypoScriptConstantsViewHelper.php:214
‪TYPO3\CMS\Install\ViewHelpers\Form\TypoScriptConstantsViewHelper\initialize
‪initialize()
Definition: TypoScriptConstantsViewHelper.php:67
‪TYPO3\CMS\Install\ViewHelpers\Form\TypoScriptConstantsViewHelper\getName
‪string getName(array $configuration)
Definition: TypoScriptConstantsViewHelper.php:299
‪TYPO3\CMS\Install\ViewHelpers\Form\TypoScriptConstantsViewHelper\renderTextField
‪string renderTextField(array $configuration)
Definition: TypoScriptConstantsViewHelper.php:233
‪TYPO3\CMS\Install\ViewHelpers\Form\TypoScriptConstantsViewHelper\renderPositiveIntegerField
‪string renderPositiveIntegerField(array $configuration)
Definition: TypoScriptConstantsViewHelper.php:194
‪TYPO3\CMS\Install\ViewHelpers\Form\TypoScriptConstantsViewHelper\$viewHelperMapping
‪array $viewHelperMapping
Definition: TypoScriptConstantsViewHelper.php:32
‪TYPO3\CMS\Install\ViewHelpers\Form\TypoScriptConstantsViewHelper\initializeArguments
‪initializeArguments()
Definition: TypoScriptConstantsViewHelper.php:55
‪TYPO3\CMS\Fluid\ViewHelpers\FormViewHelper
Definition: FormViewHelper.php:52
‪TYPO3\CMS\Install\ViewHelpers\Form\TypoScriptConstantsViewHelper\renderOptionSelect
‪string renderOptionSelect(array $configuration)
Definition: TypoScriptConstantsViewHelper.php:168
‪TYPO3\CMS\Install\ViewHelpers\Form\TypoScriptConstantsViewHelper\renderHiddenFieldForEmptyValue
‪string renderHiddenFieldForEmptyValue(array $configuration)
Definition: TypoScriptConstantsViewHelper.php:310
‪TYPO3\CMS\Install\ViewHelpers\Form\TypoScriptConstantsViewHelper\getLanguageService
‪LanguageService null getLanguageService()
Definition: TypoScriptConstantsViewHelper.php:334
‪TYPO3\CMS\Install\ViewHelpers\Form\TypoScriptConstantsViewHelper\renderOffsetField
‪string renderOffsetField(array $configuration)
Definition: TypoScriptConstantsViewHelper.php:130
‪TYPO3\CMS\Install\ViewHelpers\Form\TypoScriptConstantsViewHelper\renderColorPicker
‪string renderColorPicker(array $configuration)
Definition: TypoScriptConstantsViewHelper.php:97
‪TYPO3\CMS\Install\ViewHelpers\Form\TypoScriptConstantsViewHelper
Definition: TypoScriptConstantsViewHelper.php:29
‪$output
‪$output
Definition: annotationChecker.php:113
‪TYPO3\CMS\Install\ViewHelpers\Form\TypoScriptConstantsViewHelper\renderUserFunction
‪string renderUserFunction(array $configuration)
Definition: TypoScriptConstantsViewHelper.php:282
‪$GLOBALS
‪$GLOBALS['TYPO3_CONF_VARS']['EXTCONF']['adminpanel']['modules']
Definition: ext_localconf.php:5
‪TYPO3\CMS\Install\ViewHelpers\Form\TypoScriptConstantsViewHelper\addIdAttribute
‪addIdAttribute(array $configuration)
Definition: TypoScriptConstantsViewHelper.php:344
‪TYPO3\CMS\Install\ViewHelpers\Form\TypoScriptConstantsViewHelper\render
‪string render()
Definition: TypoScriptConstantsViewHelper.php:78
‪TYPO3\CMS\Core\Localization\LanguageService
Definition: LanguageService.php:29
‪TYPO3\CMS\Install\ViewHelpers\Form\TypoScriptConstantsViewHelper\renderCheckbox
‪string renderCheckbox(array $configuration)
Definition: TypoScriptConstantsViewHelper.php:263
‪TYPO3\CMS\Core\Utility\GeneralUtility
Definition: GeneralUtility.php:45
‪TYPO3\CMS\Install\ViewHelpers\Form
Definition: TypoScriptConstantsViewHelper.php:3
‪TYPO3\CMS\Install\ViewHelpers\Form\TypoScriptConstantsViewHelper\renderSmallTextField
‪string renderSmallTextField(array $configuration)
Definition: TypoScriptConstantsViewHelper.php:252