TYPO3 CMS  TYPO3_8-7
InputLinkElement.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 
31 
38 {
44  protected $defaultFieldControl = [
45  'linkPopup' => [
46  'renderType' => 'linkPopup',
47  'options' => []
48  ],
49  ];
50 
56  protected $defaultFieldWizard = [
57  'localizationStateSelector' => [
58  'renderType' => 'localizationStateSelector',
59  ],
60  'otherLanguageContent' => [
61  'renderType' => 'otherLanguageContent',
62  'after' => [
63  'localizationStateSelector'
64  ],
65  ],
66  'defaultLanguageDifferences' => [
67  'renderType' => 'defaultLanguageDifferences',
68  'after' => [
69  'otherLanguageContent',
70  ],
71  ],
72  ];
73 
79  public function render()
80  {
81  $languageService = $this->getLanguageService();
82 
83  $table = $this->data['tableName'];
84  $fieldName = $this->data['fieldName'];
85  $row = $this->data['databaseRow'];
86  $parameterArray = $this->data['parameterArray'];
87  $resultArray = $this->initializeResultArray();
88  $config = $parameterArray['fieldConf']['config'];
89 
90  $itemValue = $parameterArray['itemFormElValue'];
91  $evalList = GeneralUtility::trimExplode(',', $config['eval'], true);
92  $size = MathUtility::forceIntegerInRange($config['size'] ?? $this->defaultInputWidth, $this->minimumInputWidth, $this->maxInputWidth);
93  $width = (int)$this->formMaxWidth($size);
94  $nullControlNameEscaped = htmlspecialchars('control[active][' . $table . '][' . $row['uid'] . '][' . $fieldName . ']');
95 
96  if ($config['readOnly']) {
97  // Early return for read only fields
98  $html = [];
99  $html[] = '<div class="formengine-field-item t3js-formengine-field-item">';
100  $html[] = '<div class="form-wizards-wrap">';
101  $html[] = '<div class="form-wizards-element">';
102  $html[] = '<div class="form-control-wrap" style="max-width: ' . $width . 'px">';
103  $html[] = '<input class="form-control" value="' . htmlspecialchars($itemValue) . '" type="text" disabled>';
104  $html[] = '</div>';
105  $html[] = '</div>';
106  $html[] = '</div>';
107  $html[] = '</div>';
108  $resultArray['html'] = implode(LF, $html);
109  return $resultArray;
110  }
111 
112  // @todo: The whole eval handling is a mess and needs refactoring
113  foreach ($evalList as $func) {
114  // @todo: This is ugly: The code should find out on it's own whether a eval definition is a
115  // @todo: keyword like "date", or a class reference. The global registration could be dropped then
116  // Pair hook to the one in \TYPO3\CMS\Core\DataHandling\DataHandler::checkValue_input_Eval()
117  if (isset($GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['tce']['formevals'][$func])) {
118  if (class_exists($func)) {
119  $evalObj = GeneralUtility::makeInstance($func);
120  if (method_exists($evalObj, 'deevaluateFieldValue')) {
121  $_params = [
122  'value' => $itemValue
123  ];
124  $itemValue = $evalObj->deevaluateFieldValue($_params);
125  }
126  if (method_exists($evalObj, 'returnFieldJS')) {
127  $resultArray['additionalJavaScriptPost'][] = 'TBE_EDITOR.customEvalFunctions[' . GeneralUtility::quoteJSvalue($func) . ']'
128  . ' = function(value) {' . $evalObj->returnFieldJS() . '};';
129  }
130  }
131  }
132  }
133 
134  $attributes = [
135  'value' => '',
136  'id' => StringUtility::getUniqueId('formengine-input-'),
137  'class' => implode(' ', [
138  'form-control',
139  't3js-clearable',
140  't3js-form-field-inputlink-input',
141  'hidden',
142  'hasDefaultValue',
143  ]),
144  'data-formengine-validation-rules' => $this->getValidationDataAsJsonString($config),
145  'data-formengine-input-params' => json_encode([
146  'field' => $parameterArray['itemFormElName'],
147  'evalList' => implode(',', $evalList)
148  ]),
149  'data-formengine-input-name' => $parameterArray['itemFormElName'],
150  ];
151 
152  $maxLength = $config['max'] ?? 0;
153  if ((int)$maxLength > 0) {
154  $attributes['maxlength'] = (int)$maxLength;
155  }
156  if (!empty($config['placeholder'])) {
157  $attributes['placeholder'] = trim($config['placeholder']);
158  }
159  if (isset($config['autocomplete'])) {
160  $attributes['autocomplete'] = empty($config['autocomplete']) ? 'new-' . $fieldName : 'on';
161  }
162 
163  $valuePickerHtml = [];
164  if (isset($config['valuePicker']['items']) && is_array($config['valuePicker']['items'])) {
165  $mode = $config['valuePicker']['mode'] ?? '';
166  $itemName = $parameterArray['itemFormElName'];
167  $fieldChangeFunc = $parameterArray['fieldChangeFunc'];
168  if ($mode === 'append') {
169  $assignValue = 'document.querySelectorAll(' . GeneralUtility::quoteJSvalue('[data-formengine-input-name="' . $itemName . '"]') . ')[0]'
170  . '.value=\'\'+this.options[this.selectedIndex].value+document.editform[' . GeneralUtility::quoteJSvalue($itemName) . '].value';
171  } elseif ($mode === 'prepend') {
172  $assignValue = 'document.querySelectorAll(' . GeneralUtility::quoteJSvalue('[data-formengine-input-name="' . $itemName . '"]') . ')[0]'
173  . '.value+=\'\'+this.options[this.selectedIndex].value';
174  } else {
175  $assignValue = 'document.querySelectorAll(' . GeneralUtility::quoteJSvalue('[data-formengine-input-name="' . $itemName . '"]') . ')[0]'
176  . '.value=this.options[this.selectedIndex].value';
177  }
178  $valuePickerHtml[] = '<select';
179  $valuePickerHtml[] = ' class="form-control tceforms-select tceforms-wizardselect"';
180  $valuePickerHtml[] = ' onchange="' . htmlspecialchars($assignValue . ';this.blur();this.selectedIndex=0;' . implode('', $fieldChangeFunc)) . '"';
181  $valuePickerHtml[] = '>';
182  $valuePickerHtml[] = '<option></option>';
183  foreach ($config['valuePicker']['items'] as $item) {
184  $valuePickerHtml[] = '<option value="' . htmlspecialchars($item[1]) . '">' . htmlspecialchars($languageService->sL($item[0])) . '</option>';
185  }
186  $valuePickerHtml[] = '</select>';
187  }
188 
189  $legacyWizards = $this->renderWizards();
190  $legacyFieldControlHtml = implode(LF, $legacyWizards['fieldControl']);
191  $legacyFieldWizardHtml = implode(LF, $legacyWizards['fieldWizard']);
192 
193  $fieldInformationResult = $this->renderFieldInformation();
194  $fieldInformationHtml = $fieldInformationResult['html'];
195  $resultArray = $this->mergeChildReturnIntoExistingResult($resultArray, $fieldInformationResult, false);
196 
197  $fieldWizardResult = $this->renderFieldWizard();
198  $fieldWizardHtml = $legacyFieldWizardHtml . $fieldWizardResult['html'];
199  $resultArray = $this->mergeChildReturnIntoExistingResult($resultArray, $fieldWizardResult, false);
200 
201  $fieldControlResult = $this->renderFieldControl();
202  $fieldControlHtml = $legacyFieldControlHtml . $fieldControlResult['html'];
203  $resultArray = $this->mergeChildReturnIntoExistingResult($resultArray, $fieldControlResult, false);
204 
205  $linkExplanation = $this->getLinkExplanation($itemValue ?: '');
206  $explanation = htmlspecialchars($linkExplanation['text']);
207  $toggleButtonTitle = $languageService->sL('LLL:EXT:lang/Resources/Private/Language/locallang_core.xlf:buttons.toggleLinkExplanation');
208 
209  $expansionHtml = [];
210  $expansionHtml[] = '<div class="form-control-wrap" style="max-width: ' . $width . 'px">';
211  $expansionHtml[] = '<div class="form-wizards-wrap">';
212  $expansionHtml[] = '<div class="form-wizards-element">';
213  $expansionHtml[] = '<div class="input-group t3js-form-field-inputlink">';
214  $expansionHtml[] = '<span class="input-group-addon">' . $linkExplanation['icon'] . '</span>';
215  $expansionHtml[] = '<input class="form-control form-field-inputlink-explanation t3js-form-field-inputlink-explanation" data-toggle="tooltip" data-title="' . $explanation . '" value="' . $explanation . '" readonly>';
216  $expansionHtml[] = '<input type="text"' . GeneralUtility::implodeAttributes($attributes, true) . ' />';
217  $expansionHtml[] = '<span class="input-group-btn">';
218  $expansionHtml[] = '<button class="btn btn-default t3js-form-field-inputlink-explanation-toggle" type="button" title="' . htmlspecialchars($toggleButtonTitle) . '">';
219  $expansionHtml[] = $this->iconFactory->getIcon('actions-version-workspaces-preview-link', Icon::SIZE_SMALL)->render();
220  $expansionHtml[] = '</button>';
221  $expansionHtml[] = '</span>';
222  $expansionHtml[] = '<input type="hidden" name="' . $parameterArray['itemFormElName'] . '" value="' . htmlspecialchars($itemValue) . '" />';
223  $expansionHtml[] = '</div>';
224  $expansionHtml[] = '</div>';
225  $expansionHtml[] = '<div class="form-wizards-items-aside">';
226  $expansionHtml[] = '<div class="btn-group">';
227  $expansionHtml[] = implode(LF, $valuePickerHtml);
228  $expansionHtml[] = $fieldControlHtml;
229  $expansionHtml[] = '</div>';
230  $expansionHtml[] = '</div>';
231  $expansionHtml[] = '<div class="form-wizards-items-bottom">';
232  $expansionHtml[] = $linkExplanation['additionalAttributes'];
233  $expansionHtml[] = $fieldWizardHtml;
234  $expansionHtml[] = '</div>';
235  $expansionHtml[] = '</div>';
236  $expansionHtml[] = '</div>';
237  $expansionHtml = implode(LF, $expansionHtml);
238 
239  $fullElement = $expansionHtml;
240  if ($this->hasNullCheckboxButNoPlaceholder()) {
241  $checked = $itemValue !== null ? ' checked="checked"' : '';
242  $fullElement = [];
243  $fullElement[] = '<div class="t3-form-field-disable"></div>';
244  $fullElement[] = '<div class="checkbox t3-form-field-eval-null-checkbox">';
245  $fullElement[] = '<label for="' . $nullControlNameEscaped . '">';
246  $fullElement[] = '<input type="hidden" name="' . $nullControlNameEscaped . '" value="0" />';
247  $fullElement[] = '<input type="checkbox" name="' . $nullControlNameEscaped . '" id="' . $nullControlNameEscaped . '" value="1"' . $checked . ' />';
248  $fullElement[] = $languageService->sL('LLL:EXT:lang/Resources/Private/Language/locallang_core.xlf:labels.nullCheckbox');
249  $fullElement[] = '</label>';
250  $fullElement[] = '</div>';
251  $fullElement[] = $expansionHtml;
252  $fullElement = implode(LF, $fullElement);
253  } elseif ($this->hasNullCheckboxWithPlaceholder()) {
254  $checked = $itemValue !== null ? ' checked="checked"' : '';
255  $placeholder = $shortenedPlaceholder = $config['placeholder'] ?? '';
256  $disabled = '';
257  $fallbackValue = 0;
258  if (strlen($placeholder) > 0) {
259  $shortenedPlaceholder = GeneralUtility::fixed_lgd_cs($placeholder, 20);
260  if ($placeholder !== $shortenedPlaceholder) {
261  $overrideLabel = sprintf(
262  $languageService->sL('LLL:EXT:lang/Resources/Private/Language/locallang_core.xlf:labels.placeholder.override'),
263  '<span title="' . htmlspecialchars($placeholder) . '">' . htmlspecialchars($shortenedPlaceholder) . '</span>'
264  );
265  } else {
266  $overrideLabel = sprintf(
267  $languageService->sL('LLL:EXT:lang/Resources/Private/Language/locallang_core.xlf:labels.placeholder.override'),
268  htmlspecialchars($placeholder)
269  );
270  }
271  } else {
272  $overrideLabel = $languageService->sL(
273  'LLL:EXT:lang/Resources/Private/Language/locallang_core.xlf:labels.placeholder.override_not_available'
274  );
275  }
276  $fullElement = [];
277  $fullElement[] = '<div class="checkbox t3js-form-field-eval-null-placeholder-checkbox">';
278  $fullElement[] = '<label for="' . $nullControlNameEscaped . '">';
279  $fullElement[] = '<input type="hidden" name="' . $nullControlNameEscaped . '" value="' . $fallbackValue . '" />';
280  $fullElement[] = '<input type="checkbox" name="' . $nullControlNameEscaped . '" id="' . $nullControlNameEscaped . '" value="1"' . $checked . $disabled . ' />';
281  $fullElement[] = $overrideLabel;
282  $fullElement[] = '</label>';
283  $fullElement[] = '</div>';
284  $fullElement[] = '<div class="t3js-formengine-placeholder-placeholder">';
285  $fullElement[] = '<div class="form-control-wrap" style="max-width:' . $width . 'px">';
286  $fullElement[] = '<input type="text" class="form-control" disabled="disabled" value="' . $shortenedPlaceholder . '" />';
287  $fullElement[] = '</div>';
288  $fullElement[] = '</div>';
289  $fullElement[] = '<div class="t3js-formengine-placeholder-formfield">';
290  $fullElement[] = $expansionHtml;
291  $fullElement[] = '</div>';
292  $fullElement = implode(LF, $fullElement);
293  }
294 
295  $resultArray['html'] = '<div class="formengine-field-item t3js-formengine-field-item">' . $fieldInformationHtml . $fullElement . '</div>';
296  return $resultArray;
297  }
298 
303  protected function getLinkExplanation(string $itemValue): array
304  {
305  if (empty($itemValue)) {
306  return [];
307  }
308  $data = ['text' => '', 'icon' => ''];
309  $typolinkService = GeneralUtility::makeInstance(TypoLinkCodecService::class);
310  $linkParts = $typolinkService->decode($itemValue);
311  $linkService = GeneralUtility::makeInstance(LinkService::class);
312 
313  try {
314  $linkData = $linkService->resolve($linkParts['url']);
315  } catch (FileDoesNotExistException $e) {
316  return $data;
317  } catch (FolderDoesNotExistException $e) {
318  return $data;
319  } catch (UnknownLinkHandlerException $e) {
320  return $data;
321  } catch (InvalidPathException $e) {
322  return $data;
323  }
324 
325  // Resolving the TypoLink parts (class, title, params)
326  $additionalAttributes = [];
327  foreach ($linkParts as $key => $value) {
328  if ($key === 'url') {
329  continue;
330  }
331  if ($value) {
332  switch ($key) {
333  case 'class':
334  $label = $this->getLanguageService()->sL('LLL:EXT:lang/Resources/Private/Language/locallang_browse_links.xlf:class');
335  break;
336  case 'title':
337  $label = $this->getLanguageService()->sL('LLL:EXT:lang/Resources/Private/Language/locallang_browse_links.xlf:title');
338  break;
339  case 'additionalParams':
340  $label = $this->getLanguageService()->sL('LLL:EXT:lang/Resources/Private/Language/locallang_browse_links.xlf:params');
341  break;
342  default:
343  $label = $key;
344  }
345 
346  $additionalAttributes[] = '<span><strong>' . htmlspecialchars($label) . ': </strong> ' . htmlspecialchars($value) . '</span>';
347  }
348  }
349 
350  // Resolve the actual link
351  switch ($linkData['type']) {
353  $pageRecord = BackendUtility::readPageAccess($linkData['pageuid'], '1=1');
354  // Is this a real page
355  if ($pageRecord['uid']) {
356  $data = [
357  'text' => $pageRecord['_thePathFull'] . '[' . $pageRecord['uid'] . ']',
358  'icon' => $this->iconFactory->getIconForRecord('pages', $pageRecord, Icon::SIZE_SMALL)->render()
359  ];
360  }
361  break;
363  $data = [
364  'text' => $linkData['email'],
365  'icon' => $this->iconFactory->getIcon('content-elements-mailform', Icon::SIZE_SMALL)->render()
366  ];
367  break;
369  $data = [
370  'text' => $this->getDomainByUrl($linkData['url']),
371  'icon' => $this->iconFactory->getIcon('apps-pagetree-page-shortcut-external', Icon::SIZE_SMALL)->render()
372 
373  ];
374  break;
377  $file = $linkData['file'];
378  if ($file) {
379  $data = [
380  'text' => $file->getPublicUrl(),
381  'icon' => $this->iconFactory->getIconForFileExtension($file->getExtension(), Icon::SIZE_SMALL)->render()
382  ];
383  }
384  break;
387  $folder = $linkData['folder'];
388  if ($folder) {
389  $data = [
390  'text' => $folder->getPublicUrl(),
391  'icon' => $this->iconFactory->getIcon('apps-filetree-folder-default', Icon::SIZE_SMALL)->render()
392  ];
393  }
394  break;
396  $table = $this->data['pageTsConfig']['TCEMAIN.']['linkHandler.'][$linkData['identifier'] . '.']['configuration.']['table'];
397  $record = BackendUtility::getRecord($table, $linkData['uid']);
398  if ($record) {
399  $recordTitle = BackendUtility::getRecordTitle($table, $record);
400  $tableTitle = $this->getLanguageService()->sL($GLOBALS['TCA'][$table]['ctrl']['title']);
401  $data = [
402  'text' => sprintf('%s [%s:%d]', $recordTitle, $tableTitle, $linkData['uid']),
403  'icon' => $this->iconFactory->getIconForRecord($table, $record, Icon::SIZE_SMALL)->render(),
404  ];
405  } else {
406  $icon = $GLOBALS['TCA'][$table]['ctrl']['typeicon_classes']['default'];
407  if (empty($icon)) {
408  $icon = 'tcarecords-' . $table . '-default';
409  }
410  $data = [
411  'text' => sprintf('%s', $linkData['uid']),
412  'icon' => $this->iconFactory->getIcon('tcarecords-' . $table . '-default', Icon::SIZE_SMALL, 'overlay-missing')->render(),
413  ];
414  }
415  break;
416  default:
417  // Please note that this hook is preliminary and might change, as this element could become its own
418  // TCA type in the future
419  if (isset($GLOBALS['TYPO3_CONF_VARS']['SYS']['formEngine']['linkHandler'][$linkData['type']])) {
420  $linkBuilder = GeneralUtility::makeInstance($GLOBALS['TYPO3_CONF_VARS']['SYS']['formEngine']['linkHandler'][$linkData['type']]);
421  $data = $linkBuilder->getFormData($linkData, $linkParts, $this->data, $this);
422  } else {
423  $data = [
424  'text' => 'not implemented type ' . $linkData['type'],
425  'icon' => ''
426  ];
427  }
428  }
429 
430  $data['additionalAttributes'] = '<div class="help-block">' . implode(' - ', $additionalAttributes) . '</div>';
431  return $data;
432  }
433 
439  protected function getDomainByUrl(string $uriString): string
440  {
441  $data = parse_url($uriString);
442  return $data['host'] ?? '';
443  }
444 
448  protected function getLanguageService()
449  {
450  return $GLOBALS['LANG'];
451  }
452 }
static readPageAccess($id, $perms_clause)
static implodeAttributes(array $arr, $xhtmlSafe=false, $dontOmitBlankAttribs=false)
static forceIntegerInRange($theInt, $min, $max=2000000000, $defaultValue=0)
Definition: MathUtility.php:31
renderWizards( $itemKinds=null, $wizConf=null, $table=null, $row=null, $fieldName=null, $PA=null, $itemName=null, $specConf=null, $RTE=null)
static trimExplode($delim, $string, $removeEmptyValues=false, $limit=0)
static makeInstance($className,... $constructorArguments)
static getRecordTitle($table, $row, $prep=false, $forceResult=true)
static fixed_lgd_cs($string, $chars, $appendString='...')
mergeChildReturnIntoExistingResult(array $existing, array $childReturn, bool $mergeHtml=true)
static getRecord($table, $uid, $fields=' *', $where='', $useDeleteClause=true)
if(TYPO3_MODE==='BE') $GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['t3lib/class.t3lib_tsfebeuserauth.php']['frontendEditingController']['default']