‪TYPO3CMS  10.4
TypoScriptConstantsViewHelper.php
Go to the documentation of this file.
1 <?php
2 
3 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 
19 
23 use TYPO3Fluid\Fluid\Core\ViewHelper\AbstractTagBasedViewHelper;
24 use TYPO3Fluid\Fluid\Core\ViewHelper\TagBuilder;
25 
33 class ‪TypoScriptConstantsViewHelper extends AbstractTagBasedViewHelper
34 {
38  public ‪$viewHelperMapping = [
39  'int' => 'renderIntegerField',
40  'int+' => 'renderPositiveIntegerField',
41  'integer' => 'renderIntegerField',
42  'color' => 'renderColorPicker',
43  'wrap' => 'renderWrapField',
44  'offset' => 'renderOffsetField',
45  'options' => 'renderOptionSelect',
46  'boolean' => 'renderCheckbox',
47  'user' => 'renderUserFunction',
48  'small' => 'renderSmallTextField',
49  'string' => 'renderTextField',
50  'input' => 'renderTextField', // only for backwards compatibility, many extensions depend on that
51  'default' => 'renderTextField' // only for backwards compatibility, many extensions depend on that
52  ];
53 
57  public ‪$tagName = 'input';
58 
62  public function ‪initializeArguments()
63  {
64  parent::initializeArguments();
65  $this->registerArgument('name', 'string', 'Name of input tag');
66  $this->registerArgument('value', 'mixed', 'Value of input tag');
67  $this->registerArgument(
68  'configuration',
69  'array',
70  'The TypoScript constant configuration, e.g. labels, category, type and value.',
71  true
72  );
73  $this->registerUniversalTagAttributes();
74  }
75 
79  public function ‪initialize()
80  {
81  $this->setTagBuilder(new TagBuilder($this->tagName));
82  parent::initialize();
83  }
84 
90  public function ‪render(): string
91  {
93  $configuration = $this->arguments['configuration'];
94  if (isset($this->viewHelperMapping[$configuration['type']]) && method_exists($this, $this->viewHelperMapping[$configuration['type']])) {
95  $input = $this->{$this->viewHelperMapping[$configuration['type']]}($configuration);
96  } else {
97  $input = $this->{$this->viewHelperMapping['default']}($configuration);
98  }
99 
100  return $input;
101  }
102 
109  protected function ‪renderColorPicker(array $configuration): string
110  {
111  $elementName = $this->‪getFieldName($configuration);
112 
113  // configure the field
114  $this->tag->setTagName('input');
115  $this->tag->addAttribute('type', 'text');
116  $this->‪addIdAttribute($configuration);
117  $this->tag->addAttribute('name', $elementName);
118  $this->tag->addAttribute('data-formengine-input-name', $elementName);
119  $this->tag->addAttribute('class', 'form-control');
120  if ($configuration['value'] !== null) {
121  $this->tag->addAttribute('value', $configuration['value']);
122  }
123 
124  ‪$output = '
125  <div class="form-wizards-element">
126  <input class="form-control t3js-color-input formengine-colorpickerelement t3js-color-picker" type="text"
127  name="' . htmlspecialchars($elementName) . '" value="' . $this->tag->getAttribute('value') . '"/>
128  <script>
129  require([\'TYPO3/CMS/Backend/ColorPicker\'], function(ColorPicker){ColorPicker.initialize()});
130  </script>
131  </div>';
132 
133  return ‪$output;
134  }
135 
142  protected function ‪renderOffsetField(array $configuration): string
143  {
144  $this->tag->setTagName('input');
145  $this->tag->addAttribute('type', 'text');
146  $this->‪addIdAttribute($configuration);
147  $this->tag->addAttribute('name', $this->‪getFieldName($configuration));
148  $this->tag->addAttribute('class', 'form-control t3js-emconf-offset');
149  if ($configuration['value'] !== null) {
150  $this->tag->addAttribute('value', $configuration['value']);
151  }
152  return $this->tag->render();
153  }
154 
161  protected function ‪renderWrapField(array $configuration): string
162  {
163  $this->tag->setTagName('input');
164  $this->tag->addAttribute('type', 'text');
165  $this->‪addIdAttribute($configuration);
166  $this->tag->addAttribute('name', $this->‪getFieldName($configuration));
167  $this->tag->addAttribute('class', 'form-control t3js-emconf-wrap');
168  if ($configuration['value'] !== null) {
169  $this->tag->addAttribute('value', $configuration['value']);
170  }
171  return $this->tag->render();
172  }
173 
180  protected function ‪renderOptionSelect(array $configuration): string
181  {
182  $this->tag->setTagName('select');
183  $this->‪addIdAttribute($configuration);
184  $this->tag->addAttribute('name', $this->‪getFieldName($configuration));
185  $this->tag->addAttribute('class', 'form-control');
186  $optionValueArray = $configuration['generic'];
187  ‪$output = '';
188  $languageService = $this->‪getLanguageService();
189  foreach ($optionValueArray as $label => $value) {
190  ‪$output .= '<option value="' . htmlspecialchars($value) . '"';
191  if ($configuration['value'] == $value) {
192  ‪$output .= ' selected="selected"';
193  }
194  ‪$output .= '>' . htmlspecialchars($languageService->sL($label)) . '</option>';
195  }
196  $this->tag->setContent(‪$output);
197  return $this->tag->render();
198  }
199 
206  protected function ‪renderPositiveIntegerField(array $configuration): string
207  {
208  $this->tag->setTagName('input');
209  $this->tag->addAttribute('type', 'number');
210  $this->‪addIdAttribute($configuration);
211  $this->tag->addAttribute('name', $this->‪getFieldName($configuration));
212  $this->tag->addAttribute('class', 'form-control');
213  $this->tag->addAttribute('min', '0');
214  if ($configuration['value'] !== null) {
215  $this->tag->addAttribute('value', $configuration['value']);
216  }
217  return $this->tag->render();
218  }
219 
226  protected function ‪renderIntegerField(array $configuration): string
227  {
228  $this->tag->setTagName('input');
229  $this->tag->addAttribute('type', 'number');
230  $this->‪addIdAttribute($configuration);
231  $this->tag->addAttribute('name', $this->‪getFieldName($configuration));
232  $this->tag->addAttribute('class', 'form-control');
233  if ($configuration['value'] !== null) {
234  $this->tag->addAttribute('value', $configuration['value']);
235  }
236  return $this->tag->render();
237  }
238 
245  protected function ‪renderTextField(array $configuration): string
246  {
247  $this->tag->setTagName('input');
248  $this->tag->addAttribute('type', 'text');
249  $this->‪addIdAttribute($configuration);
250  $this->tag->addAttribute('name', $this->‪getFieldName($configuration));
251  $this->tag->addAttribute('class', 'form-control');
252  if ($configuration['value'] !== null) {
253  $this->tag->addAttribute('value', $configuration['value']);
254  }
255  return $this->tag->render();
256  }
257 
264  protected function ‪renderSmallTextField(array $configuration): string
265  {
266  return $this->‪renderTextField($configuration);
267  }
268 
275  public function ‪renderCheckbox(array $configuration): string
276  {
277  $this->tag->addAttribute('type', 'checkbox');
278  $this->tag->addAttribute('name', $this->‪getFieldName($configuration));
279  $this->tag->addAttribute('value', 1);
280  $this->‪addIdAttribute($configuration);
281  if ($configuration['value'] == 1) {
282  $this->tag->addAttribute('checked', 'checked');
283  }
284  $hiddenField = $this->‪renderHiddenFieldForEmptyValue($configuration);
285  return '<div class="checkbox">' . $hiddenField . '<label>' . $this->tag->render() . '</label></div>';
286  }
287 
294  protected function ‪renderUserFunction(array $configuration): string
295  {
296  $userFunction = $configuration['generic'];
297  $userFunctionParams = [
298  'fieldName' => $this->‪getFieldName($configuration),
299  'fieldValue' => $configuration['value'],
300  'propertyName' => $configuration['name']
301  ];
302  return (string)GeneralUtility::callUserFunction($userFunction, $userFunctionParams, $this);
303  }
304 
311  protected function ‪getFieldName(array $configuration): string
312  {
313  return $configuration['name'];
314  }
315 
322  protected function ‪renderHiddenFieldForEmptyValue(array $configuration): string
323  {
324  $hiddenFieldNames = [];
325 
326  // check for already set hidden field within current extension
327  $variableKey = 'renderedHiddenFields-' . $configuration['extensionKey'];
328  if ($this->renderingContext->getViewHelperVariableContainer()->exists(FormViewHelper::class, $variableKey)) {
329  $hiddenFieldNames = $this->renderingContext->getViewHelperVariableContainer()->get(FormViewHelper::class, $variableKey);
330  }
331  $fieldName = $this->‪getFieldName($configuration);
332  if (substr($fieldName, -2) === '[]') {
333  $fieldName = substr($fieldName, 0, -2);
334  }
335  if (!in_array($fieldName, $hiddenFieldNames)) {
336  $hiddenFieldNames[] = $fieldName;
337  $this->renderingContext->getViewHelperVariableContainer()->addOrUpdate(FormViewHelper::class, $variableKey, $hiddenFieldNames);
338  return '<input type="hidden" name="' . htmlspecialchars($fieldName) . '" value="0" />';
339  }
340  return '';
341  }
342 
346  protected function ‪getLanguageService()
347  {
348  return ‪$GLOBALS['LANG'];
349  }
350 
356  protected function ‪addIdAttribute(array $configuration): void
357  {
358  $this->tag->addAttribute(
359  'id',
360  'em-' . $configuration['extensionKey'] . '-' . $this->‪getFieldName($configuration)
361  );
362  }
363 }
‪TYPO3\CMS\Core\ViewHelpers\Form\TypoScriptConstantsViewHelper\initialize
‪initialize()
Definition: TypoScriptConstantsViewHelper.php:77
‪TYPO3\CMS\Core\ViewHelpers\Form\TypoScriptConstantsViewHelper\renderIntegerField
‪string renderIntegerField(array $configuration)
Definition: TypoScriptConstantsViewHelper.php:224
‪TYPO3\CMS\Core\ViewHelpers\Form\TypoScriptConstantsViewHelper\$viewHelperMapping
‪array $viewHelperMapping
Definition: TypoScriptConstantsViewHelper.php:37
‪TYPO3\CMS\Core\ViewHelpers\Form\TypoScriptConstantsViewHelper\renderUserFunction
‪string renderUserFunction(array $configuration)
Definition: TypoScriptConstantsViewHelper.php:292
‪TYPO3\CMS\Core\ViewHelpers\Form\TypoScriptConstantsViewHelper\renderTextField
‪string renderTextField(array $configuration)
Definition: TypoScriptConstantsViewHelper.php:243
‪TYPO3\CMS\Core\ViewHelpers\Form\TypoScriptConstantsViewHelper\$tagName
‪string $tagName
Definition: TypoScriptConstantsViewHelper.php:55
‪TYPO3\CMS\Core\ViewHelpers\Form\TypoScriptConstantsViewHelper\getFieldName
‪string getFieldName(array $configuration)
Definition: TypoScriptConstantsViewHelper.php:309
‪TYPO3\CMS\Core\ViewHelpers\Form\TypoScriptConstantsViewHelper\renderPositiveIntegerField
‪string renderPositiveIntegerField(array $configuration)
Definition: TypoScriptConstantsViewHelper.php:204
‪TYPO3\CMS\Fluid\ViewHelpers\FormViewHelper
Definition: FormViewHelper.php:61
‪TYPO3\CMS\Core\ViewHelpers\Form\TypoScriptConstantsViewHelper\renderHiddenFieldForEmptyValue
‪string renderHiddenFieldForEmptyValue(array $configuration)
Definition: TypoScriptConstantsViewHelper.php:320
‪TYPO3\CMS\Core\ViewHelpers\Form\TypoScriptConstantsViewHelper\renderOptionSelect
‪string renderOptionSelect(array $configuration)
Definition: TypoScriptConstantsViewHelper.php:178
‪TYPO3\CMS\Core\ViewHelpers\Form\TypoScriptConstantsViewHelper\renderOffsetField
‪string renderOffsetField(array $configuration)
Definition: TypoScriptConstantsViewHelper.php:140
‪TYPO3\CMS\Core\ViewHelpers\Form\TypoScriptConstantsViewHelper
Definition: TypoScriptConstantsViewHelper.php:34
‪TYPO3\CMS\Core\ViewHelpers\Form\TypoScriptConstantsViewHelper\addIdAttribute
‪addIdAttribute(array $configuration)
Definition: TypoScriptConstantsViewHelper.php:354
‪TYPO3\CMS\Core\ViewHelpers\Form\TypoScriptConstantsViewHelper\renderWrapField
‪string renderWrapField(array $configuration)
Definition: TypoScriptConstantsViewHelper.php:159
‪TYPO3\CMS\Core\ViewHelpers\Form
Definition: TypoScriptConstantsViewHelper.php:18
‪TYPO3\CMS\Core\ViewHelpers\Form\TypoScriptConstantsViewHelper\getLanguageService
‪LanguageService null getLanguageService()
Definition: TypoScriptConstantsViewHelper.php:344
‪TYPO3\CMS\Core\ViewHelpers\Form\TypoScriptConstantsViewHelper\renderCheckbox
‪string renderCheckbox(array $configuration)
Definition: TypoScriptConstantsViewHelper.php:273
‪$output
‪$output
Definition: annotationChecker.php:119
‪TYPO3\CMS\Core\ViewHelpers\Form\TypoScriptConstantsViewHelper\initializeArguments
‪initializeArguments()
Definition: TypoScriptConstantsViewHelper.php:60
‪$GLOBALS
‪$GLOBALS['TYPO3_CONF_VARS']['EXTCONF']['adminpanel']['modules']
Definition: ext_localconf.php:5
‪TYPO3\CMS\Core\Localization\LanguageService
Definition: LanguageService.php:42
‪TYPO3\CMS\Core\Utility\GeneralUtility
Definition: GeneralUtility.php:46
‪TYPO3\CMS\Core\ViewHelpers\Form\TypoScriptConstantsViewHelper\render
‪string render()
Definition: TypoScriptConstantsViewHelper.php:88
‪TYPO3\CMS\Core\ViewHelpers\Form\TypoScriptConstantsViewHelper\renderSmallTextField
‪string renderSmallTextField(array $configuration)
Definition: TypoScriptConstantsViewHelper.php:262
‪TYPO3\CMS\Core\ViewHelpers\Form\TypoScriptConstantsViewHelper\renderColorPicker
‪string renderColorPicker(array $configuration)
Definition: TypoScriptConstantsViewHelper.php:107