-
Notifications
You must be signed in to change notification settings - Fork 611
/
Lightswitch.php
255 lines (229 loc) · 6.31 KB
/
Lightswitch.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
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
<?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\ElementInterface;
use craft\base\Field;
use craft\base\InlineEditableFieldInterface;
use craft\base\SortableFieldInterface;
use craft\fields\conditions\LightswitchFieldConditionRule;
use craft\helpers\ArrayHelper;
use craft\helpers\Cp;
use craft\helpers\Db;
use craft\helpers\Html;
use GraphQL\Type\Definition\Type;
use yii\db\Schema;
/**
* Lightswitch represents a Lightswitch field.
*
* @author Pixel & Tonic, Inc. <support@pixelandtonic.com>
* @since 3.0.0
*/
class Lightswitch extends Field implements InlineEditableFieldInterface, SortableFieldInterface
{
/**
* @inheritdoc
*/
public static function displayName(): string
{
return Craft::t('app', 'Lightswitch');
}
/**
* @inheritdoc
*/
public static function icon(): string
{
return 'toggle-on';
}
/**
* @inheritdoc
*/
public static function isRequirable(): bool
{
return false;
}
/**
* @inheritdoc
*/
public static function phpType(): string
{
return 'bool';
}
/**
* @inheritdoc
*/
public static function dbType(): string
{
return Schema::TYPE_BOOLEAN;
}
/**
* @inheritdoc
*/
public static function queryCondition(array $instances, mixed $value, array &$params): array
{
$valueSql = static::valueSql($instances);
return Db::parseBooleanParam($valueSql, $value, $instances[0]->default, Schema::TYPE_JSON);
}
/**
* @var bool Whether the lightswitch should be enabled by default
*/
public bool $default = false;
/**
* @var string|null The label text to display beside the lightswitch’s enabled state
* @since 3.5.4
*/
public ?string $onLabel = null;
/**
* @var string|null The label text to display beside the lightswitch’s disabled state
* @since 3.5.4
*/
public ?string $offLabel = null;
/**
* @inheritdoc
*/
public function __construct($config = [])
{
// Config normalization
if (($onLabel = ArrayHelper::remove($config, 'label')) !== null) {
$config['onLabel'] = $onLabel;
}
parent::__construct($config);
}
/**
* @inheritdoc
*/
public function getSettingsHtml(): ?string
{
return
Cp::lightswitchFieldHtml([
'label' => Craft::t('app', 'Default Value'),
'id' => 'default',
'name' => 'default',
'on' => $this->default,
]) .
Cp::textFieldHtml([
'label' => Craft::t('app', 'OFF Label'),
'instructions' => Craft::t('app', 'The label text to display beside the lightswitch’s disabled state.'),
'id' => 'off-label',
'name' => 'offLabel',
'value' => $this->offLabel,
]) .
Cp::textFieldHtml([
'label' => Craft::t('app', 'ON Label'),
'instructions' => Craft::t('app', 'The label text to display beside the lightswitch’s enabled state.'),
'id' => 'on-label',
'name' => 'onLabel',
'value' => $this->onLabel,
]);
}
/**
* @inheritdoc
*/
protected function inputHtml(mixed $value, ?ElementInterface $element, bool $inline): string
{
return $this->_inputHtmlInternal($value, $element, false);
}
/**
* @inheritdoc
*/
public function getStaticHtml(mixed $value, ?ElementInterface $element = null): string
{
return $this->_inputHtmlInternal($value, $element, true);
}
/**
* Render html for both static and interactive lightswitch field
*
* @param mixed $value
* @param ElementInterface|null $element
* @param bool $static
* @return string
*/
private function _inputHtmlInternal(mixed $value, ?ElementInterface $element, bool $static): string
{
$id = $this->getInputId();
return Craft::$app->getView()->renderTemplate('_includes/forms/lightswitch.twig', [
'id' => $id,
'labelId' => $this->getLabelId(),
'describedBy' => $this->describedBy,
'name' => $this->handle,
'on' => (bool)$value,
'onLabel' => Craft::t('site', $this->onLabel),
'offLabel' => Craft::t('site', $this->offLabel),
'disabled' => $static,
]);
}
/**
* @inheritdoc
*/
public function normalizeValue(mixed $value, ?ElementInterface $element): mixed
{
// If this is a new entry, look for a default option
if ($value === null) {
$value = $this->default;
}
return (bool)$value;
}
/**
* @inheritdoc
*/
public function getElementConditionRuleType(): array|string|null
{
return LightswitchFieldConditionRule::class;
}
/**
* @inheritdoc
*/
public function getContentGqlType(): Type|array
{
return Type::boolean();
}
/**
* @inheritdoc
* @since 3.5.0
*/
public function getContentGqlMutationArgumentType(): Type|array
{
return [
'name' => $this->handle,
'type' => Type::boolean(),
'description' => $this->instructions,
];
}
/**
* @inheritdoc
* @since 3.5.0
*/
public function getContentGqlQueryArgumentType(): Type|array
{
return [
'name' => $this->handle,
'type' => Type::boolean(),
];
}
/**
* @inheritdoc
*/
public function getPreviewHtml(mixed $value, ElementInterface $element): string
{
if (!$value) {
return '';
}
$label = $this->onLabel ?: Craft::t('app', 'Enabled');
return
Html::tag('span', '', [
'class' => 'checkbox-icon',
'role' => 'img',
'title' => $label,
'aria' => [
'label' => $label,
],
]) .
Html::tag('span', $this->getUiLabel(), [
'class' => 'checkbox-preview-label',
]);
}
}