‪TYPO3CMS  ‪main
EmailElement.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 
24 
29 {
35  protected ‪$defaultFieldInformation = [
36  'tcaDescription' => [
37  'renderType' => 'tcaDescription',
38  ],
39  ];
40 
46  protected ‪$defaultFieldWizard = [
47  'localizationStateSelector' => [
48  'renderType' => 'localizationStateSelector',
49  ],
50  'otherLanguageContent' => [
51  'renderType' => 'otherLanguageContent',
52  'after' => [
53  'localizationStateSelector',
54  ],
55  ],
56  'defaultLanguageDifferences' => [
57  'renderType' => 'defaultLanguageDifferences',
58  'after' => [
59  'otherLanguageContent',
60  ],
61  ],
62  ];
63 
69  public function ‪render(): array
70  {
71  $table = $this->data['tableName'];
72  $fieldName = $this->data['fieldName'];
73  $parameterArray = $this->data['parameterArray'];
74  $resultArray = $this->‪initializeResultArray();
75  $config = $parameterArray['fieldConf']['config'];
76 
77  $itemValue = $parameterArray['itemFormElValue'];
78  $width = $this->‪formMaxWidth(
79  ‪MathUtility::forceIntegerInRange($config['size'] ?? $this->defaultInputWidth, $this->minimumInputWidth, $this->maxInputWidth)
80  );
81  $fieldId = ‪StringUtility::getUniqueId('formengine-input-');
82  $itemName = (string)$parameterArray['itemFormElName'];
83  $renderedLabel = $this->‪renderLabel($fieldId);
84 
85  $fieldInformationResult = $this->‪renderFieldInformation();
86  $fieldInformationHtml = $fieldInformationResult['html'];
87  $resultArray = $this->‪mergeChildReturnIntoExistingResult($resultArray, $fieldInformationResult, false);
88 
89  if ($config['readOnly'] ?? false) {
90  $html = [];
91  $html[] = $renderedLabel;
92  $html[] = '<div class="formengine-field-item t3js-formengine-field-item">';
93  $html[] = $fieldInformationHtml;
94  $html[] = '<div class="form-wizards-wrap">';
95  $html[] = '<div class="form-wizards-element">';
96  $html[] = '<div class="form-control-wrap" style="max-width: ' . $width . 'px">';
97  $html[] = '<input class="form-control" id="' . htmlspecialchars($fieldId) . '" name="' . htmlspecialchars($itemName) . '" value="' . htmlspecialchars((string)$itemValue) . '" type="text" disabled>';
98  $html[] = '</div>';
99  $html[] = '</div>';
100  $html[] = '</div>';
101  $html[] = '</div>';
102  $resultArray['html'] = implode(LF, $html);
103  return $resultArray;
104  }
105 
106  $languageService = $this->‪getLanguageService();
107 
108  // Get filtered eval list, while always adding "trim"
109  $evalList = array_merge(array_filter(
110  ‪GeneralUtility::trimExplode(',', $config['eval'] ?? '', true),
111  static fn(string $value): bool => in_array($value, ['unique', 'uniqueInPid'], true)
112  ), ['trim']);
113 
114  if ($config['nullable'] ?? false) {
115  $evalList[] = 'null';
116  }
117 
118  $attributes = [
119  'value' => '',
120  'id' => $fieldId,
121  'maxlength' => '254',
122  'class' => implode(' ', [
123  'form-control',
124  'form-control-clearable',
125  't3js-clearable',
126  ]),
127  'data-formengine-validation-rules' => $this->‪getValidationDataAsJsonString($config),
128  'data-formengine-input-params' => (string)json_encode([
129  'field' => $itemName,
130  'evalList' => implode(',', $evalList),
131  ], JSON_THROW_ON_ERROR),
132  'data-formengine-input-name' => $itemName,
133  ];
134 
135  if (!empty($config['placeholder'])) {
136  $attributes['placeholder'] = trim($config['placeholder']);
137  }
138  if (isset($config['autocomplete'])) {
139  $attributes['autocomplete'] = empty($config['autocomplete']) ? 'new-' . $fieldName : 'on';
140  }
141 
142  $valuePickerHtml = [];
143  if (is_array($config['valuePicker']['items'] ?? false)) {
144  $valuePickerConfiguration = [
145  'mode' => $config['valuePicker']['mode'] ?? 'replace',
146  'linked-field' => '[data-formengine-input-name="' . $itemName . '"]',
147  ];
148  $valuePickerAttributes = array_merge(
149  [
150  'class' => 'form-select form-control-adapt',
151  ],
152  $this->getOnFieldChangeAttrs('change', $parameterArray['fieldChangeFunc'] ?? [])
153  );
154 
155  $valuePickerHtml[] = '<typo3-formengine-valuepicker ' . GeneralUtility::implodeAttributes($valuePickerConfiguration, true) . '>';
156  $valuePickerHtml[] = '<select ' . GeneralUtility::implodeAttributes($valuePickerAttributes, true) . '>';
157  $valuePickerHtml[] = '<option></option>';
158  foreach ($config['valuePicker']['items'] as $item) {
159  $valuePickerHtml[] = '<option value="' . htmlspecialchars((string)$item[1]) . '">' . htmlspecialchars($languageService->sL($item[0])) . '</option>';
160  }
161  $valuePickerHtml[] = '</select>';
162  $valuePickerHtml[] = '</typo3-formengine-valuepicker>';
163 
164  $resultArray['javaScriptModules'][] = ‪JavaScriptModuleInstruction::create('@typo3/backend/form-engine/field-wizard/value-picker.js');
165  }
166 
167  $fieldControlResult = $this->‪renderFieldControl();
168  $fieldControlHtml = $fieldControlResult['html'];
169  $resultArray = $this->‪mergeChildReturnIntoExistingResult($resultArray, $fieldControlResult, false);
170 
171  $fieldWizardResult = $this->‪renderFieldWizard();
172  $fieldWizardHtml = $fieldWizardResult['html'];
173  $resultArray = $this->‪mergeChildReturnIntoExistingResult($resultArray, $fieldWizardResult, false);
174 
175  $mainFieldHtml = [];
176  $mainFieldHtml[] = '<div class="form-control-wrap" style="max-width: ' . $width . 'px">';
177  $mainFieldHtml[] = '<div class="form-wizards-wrap">';
178  $mainFieldHtml[] = '<div class="form-wizards-element">';
179  $mainFieldHtml[] = '<input type="email" ' . GeneralUtility::implodeAttributes($attributes, true) . ' />';
180  $mainFieldHtml[] = '<input type="hidden" name="' . $itemName . '" value="' . htmlspecialchars((string)$itemValue) . '" />';
181  $mainFieldHtml[] = '</div>';
182  if (!empty($valuePickerHtml) || !empty($fieldControlHtml)) {
183  $mainFieldHtml[] = '<div class="form-wizards-items-aside form-wizards-items-aside--field-control">';
184  $mainFieldHtml[] = '<div class="btn-group">';
185  $mainFieldHtml[] = implode(LF, $valuePickerHtml);
186  $mainFieldHtml[] = $fieldControlHtml;
187  $mainFieldHtml[] = '</div>';
188  $mainFieldHtml[] = '</div>';
189  }
190  if (!empty($fieldWizardHtml)) {
191  $mainFieldHtml[] = '<div class="form-wizards-items-bottom">';
192  $mainFieldHtml[] = $fieldWizardHtml;
193  $mainFieldHtml[] = '</div>';
194  }
195  $mainFieldHtml[] = '</div>';
196  $mainFieldHtml[] = '</div>';
197  $mainFieldHtml = implode(LF, $mainFieldHtml);
198 
199  $nullControlNameEscaped = htmlspecialchars('control[active][' . $table . '][' . $this->data['databaseRow']['uid'] . '][' . $fieldName . ']');
200 
201  $fullElement = $mainFieldHtml;
202  if ($this->‪hasNullCheckboxButNoPlaceholder()) {
203  $checked = $itemValue !== null ? ' checked="checked"' : '';
204  $fullElement = [];
205  $fullElement[] = '<div class="t3-form-field-disable"></div>';
206  $fullElement[] = '<div class="form-check t3-form-field-eval-null-checkbox">';
207  $fullElement[] = '<input type="hidden" name="' . $nullControlNameEscaped . '" value="0" />';
208  $fullElement[] = '<input type="checkbox" class="form-check-input" name="' . $nullControlNameEscaped . '" id="' . $nullControlNameEscaped . '" value="1"' . $checked . ' />';
209  $fullElement[] = '<label class="form-check-label" for="' . $nullControlNameEscaped . '">';
210  $fullElement[] = $languageService->sL('LLL:EXT:core/Resources/Private/Language/locallang_core.xlf:labels.nullCheckbox');
211  $fullElement[] = '</label>';
212  $fullElement[] = '</div>';
213  $fullElement[] = $mainFieldHtml;
214  $fullElement = implode(LF, $fullElement);
215  } elseif ($this->‪hasNullCheckboxWithPlaceholder()) {
216  $checked = $itemValue !== null ? ' checked="checked"' : '';
217  $placeholder = $shortenedPlaceholder = trim((string)($config['placeholder'] ?? ''));
218  if ($placeholder !== '') {
219  $shortenedPlaceholder = ‪GeneralUtility::fixed_lgd_cs($placeholder, 20);
220  if ($placeholder !== $shortenedPlaceholder) {
221  $overrideLabel = sprintf(
222  $languageService->sL('LLL:EXT:core/Resources/Private/Language/locallang_core.xlf:labels.placeholder.override'),
223  '<span title="' . htmlspecialchars($placeholder) . '">' . htmlspecialchars($shortenedPlaceholder) . '</span>'
224  );
225  } else {
226  $overrideLabel = sprintf(
227  $languageService->sL('LLL:EXT:core/Resources/Private/Language/locallang_core.xlf:labels.placeholder.override'),
228  htmlspecialchars($placeholder)
229  );
230  }
231  } else {
232  $overrideLabel = $languageService->sL(
233  'LLL:EXT:core/Resources/Private/Language/locallang_core.xlf:labels.placeholder.override_not_available'
234  );
235  }
236  $fullElement = [];
237  $fullElement[] = '<div class="form-check t3js-form-field-eval-null-placeholder-checkbox">';
238  $fullElement[] = '<input type="hidden" name="' . $nullControlNameEscaped . '" value="0" />';
239  $fullElement[] = '<input type="checkbox" class="form-check-input" name="' . $nullControlNameEscaped . '" id="' . $nullControlNameEscaped . '" value="1"' . $checked . ' />';
240  $fullElement[] = '<label class="form-check-label" for="' . $nullControlNameEscaped . '">';
241  $fullElement[] = $overrideLabel;
242  $fullElement[] = '</label>';
243  $fullElement[] = '</div>';
244  $fullElement[] = '<div class="t3js-formengine-placeholder-placeholder">';
245  $fullElement[] = '<div class="form-control-wrap" style="max-width:' . $width . 'px">';
246  $fullElement[] = '<input type="text" class="form-control" disabled="disabled" value="' . htmlspecialchars($shortenedPlaceholder) . '" />';
247  $fullElement[] = '</div>';
248  $fullElement[] = '</div>';
249  $fullElement[] = '<div class="t3js-formengine-placeholder-formfield">';
250  $fullElement[] = $mainFieldHtml;
251  $fullElement[] = '</div>';
252  $fullElement = implode(LF, $fullElement);
253  }
254 
255  $resultArray['html'] = $renderedLabel . '
256  <div class="formengine-field-item t3js-formengine-field-item">
257  ' . $fieldInformationHtml . $fullElement . '
258  </div>';
259 
260  return $resultArray;
261  }
262 }
‪TYPO3\CMS\Backend\Form\Element\AbstractFormElement\renderFieldInformation
‪array renderFieldInformation()
Definition: AbstractFormElement.php:73
‪TYPO3\CMS\Core\Utility\GeneralUtility\fixed_lgd_cs
‪static string fixed_lgd_cs(string $string, int $chars, string $appendString='...')
Definition: GeneralUtility.php:92
‪TYPO3\CMS\Backend\Form\AbstractNode\mergeChildReturnIntoExistingResult
‪array mergeChildReturnIntoExistingResult(array $existing, array $childReturn, bool $mergeHtml=true)
Definition: AbstractNode.php:104
‪TYPO3\CMS\Backend\Form\Element\EmailElement\$defaultFieldInformation
‪array $defaultFieldInformation
Definition: EmailElement.php:34
‪TYPO3\CMS\Backend\Form\Element\AbstractFormElement
Definition: AbstractFormElement.php:37
‪TYPO3\CMS\Core\Page\JavaScriptModuleInstruction
Definition: JavaScriptModuleInstruction.php:23
‪TYPO3\CMS\Backend\Form\Element
Definition: AbstractFormElement.php:16
‪TYPO3\CMS\Backend\Form\Element\EmailElement\render
‪array render()
Definition: EmailElement.php:67
‪TYPO3\CMS\Backend\Form\Element\EmailElement
Definition: EmailElement.php:29
‪TYPO3\CMS\Backend\Form\Element\AbstractFormElement\renderFieldControl
‪array renderFieldControl()
Definition: AbstractFormElement.php:89
‪TYPO3\CMS\Backend\Form\Element\AbstractFormElement\getLanguageService
‪getLanguageService()
Definition: AbstractFormElement.php:456
‪TYPO3\CMS\Backend\Form\Element\EmailElement\$defaultFieldWizard
‪array $defaultFieldWizard
Definition: EmailElement.php:44
‪TYPO3\CMS\Backend\Form\Element\AbstractFormElement\formMaxWidth
‪int formMaxWidth($size=48)
Definition: AbstractFormElement.php:332
‪TYPO3\CMS\Backend\Form\AbstractNode\getValidationDataAsJsonString
‪getValidationDataAsJsonString(array $config)
Definition: AbstractNode.php:133
‪TYPO3\CMS\Backend\Form\Element\AbstractFormElement\renderLabel
‪renderLabel(string $for)
Definition: AbstractFormElement.php:119
‪TYPO3\CMS\Core\Utility\MathUtility
Definition: MathUtility.php:24
‪TYPO3\CMS\Core\Page\JavaScriptModuleInstruction\create
‪static create(string $name, ?string $exportName=null)
Definition: JavaScriptModuleInstruction.php:47
‪TYPO3\CMS\Core\Utility\MathUtility\forceIntegerInRange
‪static int forceIntegerInRange(mixed $theInt, int $min, int $max=2000000000, int $defaultValue=0)
Definition: MathUtility.php:34
‪TYPO3\CMS\Core\Utility\GeneralUtility
Definition: GeneralUtility.php:52
‪TYPO3\CMS\Core\Utility\StringUtility
Definition: StringUtility.php:24
‪TYPO3\CMS\Backend\Form\Element\AbstractFormElement\renderFieldWizard
‪array renderFieldWizard()
Definition: AbstractFormElement.php:105
‪TYPO3\CMS\Backend\Form\Element\AbstractFormElement\hasNullCheckboxWithPlaceholder
‪hasNullCheckboxWithPlaceholder()
Definition: AbstractFormElement.php:195
‪TYPO3\CMS\Core\Utility\GeneralUtility\trimExplode
‪static list< string > trimExplode(string $delim, string $string, bool $removeEmptyValues=false, int $limit=0)
Definition: GeneralUtility.php:822
‪TYPO3\CMS\Backend\Form\Element\AbstractFormElement\hasNullCheckboxButNoPlaceholder
‪hasNullCheckboxButNoPlaceholder()
Definition: AbstractFormElement.php:163
‪TYPO3\CMS\Core\Utility\StringUtility\getUniqueId
‪static getUniqueId(string $prefix='')
Definition: StringUtility.php:57
‪TYPO3\CMS\Backend\Form\AbstractNode\initializeResultArray
‪initializeResultArray()
Definition: AbstractNode.php:77