/
InputSlugElement.php
160 lines (145 loc) · 6.26 KB
/
InputSlugElement.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
<?php
declare(strict_types = 1);
namespace TYPO3\CMS\Backend\Form\Element;
/*
* This file is part of the TYPO3 CMS project.
*
* It is free software; you can redistribute it and/or modify it under
* the terms of the GNU General Public License, either version 2
* of the License, or any later version.
*
* For the full copyright and license information, please read the
* LICENSE.txt file that was distributed with this source code.
*
* The TYPO3 project - inspiring people to share!
*/
use TYPO3\CMS\Core\Localization\LanguageService;
use TYPO3\CMS\Core\Site\Entity\SiteInterface;
use TYPO3\CMS\Core\Utility\GeneralUtility;
use TYPO3\CMS\Core\Utility\MathUtility;
use TYPO3\CMS\Core\Utility\StringUtility;
/**
* General type=input element with some additional value.
*/
class InputSlugElement extends AbstractFormElement
{
/**
* Default field wizards enabled for this element.
*
* @var array
*/
protected $defaultFieldWizard = [
'localizationStateSelector' => [
'renderType' => 'localizationStateSelector',
],
'otherLanguageContent' => [
'renderType' => 'otherLanguageContent',
'after' => [
'localizationStateSelector'
],
],
'defaultLanguageDifferences' => [
'renderType' => 'defaultLanguageDifferences',
'after' => [
'otherLanguageContent',
],
],
];
/**
* This will render a single-line input form field, possibly with various control/validation features
*
* @return array As defined in initializeResultArray() of AbstractNode
*/
public function render()
{
$table = $this->data['tableName'];
$row = $this->data['databaseRow'];
$parameterArray = $this->data['parameterArray'];
$resultArray = $this->initializeResultArray();
$languageField = $GLOBALS['TCA'][$table]['ctrl']['languageField'];
$languageId = (int)($row[$languageField][0] ?? 0);
$baseUrl = $this->getPrefix($this->data['site'], $languageId);
$itemValue = $parameterArray['itemFormElValue'];
$config = $parameterArray['fieldConf']['config'];
$evalList = GeneralUtility::trimExplode(',', $config['eval'], true);
$size = MathUtility::forceIntegerInRange($config['size'] ?? $this->defaultInputWidth, $this->minimumInputWidth, $this->maxInputWidth);
$width = (int)$this->formMaxWidth($size);
// Convert UTF-8 characters back (that is important, see Slug class when sanitizing)
$itemValue = rawurldecode($itemValue);
$idAttribute = StringUtility::getUniqueId('formengine-input-');
$attributes = [
'value' => '',
'id' => $idAttribute,
'class' => 'form-control',
'disabled' => 'disabled',
'placeholder' => '/',
'data-formengine-validation-rules' => $this->getValidationDataAsJsonString($config),
'data-formengine-input-params' => json_encode([
'field' => $parameterArray['itemFormElName'],
'evalList' => implode(',', $evalList),
'is_in' => trim($config['is_in'] ?? '')
]),
'data-formengine-input-name' => $parameterArray['itemFormElName'],
];
$fieldInformationResult = $this->renderFieldInformation();
$fieldInformationHtml = $fieldInformationResult['html'];
$resultArray = $this->mergeChildReturnIntoExistingResult($resultArray, $fieldInformationResult, false);
$fieldControlResult = $this->renderFieldControl();
$fieldControlHtml = $fieldControlResult['html'];
$resultArray = $this->mergeChildReturnIntoExistingResult($resultArray, $fieldControlResult, false);
$fieldWizardResult = $this->renderFieldWizard();
$fieldWizardHtml = $fieldWizardResult['html'];
$resultArray = $this->mergeChildReturnIntoExistingResult($resultArray, $fieldWizardResult, false);
$mainFieldHtml = [];
$mainFieldHtml[] = '<div class="formengine-field-item t3js-formengine-field-item">';
$mainFieldHtml[] = $fieldInformationHtml;
$mainFieldHtml[] = '<div class="form-control-wrap" style="max-width: ' . $width . 'px">';
$mainFieldHtml[] = '<div class="form-wizards-wrap">';
$mainFieldHtml[] = '<div class="form-wizards-element">';
$mainFieldHtml[] = '<div class="input-group">' . ($baseUrl ? '<span class="input-group-addon">' . htmlspecialchars($baseUrl) . '</span>' : '') . '<input type="text"' . GeneralUtility::implodeAttributes($attributes, true) . ' /></div>';
$mainFieldHtml[] = '<input type="hidden" name="' . $parameterArray['itemFormElName'] . '" value="' . htmlspecialchars($itemValue) . '" />';
$mainFieldHtml[] = '</div>';
if (!empty($fieldControlHtml)) {
$mainFieldHtml[] = '<div class="form-wizards-items-aside">';
$mainFieldHtml[] = '<div class="btn-group">';
$mainFieldHtml[] = $fieldControlHtml;
$mainFieldHtml[] = '</div>';
$mainFieldHtml[] = '</div>';
}
if (!empty($fieldWizardHtml)) {
$mainFieldHtml[] = '<div class="form-wizards-items-bottom">';
$mainFieldHtml[] = $fieldWizardHtml;
$mainFieldHtml[] = '</div>';
}
$mainFieldHtml[] = '</div>';
$mainFieldHtml[] = '</div>';
$mainFieldHtml[] = '</div>';
$resultArray['html'] = implode(LF, $mainFieldHtml);
return $resultArray;
}
/**
* Render the prefix for the input field.
*
* @param SiteInterface $site
* @param int $requestLanguageId
* @return string
*/
protected function getPrefix(SiteInterface $site, int $requestLanguageId = 0): string
{
$language = $site->getLanguageById($requestLanguageId);
$base = $language->getBase();
$baseUrl = (string)$base;
$baseUrl = rtrim($baseUrl, '/');
if (!empty($baseUrl) && empty($base->getScheme()) && $base->getHost() !== '') {
$baseUrl = 'http:' . $baseUrl;
}
return $baseUrl;
}
/**
* @return LanguageService
*/
protected function getLanguageService(): LanguageService
{
return $GLOBALS['LANG'];
}
}