/
TextSize.php
176 lines (141 loc) · 5.05 KB
/
TextSize.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
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
<?php
namespace verbb\buttonbox\fields;
use verbb\buttonbox\assetbundles\ButtonBoxAsset;
use Craft;
use craft\base\ElementInterface;
use craft\fields\BaseOptionsField;
use craft\fields\data\OptionData;
use craft\fields\data\SingleOptionFieldData;
use craft\helpers\Cp;
use yii\db\Schema;
class TextSize extends BaseOptionsField
{
// Static Methods
// =========================================================================
public static function displayName(): string
{
return Craft::t('buttonbox', 'Button Box - Text Size');
}
// Properties
// =========================================================================
public array $options = [];
// Public Methods
// =========================================================================
public function getContentColumnType(): string
{
return Schema::TYPE_TEXT;
}
public function normalizeValue(mixed $value, ElementInterface $element = null): SingleOptionFieldData
{
if (!$value) {
$value = $this->defaultValue();
}
if ($value instanceof SingleOptionFieldData) {
$value = $value->value;
}
$selectedValues = (array)$value;
$value = reset($selectedValues) ?: null;
$label = $this->optionsSettingLabel();
$value = new SingleOptionFieldData($label, $value, true);
$options = [];
if ($this->options) {
foreach ($this->options as $option) {
$selected = in_array($option['value'], $selectedValues, true);
$options[] = new OptionData($option['label'], $option['value'], $selected);
}
}
$value->setOptions($options);
return $value;
}
public function getSettingsHtml(): ?string
{
$options = $this->translatedOptions();
if (!$options) {
$options = [
[
'label' => '',
'value' => '',
'pxVal' => '',
],
];
}
return Cp::editableTableFieldHtml([
'label' => $this->optionsSettingLabel(),
'instructions' => Craft::t('buttonbox', 'Pixel Size is optional and should be a single number.'),
'id' => 'options',
'name' => 'options',
'addRowLabel' => Craft::t('buttonbox', 'Add an option'),
'cols' => [
'label' => [
'heading' => Craft::t('buttonbox', 'Option Label'),
'type' => 'singleline',
'autopopulate' => 'value',
],
'value' => [
'heading' => Craft::t('buttonbox', 'Value'),
'type' => 'singleline',
'class' => 'code',
],
'pxVal' => [
'heading' => Craft::t('buttonbox', 'Pixel Size'),
'type' => 'number',
],
'default' => [
'heading' => Craft::t('buttonbox', 'Default?'),
'type' => 'checkbox',
'class' => 'thin',
],
],
'rows' => $options,
'static' => false,
'allowAdd' => true,
'allowDelete' => true,
'allowReorder' => true,
]);
}
public function getInputHtml(mixed $value, ElementInterface $element = null): string
{
$name = $this->handle;
$options = $this->translatedOptions();
// If this is a new entry, look for a default option
if ($this->isFresh($element)) {
$value = $this->defaultValue();
}
Craft::$app->getView()->registerAssetBundle(ButtonBoxAsset::class);
Craft::$app->getView()->registerJs('new Craft.ButtonBoxFancyOptions("' . Craft::$app->getView()->namespaceInputId($name) . '");');
return Craft::$app->getView()->renderTemplate('buttonbox/_field/textsize/input', [
'name' => $name,
'value' => $value,
'options' => $options,
]);
}
// Protected Methods
// =========================================================================
protected function optionsSettingLabel(): string
{
return Craft::t('buttonbox', 'Text Size Options');
}
protected function defaultValue(): array|string|null
{
$options = $this->translatedOptions();
foreach ($options as $option) {
if (!empty($option['default'])) {
return $option['value'];
}
}
return $options[0]['value'];
}
protected function translatedOptions(bool $encode = false): array
{
$translatedOptions = [];
foreach ($this->options as $option) {
$translatedOptions[] = [
'label' => Craft::t('site', $option['label']),
'value' => $option['value'],
'pxVal' => $option['pxVal'],
'default' => $option['default'],
];
}
return $translatedOptions;
}
}