-
Notifications
You must be signed in to change notification settings - Fork 690
Expand file tree
/
Copy pathPlainText.php
More file actions
280 lines (243 loc) · 7.17 KB
/
PlainText.php
File metadata and controls
280 lines (243 loc) · 7.17 KB
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
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
<?php
/**
* @link https://craftcms.com/
* @copyright Copyright (c) Pixel & Tonic, Inc.
* @license https://craftcms.github.io/license/
*/
namespace craft\fields;
use Craft;
use craft\base\CrossSiteCopyableFieldInterface;
use craft\base\ElementInterface;
use craft\base\Field;
use craft\base\InlineEditableFieldInterface;
use craft\base\MergeableFieldInterface;
use craft\base\SortableFieldInterface;
use craft\elements\Entry;
use craft\fields\conditions\TextFieldConditionRule;
use craft\helpers\Html;
use craft\helpers\StringHelper;
/**
* PlainText represents a Plain Text field.
*
* @author Pixel & Tonic, Inc. <[email protected]>
* @since 3.0.0
*/
class PlainText extends Field implements InlineEditableFieldInterface, SortableFieldInterface, MergeableFieldInterface, CrossSiteCopyableFieldInterface
{
/**
* @inheritdoc
*/
public static function displayName(): string
{
return Craft::t('app', 'Plain Text');
}
/**
* @inheritdoc
*/
public static function phpType(): string
{
return 'string|null';
}
/**
* @var string The UI mode of the field.
* @phpstan-var 'normal'|'enlarged'
* @since 3.5.0
*/
public string $uiMode = 'normal';
/**
* @var string|null The input’s placeholder text
*/
public ?string $placeholder = null;
/**
* @var bool Whether the input should use monospace font
*/
public bool $code = false;
/**
* @var bool Whether the input should allow line breaks
*/
public bool $multiline = false;
/**
* @var int The minimum number of rows the input should have, if multi-line
*/
public int $initialRows = 4;
/**
* @var int|null The maximum number of characters allowed in the field
*/
public ?int $charLimit = null;
/**
* @var int|null The maximum number of bytes allowed in the field
* @since 3.4.0
*/
public ?int $byteLimit = null;
/**
* @inheritdoc
*/
public function __construct(array $config = [])
{
// Config normalization
if (isset($config['limitUnit'], $config['fieldLimit'])) {
if ($config['limitUnit'] === 'chars') {
$config['charLimit'] = (int)$config['fieldLimit'] ?: null;
} else {
$config['byteLimit'] = (int)$config['fieldLimit'] ?: null;
}
unset($config['limitUnit'], $config['fieldLimit']);
}
// remove unused settings
unset($config['maxLengthUnit'], $config['columnType']);
parent::__construct($config);
}
/**
* @inheritdoc
*/
public function init(): void
{
parent::init();
if (isset($this->placeholder)) {
$this->placeholder = StringHelper::shortcodesToEmoji($this->placeholder);
}
}
/**
* @inheritdoc
*/
public function getSettings(): array
{
$settings = parent::getSettings();
if (isset($settings['placeholder']) && !Craft::$app->getDb()->getSupportsMb4()) {
$settings['placeholder'] = StringHelper::emojiToShortcodes($settings['placeholder']);
}
return $settings;
}
/**
* @inheritdoc
*/
protected function defineRules(): array
{
$rules = parent::defineRules();
$rules[] = [['initialRows', 'charLimit', 'byteLimit'], 'integer', 'min' => 1];
return $rules;
}
/**
* @inheritdoc
*/
public function getSettingsHtml(): ?string
{
return $this->settingsHtml(false);
}
/**
* @inheritdoc
*/
public function getReadOnlySettingsHtml(): ?string
{
return $this->settingsHtml(true);
}
private function settingsHtml(bool $readOnly): string
{
return Craft::$app->getView()->renderTemplate('_components/fieldtypes/PlainText/settings.twig', [
'field' => $this,
'readOnly' => $readOnly,
]);
}
/**
* @inheritdoc
*/
public function normalizeValue(mixed $value, ?ElementInterface $element): mixed
{
return $this->_normalizeValueInternal($value, $element, false);
}
/**
* @inheritdoc
*/
public function normalizeValueFromRequest(mixed $value, ?ElementInterface $element): mixed
{
return $this->_normalizeValueInternal($value, $element, true);
}
private function _normalizeValueInternal(mixed $value, ?ElementInterface $element, bool $fromRequest): mixed
{
if ($value !== null) {
if (!$fromRequest) {
$value = StringHelper::unescapeShortcodes(StringHelper::shortcodesToEmoji($value));
}
$value = trim(StringHelper::convertLineBreaks($value));
}
return $value !== '' ? $value : null;
}
/**
* @inheritdoc
*/
protected function inputHtml(mixed $value, ?ElementInterface $element, bool $inline): string
{
return $this->_inputHtml($value, $element, false);
}
public function getStaticHtml(mixed $value, ElementInterface $element): string
{
return $this->_inputHtml($value, $element, true);
}
private function _inputHtml(mixed $value, ?ElementInterface $element, bool $static): string
{
return Craft::$app->getView()->renderTemplate('_components/fieldtypes/PlainText/input.twig', [
'name' => $this->handle,
'value' => $value,
'field' => $this,
'placeholder' => $this->placeholder !== null ? Craft::t('site', StringHelper::unescapeShortcodes($this->placeholder)) : null,
'orientation' => $this->getOrientation($element),
'disabled' => $static,
]);
}
/**
* @inheritdoc
*/
public function getElementValidationRules(): array
{
return [
[
'string',
'max' => $this->byteLimit ?? $this->charLimit ?? null,
'encoding' => $this->byteLimit ? '8bit' : 'UTF-8',
],
];
}
/**
* @inheritdoc
*/
public function serializeValue(mixed $value, ?ElementInterface $element): mixed
{
if ($value !== null) {
$value = StringHelper::escapeShortcodes($value);
if (!Craft::$app->getDb()->getSupportsMb4()) {
$value = StringHelper::emojiToShortcodes($value);
}
}
return $value;
}
/**
* @inheritdoc
*/
public function getElementConditionRuleType(): ?string
{
return TextFieldConditionRule::class;
}
/**
* @inheritdoc
*/
public function getPreviewHtml(mixed $value, ?ElementInterface $element = null): string
{
$previewHtml = parent::getPreviewHtml($value, $element);
if (!$this->code) {
return $previewHtml;
}
return Html::tag('div', $previewHtml, [
'class' => 'code',
]);
}
/**
* @inheritdoc
*/
public function previewPlaceholderHtml(mixed $value, ?ElementInterface $element): string
{
if (!$value) {
$value = 'Lorem ipsum dolor sit amet, consectetur adipiscing elit.';
}
return $this->getPreviewHtml($value, $element ?? new Entry());
}
}