-
Notifications
You must be signed in to change notification settings - Fork 645
/
Copy pathFieldLayoutComponent.php
357 lines (316 loc) · 11.2 KB
/
FieldLayoutComponent.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
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
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
<?php
/**
* @link https://craftcms.com/
* @copyright Copyright (c) Pixel & Tonic, Inc.
* @license https://craftcms.github.io/license/
*/
namespace craft\base;
use Craft;
use craft\base\conditions\ConditionInterface;
use craft\elements\conditions\ElementConditionInterface;
use craft\elements\conditions\users\UserCondition;
use craft\elements\User;
use craft\events\DefineShowFieldLayoutComponentInFormEvent;
use craft\helpers\Cp;
use craft\models\FieldLayout;
/**
* FieldLayoutComponent is the base class for classes representing field layout components (tabs or elements) in terms of objects.
*
* @property ElementConditionInterface|null $elementCondition The element condition for this layout element
* @property UserCondition|null $userCondition The user condition for this layout element
* @property FieldLayout $layout The layout this element belongs to
* @author Pixel & Tonic, Inc. <[email protected]>
* @since 4.0.0
*/
abstract class FieldLayoutComponent extends Model
{
/**
* @event DefineShowFieldLayoutComponentInFormEvent The event that is triggered when determining whether the component should be shown in a field layout.
* @see showInForm()
* @since 5.3.0
*/
public const EVENT_DEFINE_SHOW_IN_FORM = 'defineShowInForm';
/**
* @var UserCondition
*/
private static UserCondition $defaultUserCondition;
/**
* @var ElementConditionInterface[]
*/
private static array $defaultElementConditions = [];
/**
* @return UserCondition
*/
private static function defaultUserCondition(): UserCondition
{
if (!isset(self::$defaultUserCondition)) {
self::$defaultUserCondition = User::createCondition();
}
return self::$defaultUserCondition;
}
/**
* @param class-string<ElementInterface> $elementType
* @return ElementConditionInterface
*/
private static function defaultElementCondition(string $elementType): ElementConditionInterface
{
if (!isset(self::$defaultElementConditions[$elementType])) {
self::$defaultElementConditions[$elementType] = $elementType::createCondition();
}
return self::$defaultElementConditions[$elementType];
}
/**
* @var string|null The UUID of the layout element.
*/
public ?string $uid = null;
/**
* @var string|null The element type the field layout is for
* @since 5.0.0
*/
public ?string $elementType = null;
/**
* @var FieldLayout The field layout tab this element belongs to
* @see getLayout()
* @see setLayout()
*/
private FieldLayout $_layout;
/**
* @var UserCondition|class-string<UserCondition>|array|null
* @phpstan-var UserCondition|class-string<UserCondition>|array{class:class-string<UserCondition>}|null
* @see getUserCondition()
* @see setUserCondition()
*/
private mixed $_userCondition = null;
/**
* @var ElementConditionInterface|class-string<ElementConditionInterface>|array|null
* @phpstan-var ElementConditionInterface|class-string<ElementConditionInterface>|array{class:class-string<ElementConditionInterface>}|null
* @see getElementCondition()
* @see setElementCondition()
*/
private mixed $_elementCondition = null;
/**
* Returns the layout this element belongs to.
*
* @return FieldLayout
*/
public function getLayout(): FieldLayout
{
return $this->_layout;
}
/**
* Sets the layout this element belongs to.
*
* @param FieldLayout $layout
*/
public function setLayout(FieldLayout $layout): void
{
$this->_layout = $layout;
}
/**
* Returns whether this element can be conditional.
*
* @return bool
*/
protected function conditional(): bool
{
return true;
}
/**
* Returns whether this element has any conditions.
*
* @return bool
*/
public function hasConditions(): bool
{
return $this->getUserCondition() || $this->getElementCondition();
}
/**
* Returns the user condition for this layout element.
*
* @return UserCondition|null
*/
public function getUserCondition(): ?UserCondition
{
if (isset($this->_userCondition) && !$this->_userCondition instanceof UserCondition) {
$this->_userCondition = $this->_normalizeCondition($this->_userCondition);
}
return $this->_userCondition;
}
/**
* Sets the user condition for this layout element.
*
* @param UserCondition|class-string<UserCondition>|array|null $userCondition
* @phpstan-param UserCondition|class-string<UserCondition>|array{class:class-string<UserCondition>}|null $userCondition
*/
public function setUserCondition(mixed $userCondition): void
{
$this->_userCondition = $userCondition;
}
/**
* Returns the element condition for this layout element.
*
* @return ElementConditionInterface|null
*/
public function getElementCondition(): ?ElementConditionInterface
{
if (isset($this->_elementCondition) && !$this->_elementCondition instanceof ElementConditionInterface) {
if (is_string($this->_elementCondition)) {
$this->_elementCondition = ['class' => $this->_elementCondition];
}
$this->_elementCondition = array_merge(
['fieldLayouts' => [$this->getLayout()]],
$this->_elementCondition,
);
$this->_elementCondition = $this->_normalizeCondition($this->_elementCondition);
}
return $this->_elementCondition;
}
/**
* Sets the element condition for this layout element.
*
* @param ElementConditionInterface|class-string<ElementConditionInterface>|array|null $elementCondition
* @phpstan-param ElementConditionInterface|class-string<ElementConditionInterface>|array{class:class-string<ElementConditionInterface>}|null $elementCondition
*/
public function setElementCondition(mixed $elementCondition): void
{
$this->_elementCondition = $elementCondition;
}
/**
* Normalizes a condition.
*
* @template T of ConditionInterface
* @param T|class-string<T>|array|null $condition
* @phpstan-param T|class-string<T>|array{class:class-string<T>}|null $condition
* @return T|null
*/
private function _normalizeCondition(mixed $condition): ?ConditionInterface
{
if ($condition !== null) {
if (!$condition instanceof ConditionInterface) {
$condition = Craft::$app->getConditions()->createCondition($condition);
}
if (!$condition->getConditionRules()) {
$condition = null;
}
}
return $condition;
}
/**
* @inheritdoc
*/
public function fields(): array
{
$fields = parent::fields();
unset($fields['elementType']);
$fields['userCondition'] = fn() => $this->getUserCondition()?->getConfig();
$fields['elementCondition'] = fn() => $this->getElementCondition()?->getConfig();
return $fields;
}
/**
* Returns whether the layout element has settings.
*
* @return bool
* @since 5.0.0
*/
public function hasSettings()
{
return $this->conditional();
}
/**
* Returns the settings HTML for the layout element.
*
* ::: tip
* Subclasses should override [[settingsHtml()]] instead of this method.
* :::
*
* @return string
* @since 4.0.0
*/
public function getSettingsHtml(): string
{
$html = (string)$this->settingsHtml();
if ($this->conditional()) {
if ($html !== '') {
$html .= '<hr>';
}
$userCondition = $this->getUserCondition() ?? self::defaultUserCondition();
$userCondition->mainTag = 'div';
$userCondition->id = 'user-condition';
$userCondition->name = 'userCondition';
$userCondition->forProjectConfig = true;
$html .= Cp::fieldHtml($userCondition->getBuilderHtml(), [
'label' => Craft::t('app', 'Current User Condition'),
'instructions' => Craft::t('app', 'Only show for users who match the following rules:'),
]);
// Do we know the element type?
/** @var ElementInterface|string|null $elementType */
$elementType = $this->elementType ?? $this->getLayout()->type;
if ($elementType && is_subclass_of($elementType, ElementInterface::class)) {
$elementCondition = $this->getElementCondition();
if (!$elementCondition) {
$elementCondition = clone self::defaultElementCondition($elementType);
$elementCondition->setFieldLayouts([$this->getLayout()]);
}
$elementCondition->mainTag = 'div';
$elementCondition->id = 'element-condition';
$elementCondition->name = 'elementCondition';
$elementCondition->forProjectConfig = true;
$html .= Cp::fieldHtml($elementCondition->getBuilderHtml(), [
'label' => Craft::t('app', '{type} Condition', [
'type' => $elementType::displayName(),
]),
'instructions' => Craft::t('app', 'Only show when editing {type} that match the following rules:', [
'type' => $elementType::pluralLowerDisplayName(),
]),
]);
}
}
return $html;
}
/**
* Returns the settings HTML for the layout element.
*
* @return string|null
*/
protected function settingsHtml(): ?string
{
return null;
}
/**
* Returns whether the layout element should be shown in an edit form for the given element.
*
* This will only be called if the field layout component has been saved with a [[uid|UUID]] already.
*
* @param ElementInterface|null $element
* @return bool
* @since 4.0.0
*/
public function showInForm(?ElementInterface $element = null): bool
{
// Fire a 'defineShowInForm' event
if ($this->hasEventHandlers(self::EVENT_DEFINE_SHOW_IN_FORM)) {
$event = new DefineShowFieldLayoutComponentInFormEvent([
'fieldLayout' => $this->getLayout(),
'element' => $element,
]);
$this->trigger(self::EVENT_DEFINE_SHOW_IN_FORM, $event);
if (!$event->showInForm || $event->handled) {
return $event->showInForm;
}
}
if ($this->conditional()) {
$userCondition = $this->getUserCondition();
$elementCondition = $this->getElementCondition();
if ($userCondition) {
$currentUser = Craft::$app->getUser()->getIdentity();
if ($currentUser && !$userCondition->matchElement($currentUser)) {
return false;
}
}
if ($elementCondition && $element && !$elementCondition->matchElement($element)) {
return false;
}
}
return true;
}
}