-
Notifications
You must be signed in to change notification settings - Fork 638
/
Copy pathElement.php
6524 lines (5751 loc) · 199 KB
/
Element.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
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<?php
/**
* @link https://craftcms.com/
* @copyright Copyright (c) Pixel & Tonic, Inc.
* @license https://craftcms.github.io/license/
*/
namespace craft\base;
use ArrayIterator;
use Craft;
use craft\behaviors\CustomFieldBehavior;
use craft\behaviors\DraftBehavior;
use craft\behaviors\RevisionBehavior;
use craft\db\CoalesceColumnsExpression;
use craft\db\Command;
use craft\db\Connection;
use craft\db\Query;
use craft\db\Table;
use craft\elements\actions\Delete;
use craft\elements\actions\DeleteActionInterface;
use craft\elements\actions\Duplicate;
use craft\elements\actions\Edit;
use craft\elements\actions\SetStatus;
use craft\elements\actions\View as ViewAction;
use craft\elements\conditions\ElementCondition;
use craft\elements\conditions\ElementConditionInterface;
use craft\elements\db\EagerLoadPlan;
use craft\elements\db\ElementQuery;
use craft\elements\db\ElementQueryInterface;
use craft\elements\db\NestedElementQueryInterface;
use craft\elements\ElementCollection;
use craft\elements\exporters\Expanded;
use craft\elements\exporters\Raw;
use craft\elements\User;
use craft\enums\AttributeStatus;
use craft\enums\Color;
use craft\errors\InvalidFieldException;
use craft\events\AuthorizationCheckEvent;
use craft\events\DefineAttributeHtmlEvent;
use craft\events\DefineAttributeKeywordsEvent;
use craft\events\DefineEagerLoadingMapEvent;
use craft\events\DefineHtmlEvent;
use craft\events\DefineMenuItemsEvent;
use craft\events\DefineMetadataEvent;
use craft\events\DefineUrlEvent;
use craft\events\DefineValueEvent;
use craft\events\ElementIndexTableAttributeEvent;
use craft\events\ElementStructureEvent;
use craft\events\ModelEvent;
use craft\events\RegisterElementActionsEvent;
use craft\events\RegisterElementCardAttributesEvent;
use craft\events\RegisterElementDefaultCardAttributesEvent;
use craft\events\RegisterElementDefaultTableAttributesEvent;
use craft\events\RegisterElementExportersEvent;
use craft\events\RegisterElementFieldLayoutsEvent;
use craft\events\RegisterElementHtmlAttributesEvent;
use craft\events\RegisterElementSearchableAttributesEvent;
use craft\events\RegisterElementSortOptionsEvent;
use craft\events\RegisterElementSourcesEvent;
use craft\events\RegisterElementTableAttributesEvent;
use craft\events\RegisterPreviewTargetsEvent;
use craft\events\SetEagerLoadedElementsEvent;
use craft\events\SetElementRouteEvent;
use craft\fieldlayoutelements\BaseField;
use craft\fieldlayoutelements\CustomField;
use craft\helpers\App;
use craft\helpers\ArrayHelper;
use craft\helpers\Cp;
use craft\helpers\Db;
use craft\helpers\ElementHelper;
use craft\helpers\Html;
use craft\helpers\Json;
use craft\helpers\StringHelper;
use craft\helpers\Template;
use craft\helpers\UrlHelper;
use craft\i18n\Formatter;
use craft\models\FieldLayout;
use craft\models\Site;
use craft\validators\DateTimeValidator;
use craft\validators\ElementUriValidator;
use craft\validators\SiteIdValidator;
use craft\validators\SlugValidator;
use craft\validators\StringValidator;
use craft\web\UploadedFile;
use Illuminate\Support\Collection;
use ReflectionClass;
use Throwable;
use Traversable;
use Twig\Markup;
use UnitEnum;
use yii\base\ArrayableTrait;
use yii\base\ErrorHandler;
use yii\base\Event;
use yii\base\InvalidCallException;
use yii\base\InvalidConfigException;
use yii\base\NotSupportedException;
use yii\base\UnknownPropertyException;
use yii\db\Expression;
use yii\db\ExpressionInterface;
use yii\validators\BooleanValidator;
use yii\validators\RequiredValidator;
use yii\validators\Validator;
use yii\web\Response;
/**
* Element is the base class for classes representing elements in terms of objects.
*
* @mixin CustomFieldBehavior
* @property int|null $canonicalId The element’s canonical ID
* @property-read string $canonicalUid The element’s canonical UID
* @property-read bool $isCanonical Whether this is the canonical element
* @property-read bool $isDerivative Whether this is a derivative element, such as a draft or revision
* @property ElementQueryInterface $ancestors The element’s ancestors
* @property ElementQueryInterface $children The element’s children
* @property string|null $cpEditUrl The element’s edit URL in the control panel
* @property ElementQueryInterface $descendants The element’s descendants
* @property string $editorHtml The HTML for the element’s editor HUD
* @property bool $enabledForSite Whether the element is enabled for this site
* @property string $fieldContext The field context this element’s content uses
* @property FieldLayout|null $fieldLayout The field layout used by this element
* @property array $fieldParamNamespace The namespace used by custom field params on the request
* @property array $fieldValues The element’s normalized custom field values, indexed by their handles
* @property bool $hasDescendants Whether the element has descendants
* @property array $htmlAttributes Any attributes that should be included in the element’s DOM representation in the control panel
* @property Markup|null $link An anchor pre-filled with this element’s URL and title
* @property ElementInterface|null $canonical The canonical element, if one exists for the current site
* @property ElementInterface|null $next The next element relative to this one, from a given set of criteria
* @property ElementInterface|null $nextSibling The element’s next sibling
* @property ElementInterface|null $parent The element’s parent
* @property int|null $parentId The element’s parent’s ID
* @property ElementInterface|null $prev The previous element relative to this one, from a given set of criteria
* @property ElementInterface|null $prevSibling The element’s previous sibling
* @property string|null $ref The reference string to this element
* @property mixed $route The route that should be used when the element’s URI is requested
* @property array $serializedFieldValues Array of the element’s serialized custom field values, indexed by their handles
* @property ElementQueryInterface $siblings All of the element’s siblings
* @property Site $site Site the element is associated with
* @property string|null $status The element’s status
* @property int[]|array $supportedSites The sites this element is associated with
* @property int $totalDescendants The total number of descendants that the element has
* @property string|null $uriFormat The URI format used to generate this element’s URL
* @property string|null $url The element’s full URL
* @property-write int|null $revisionCreatorId revision creator ID to be saved
* @property-write string|null $revisionNotes revision notes to be saved
* @author Pixel & Tonic, Inc. <[email protected]>
* @since 3.0.0
*/
abstract class Element extends Component implements ElementInterface
{
use ElementTrait;
use ArrayableTrait {
toArray as traitToArray;
}
/**
* @since 3.3.6
*/
public const HOMEPAGE_URI = '__home__';
// Statuses
// -------------------------------------------------------------------------
public const STATUS_ENABLED = 'enabled';
public const STATUS_DISABLED = 'disabled';
public const STATUS_ARCHIVED = 'archived';
/** @since 5.0.0 */
public const STATUS_DRAFT = 'draft';
// Validation scenarios
// -------------------------------------------------------------------------
public const SCENARIO_ESSENTIALS = 'essentials';
public const SCENARIO_LIVE = 'live';
// Events
// -------------------------------------------------------------------------
/**
* @event RegisterElementSourcesEvent The event that is triggered when registering the available sources for the element type.
*/
public const EVENT_REGISTER_SOURCES = 'registerSources';
/**
* @event RegisterElementFieldLayoutsEvent The event that is triggered when registering all of the field layouts
* associated with elements from a given source.
* @see fieldLayouts()
* @since 3.5.0
*/
public const EVENT_REGISTER_FIELD_LAYOUTS = 'registerFieldLayouts';
/**
* @event RegisterElementActionsEvent The event that is triggered when registering the available bulk actions for the element type.
*/
public const EVENT_REGISTER_ACTIONS = 'registerActions';
/**
* @event RegisterElementExportersEvent The event that is triggered when registering the available exporters for the element type.
* @since 3.4.0
*/
public const EVENT_REGISTER_EXPORTERS = 'registerExporters';
/**
* @event RegisterElementSearchableAttributesEvent The event that is triggered when registering the searchable attributes for the element type.
*/
public const EVENT_REGISTER_SEARCHABLE_ATTRIBUTES = 'registerSearchableAttributes';
/**
* @event RegisterElementSortOptionsEvent The event that is triggered when registering the sort options for the element type.
*/
public const EVENT_REGISTER_SORT_OPTIONS = 'registerSortOptions';
/**
* @event RegisterElementTableAttributesEvent The event that is triggered when registering the table attributes for the element type.
*/
public const EVENT_REGISTER_TABLE_ATTRIBUTES = 'registerTableAttributes';
/**
* @event RegisterElementTableAttributesEvent The event that is triggered when registering the table attributes for the element type.
*/
public const EVENT_REGISTER_DEFAULT_TABLE_ATTRIBUTES = 'registerDefaultTableAttributes';
/**
* @event ElementIndexTableAttributeEvent The event that is triggered when preparing an element query for an element index, for each
* attribute present in the table.
*
* Paired with [[EVENT_REGISTER_TABLE_ATTRIBUTES]] and [[EVENT_SET_TABLE_ATTRIBUTE_HTML]], this allows optimization of queries on element indexes.
*
* ```php
* use craft\base\Element;
* use craft\elements\Entry;
* use craft\events\DefineAttributeHtmlEvent;
* use craft\events\PrepareElementQueryForTableAttributeEvent;
* use craft\events\RegisterElementTableAttributesEvent;
* use craft\helpers\Cp;
* use yii\base\Event;
*
* Event::on(
* Entry::class,
* Element::EVENT_REGISTER_TABLE_ATTRIBUTES,
* function(RegisterElementTableAttributesEvent $e) {
* $e->attributes[] = 'authorExpertise';
* }
* );
*
* Event::on(
* Entry::class,
* Element::EVENT_PREP_QUERY_FOR_TABLE_ATTRIBUTE,
* function(PrepareElementQueryForTableAttributeEvent $e) {
* $query = $e->query;
* $attr = $e->attribute;
*
* if ($attr === 'authorExpertise') {
* $query->andWith(['author.areasOfExpertiseCategoryField']);
* }
* }
* );
*
* Event::on(
* Entry::class,
* Element::EVENT_SET_TABLE_ATTRIBUTE_HTML,
* function(DefineAttributeHtmlEvent $e) {
* $attribute = $e->attribute;
*
* if ($attribute !== 'authorExpertise') {
* return;
* }
*
* // The field data is eager-loaded!
* $author = $e->sender->getAuthor();
* $categories = $author->areasOfExpertiseCategoryField;
*
* $e->html = Cp::elementPreviewHtml($categories);
* }
* );
* ```
*
* @since 3.7.14
*/
public const EVENT_PREP_QUERY_FOR_TABLE_ATTRIBUTE = 'prepQueryForTableAttribute';
/**
* @event RegisterElementCardAttributesEvent The event that is triggered when registering the card attributes for the element type.
* @since 5.5.0
*/
public const EVENT_REGISTER_CARD_ATTRIBUTES = 'registerCardAttributes';
/**
* @event RegisterElementCardAttributesEvent The event that is triggered when registering the card attributes for the element type.
* @since 5.5.0
*/
public const EVENT_REGISTER_DEFAULT_CARD_ATTRIBUTES = 'registerDefaultCardAttributes';
/**
* @event DefineEagerLoadingMapEvent The event that is triggered when defining an eager-loading map.
*
* ```php
* use craft\base\Element;
* use craft\base\ElementInterface;
* use craft\db\Query;
* use craft\elements\Entry;
* use craft\events\DefineEagerLoadingMapEvent;
* use yii\base\Event;
*
* // Add support for `with(['bookClub'])` to entries
* Event::on(
* Entry::class,
* Element::EVENT_DEFINE_EAGER_LOADING_MAP,
* function(DefineEagerLoadingMapEvent $event) {
* if ($event->handle === 'bookClub') {
* $bookEntryIds = array_map(fn(ElementInterface $element) => $element->id, $event->elements);
* $event->elementType = \my\plugin\BookClub::class,
* $event->map = (new Query)
* ->select(['source' => 'bookId', 'target' => 'clubId'])
* ->from('{{%bookclub_books}}')
* ->where(['bookId' => $bookEntryIds])
* ->all();
* $event->handled = true;
* }
* }
* );
* ```
*
* @since 3.1.0
*/
public const EVENT_DEFINE_EAGER_LOADING_MAP = 'defineEagerLoadingMap';
/**
* @event SetEagerLoadedElementsEvent The event that is triggered when setting eager-loaded elements.
*
* Set [[Event::$handled]] to `true` to prevent the elements from getting stored to the private
* `$_eagerLoadedElements` array.
*
* @since 3.5.0
*/
public const EVENT_SET_EAGER_LOADED_ELEMENTS = 'setEagerLoadedElements';
/**
* @event RegisterPreviewTargetsEvent The event that is triggered when registering the element’s preview targets.
* @since 3.2.0
*/
public const EVENT_REGISTER_PREVIEW_TARGETS = 'registerPreviewTargets';
/**
* @event DefineAttributeHtmlEvent The event that is triggered when defining an attribute’s HTML for table and card views.
* @see getAttributeHtml()
* @since 5.0.0
*/
public const EVENT_DEFINE_ATTRIBUTE_HTML = 'defineAttributeHtml';
/**
* @event DefineAttributeHtmlEvent The event that is triggered when defining an attribute’s inline input HTML.
* @see getInlineAttributeInputHtml()
* @since 5.0.0
*/
public const EVENT_DEFINE_INLINE_ATTRIBUTE_INPUT_HTML = 'defineInlineAttributeInputHtml';
/**
* @event RegisterElementHtmlAttributesEvent The event that is triggered when registering the HTML attributes that should be included in the element’s DOM representation in the control panel.
*/
public const EVENT_REGISTER_HTML_ATTRIBUTES = 'registerHtmlAttributes';
/**
* @event DefineHtmlEvent The event that is triggered when defining additional buttons that should be shown at the top of the element’s edit page.
* @see getAdditionalButtons()
* @since 4.0.0
*/
public const EVENT_DEFINE_ADDITIONAL_BUTTONS = 'defineAdditionalButtons';
/**
* @event DefineMenuItemsEvent The event that is triggered when defining action menu items..
* @see getActionMenuItems()
* @since 5.0.0
*/
public const EVENT_DEFINE_ACTION_MENU_ITEMS = 'defineActionMenuItems';
/**
* @event DefineHtmlEvent The event that is triggered when defining the HTML for the editor sidebar.
* @see getSidebarHtml()
* @since 3.7.0
*/
public const EVENT_DEFINE_SIDEBAR_HTML = 'defineSidebarHtml';
/**
* @event DefineHtmlEvent The event that is triggered when defining the HTML for meta fields within the editor sidebar.
* @see metaFieldsHtml()
* @since 3.7.0
*/
public const EVENT_DEFINE_META_FIELDS_HTML = 'defineMetaFieldsHtml';
/**
* @event DefineMetadataEvent The event that is triggered when defining the element’s metadata info.
* @see getMetadata()
* @since 3.7.0
*/
public const EVENT_DEFINE_METADATA = 'defineMetadata';
/**
* @event AuthorizationCheckEvent The event that is triggered when determining whether a user is authorized to view the element’s edit page.
*
* To authorize the user, set [[AuthorizationCheckEvent::$authorized]] to `true`.
*
* ```php
* Event::on(
* Entry::class,
* Element::EVENT_AUTHORIZE_VIEW,
* function(AuthorizationCheckEvent $event) {
* $event->authorized = true;
* }
* );
* ```
*
* @see canView()
* @since 4.0.0
* @deprecated in 4.3.0. [[\craft\services\Elements::EVENT_AUTHORIZE_VIEW]] should be used instead.
*/
public const EVENT_AUTHORIZE_VIEW = 'authorizeView';
/**
* @event AuthorizationCheckEvent The event that is triggered when determining whether a user is authorized to save the element in its current state.
*
* To authorize the user, set [[AuthorizationCheckEvent::$authorized]] to `true`.
*
* ```php
* Event::on(
* Entry::class,
* Element::EVENT_AUTHORIZE_SAVE,
* function(AuthorizationCheckEvent $event) {
* $event->authorized = true;
* }
* );
* ```
*
* @see canSave()
* @since 4.0.0
* @deprecated in 4.3.0. [[\craft\services\Elements::EVENT_AUTHORIZE_SAVE]] should be used instead.
*/
public const EVENT_AUTHORIZE_SAVE = 'authorizeSave';
/**
* @event AuthorizationCheckEvent The event that is triggered when determining whether a user is authorized to create drafts for the element.
*
* To authorize the user, set [[AuthorizationCheckEvent::$authorized]] to `true`.
*
* ```php
* Event::on(
* Entry::class,
* Element::EVENT_AUTHORIZE_CREATE_DRAFTS,
* function(AuthorizationCheckEvent $event) {
* $event->authorized = true;
* }
* );
* ```
*
* @see canCreateDrafts()
* @since 4.0.0
* @deprecated in 4.3.0. [[\craft\services\Elements::EVENT_AUTHORIZE_CREATE_DRAFTS]] should be used instead.
*/
public const EVENT_AUTHORIZE_CREATE_DRAFTS = 'authorizeCreateDrafts';
/**
* @event AuthorizationCheckEvent The event that is triggered when determining whether a user is authorized to duplicate the element.
*
* To authorize the user, set [[AuthorizationCheckEvent::$authorized]] to `true`.
*
* ```php
* Event::on(
* Entry::class,
* Element::EVENT_AUTHORIZE_DUPLICATE,
* function(AuthorizationCheckEvent $event) {
* $event->authorized = true;
* }
* );
* ```
*
* @see canDuplicate()
* @since 4.0.0
* @deprecated in 4.3.0. [[\craft\services\Elements::EVENT_AUTHORIZE_DUPLICATE]] should be used instead.
*/
public const EVENT_AUTHORIZE_DUPLICATE = 'authorizeDuplicate';
/**
* @event AuthorizationCheckEvent The event that is triggered when determining whether a user is authorized to delete the element.
*
* To authorize the user, set [[AuthorizationCheckEvent::$authorized]] to `true`.
*
* ```php
* Event::on(
* Entry::class,
* Element::EVENT_AUTHORIZE_DELETE,
* function(AuthorizationCheckEvent $event) {
* $event->authorized = true;
* }
* );
* ```
*
* @see canDelete()
* @since 4.0.0
* @deprecated in 4.3.0. [[\craft\services\Elements::EVENT_AUTHORIZE_DELETE]] should be used instead.
*/
public const EVENT_AUTHORIZE_DELETE = 'authorizeDelete';
/**
* @event AuthorizationCheckEvent The event that is triggered when determining whether a user is authorized to delete the element for its current site.
*
* To authorize the user, set [[AuthorizationCheckEvent::$authorized]] to `true`.
*
* ```php
* Event::on(
* Entry::class,
* Element::EVENT_AUTHORIZE_DELETE_FOR_SITE,
* function(AuthorizationCheckEvent $event) {
* $event->authorized = true;
* }
* );
* ```
*
* @see canDeleteForSite()
* @since 4.0.0
* @deprecated in 4.3.0. [[\craft\services\Elements::EVENT_AUTHORIZE_DELETE_FOR_SITE]] should be used instead.
*/
public const EVENT_AUTHORIZE_DELETE_FOR_SITE = 'authorizeDeleteForSite';
/**
* @event SetElementRouteEvent The event that is triggered when defining the route that should be used when this element’s URL is requested.
*
* Set [[Event::$handled]] to `true` to explicitly tell the element that a route has been set (even if you’re
* setting it to `null`).
*
* ```php
* Event::on(craft\elements\Entry::class, craft\base\Element::EVENT_SET_ROUTE, function(craft\events\SetElementRouteEvent $e) {
* // @var craft\elements\Entry $entry
* $entry = $e->sender;
*
* if ($entry->uri === 'pricing') {
* $e->route = 'module/pricing/index';
*
* // Explicitly tell the element that a route has been set,
* // and prevent other event handlers from running, and tell
* $e->handled = true;
* }
* });
* ```
*/
public const EVENT_SET_ROUTE = 'setRoute';
/**
* @event DefineValueEvent The event that is triggered when defining the cache tags that should be cleared when
* this element is saved.
* @see getCacheTags()
* @since 4.1.0
*/
public const EVENT_DEFINE_CACHE_TAGS = 'defineCacheTags';
/**
* @event DefineAttributeKeywordsEvent The event that is triggered when defining the search keywords for an
* element attribute.
*
* Note that you _must_ set [[Event::$handled]] to `true` if you want the element to accept your custom
* [[DefineAttributeKeywordsEvent::$keywords|$keywords]] value.
*
* ```php
* Event::on(
* craft\elements\Entry::class,
* craft\base\Element::EVENT_DEFINE_KEYWORDS,
* function(craft\events\DefineAttributeKeywordsEvent $e
* ) {
* // @var craft\elements\Entry $entry
* $entry = $e->sender;
*
* // Prevent entry titles in the Parts section from getting search keywords
* if ($entry->section->handle === 'parts' && $e->attribute === 'title') {
* $e->keywords = '';
* $e->handled = true;
* }
* });
* ```
*
* @since 3.5.0
*/
public const EVENT_DEFINE_KEYWORDS = 'defineKeywords';
/**
* @event DefineUrlEvent The event that is triggered before defining the element’s URL.
*
* It can be used to provide a custom URL, completely bypassing the default URL generation.
*
* ```php
* use craft\base\Element;
* use craft\elements\Entry;
* use craft\events\DefineUrlEvent;
* use craft\helpers\UrlHelper;
* use yii\base\Event;
*
* Event::on(
* Entry::class,
* Element::EVENT_BEFORE_DEFINE_URL,
* function(DefineUrlEvent $e
* ) {
* // @var Entry $entry
* $entry = $e->sender;
*
* $event->url = '...';
* });
* ```
*
* To prevent the element from getting a URL, ensure `$event->url` is set to `null`,
* and set `$event->handled` to `true`.
*
* Note that [[EVENT_DEFINE_URL]] will still be called regardless of what happens with this event.
*
* @see getUrl()
* @since 4.4.6
*/
public const EVENT_BEFORE_DEFINE_URL = 'beforeDefineUrl';
/**
* @event DefineUrlEvent The event that is triggered when defining the element’s URL.
*
* ```php
* use craft\base\Element;
* use craft\elements\Entry;
* use craft\events\DefineUrlEvent;
* use craft\helpers\UrlHelper;
* use yii\base\Event;
*
* Event::on(
* Entry::class,
* Element::EVENT_DEFINE_URL,
* function(DefineUrlEvent $e
* ) {
* // @var Entry $entry
* $entry = $e->sender;
*
* // Add a custom query string param to the URL
* if ($event->value !== null) {
* $event->url = UrlHelper::urlWithParams($event->url, [
* 'foo' => 'bar',
* ]);
* }
* });
* ```
*
* To prevent the element from getting a URL, ensure `$event->url` is set to `null`,
* and set `$event->handled` to `true`.
*
* @see getUrl()
* @since 4.3.0
*/
public const EVENT_DEFINE_URL = 'defineUrl';
/**
* @event ModelEvent The event that is triggered before the element is saved.
*
* You may set [[\yii\base\ModelEvent::$isValid]] to `false` to prevent the element from getting saved.
*
* If you want to ignore events for drafts or revisions, call [[\craft\helpers\ElementHelper::isDraftOrRevision()]]
* from your event handler:
*
* ```php
* use craft\base\Element;
* use craft\elements\Entry;
* use craft\events\ModelEvent;
* use craft\helpers\ElementHelper;
* use yii\base\Event;
*
* Event::on(Entry::class, Element::EVENT_BEFORE_SAVE, function(ModelEvent $e) {
* // @var Entry $entry
* $entry = $e->sender;
*
* if (ElementHelper::isDraftOrRevision($entry)) {
* return;
* }
*
* // ...
* });
* ```
*/
public const EVENT_BEFORE_SAVE = 'beforeSave';
/**
* @event ModelEvent The event that is triggered after the element is saved.
*
* If you want to ignore events for drafts or revisions, call [[\craft\helpers\ElementHelper::isDraftOrRevision()]]
* from your event handler:
*
* ```php
* use craft\base\Element;
* use craft\elements\Entry;
* use craft\events\ModelEvent;
* use craft\helpers\ElementHelper;
* use yii\base\Event;
*
* Event::on(Entry::class, Element::EVENT_AFTER_SAVE, function(ModelEvent $e) {
* // @var Entry $entry
* $entry = $e->sender;
*
* if (ElementHelper::isDraftOrRevision($entry)) {
* return;
* }
*
* // ...
* });
* ```
*/
public const EVENT_AFTER_SAVE = 'afterSave';
/**
* @event ModelEvent The event that is triggered after the element is fully saved and propagated to other sites.
*
* If you want to ignore events for drafts or revisions, call [[\craft\helpers\ElementHelper::isDraftOrRevision()]]
* from your event handler:
*
* ```php
* use craft\base\Element;
* use craft\elements\Entry;
* use craft\events\ModelEvent;
* use craft\helpers\ElementHelper;
* use yii\base\Event;
*
* Event::on(Entry::class, Element::EVENT_AFTER_PROPAGATE, function(ModelEvent $e) {
* // @var Entry $entry
* $entry = $e->sender;
*
* if (ElementHelper::isDraftOrRevision($entry) {
* return;
* }
*
* // ...
* });
* ```
*
* @since 3.2.0
*/
public const EVENT_AFTER_PROPAGATE = 'afterPropagate';
/**
* @event ModelEvent The event that is triggered before the element is deleted.
*
* You may set [[\yii\base\ModelEvent::$isValid]] to `false` to prevent the element from getting deleted.
*/
public const EVENT_BEFORE_DELETE = 'beforeDelete';
/**
* @event \yii\base\Event The event that is triggered after the element is deleted.
*/
public const EVENT_AFTER_DELETE = 'afterDelete';
/**
* @event ModelEvent The event that is triggered before the element is restored.
*
* You may set [[\yii\base\ModelEvent::$isValid]] to `false` to prevent the element from getting restored.
* @since 3.1.0
*/
public const EVENT_BEFORE_RESTORE = 'beforeRestore';
/**
* @event \yii\base\Event The event that is triggered after the element is restored.
* @since 3.1.0
*/
public const EVENT_AFTER_RESTORE = 'afterRestore';
/**
* @event ElementStructureEvent The event that is triggered before the element is moved in a structure.
*
* You may set [[\yii\base\ModelEvent::$isValid]] to `false` to prevent the element from getting moved.
*
* @deprecated in 4.5.0. [[\craft\services\Structures::EVENT_BEFORE_INSERT_ELEMENT]] or
* [[\craft\services\Structures::EVENT_BEFORE_MOVE_ELEMENT|EVENT_BEFORE_MOVE_ELEMENT]]
* should be used instead.
*/
public const EVENT_BEFORE_MOVE_IN_STRUCTURE = 'beforeMoveInStructure';
/**
* @event ElementStructureEvent The event that is triggered after the element is moved in a structure.
* @deprecated in 4.5.0. [[\craft\services\Structures::EVENT_AFTER_INSERT_ELEMENT]] or
* [[\craft\services\Structures::EVENT_AFTER_MOVE_ELEMENT|EVENT_AFTER_MOVE_ELEMENT]]
* should be used instead.
*/
public const EVENT_AFTER_MOVE_IN_STRUCTURE = 'afterMoveInStructure';
/**
* @inheritdoc
*/
public static function displayName(): string
{
return Craft::t('app', 'Element');
}
/**
* @inheritdoc
*/
public static function lowerDisplayName(): string
{
return StringHelper::toLowerCase(static::displayName());
}
/**
* @inheritdoc
*/
public static function pluralDisplayName(): string
{
return Craft::t('app', 'Elements');
}
/**
* @inheritdoc
*/
public static function pluralLowerDisplayName(): string
{
return StringHelper::toLowerCase(static::pluralDisplayName());
}
/**
* @inheritdoc
*/
public static function refHandle(): ?string
{
return null;
}
/**
* @inheritdoc
*/
public static function hasDrafts(): bool
{
return false;
}
/**
* @inheritdoc
*/
public static function trackChanges(): bool
{
return false;
}
/**
* @inheritdoc
*/
public static function hasTitles(): bool
{
return false;
}
/**
* @inheritdoc
*/
public static function hasThumbs(): bool
{
return false;
}
/**
* @inheritdoc
*/
public static function hasUris(): bool
{
return false;
}
/**
* @inheritdoc
*/
public static function isLocalized(): bool
{
return false;
}
/**
* @inheritdoc
*/
public static function hasStatuses(): bool
{
return false;
}
/**
* @inheritdoc
*/
public static function statuses(): array
{
return [
self::STATUS_ENABLED => Craft::t('app', 'Enabled'),
self::STATUS_DISABLED => Craft::t('app', 'Disabled'),
];
}
/**
* @inheritdoc
* @return ElementQueryInterface
*/
public static function find(): ElementQueryInterface
{
return new ElementQuery(static::class);
}
/**
* @inheritdoc
*/
public static function findOne(mixed $criteria = null): ?static
{
return static::findByCondition($criteria, true);
}
/**
* @inheritdoc
*/
public static function findAll(mixed $criteria = null): array
{
return static::findByCondition($criteria, false);
}
/**
* @interitdoc
*/
public static function get(int|string $id): ?static
{
return static::find()
->id($id)
->fixedOrder()
->drafts(null)
->provisionalDrafts(null)
->revisions(null)
->status(null)
->one();
}
/**
* @inheritdoc
*/
public static function createCondition(): ElementConditionInterface
{
return Craft::createObject(ElementCondition::class, [static::class]);
}
/**
* @inheritdoc
*/
public static function sources(string $context): array
{
$sources = static::defineSources($context);
// Fire a 'registerSources' event
if (Event::hasHandlers(static::class, self::EVENT_REGISTER_SOURCES)) {
$event = new RegisterElementSourcesEvent([
'context' => $context,
'sources' => $sources,
]);
Event::trigger(static::class, self::EVENT_REGISTER_SOURCES, $event);
return $event->sources;
}
return $sources;
}
/**
* Defines the sources that elements of this type may belong to.
*
* @param string $context The context ('index', 'modal', 'field', or 'settings').
* @return array The sources.
* @see sources()
*/
protected static function defineSources(string $context): array
{
return [];
}
/**
* @inheritdoc
*/
public static function findSource(string $sourceKey, ?string $context): ?array
{
return null;
}
/**
* @inheritdoc
*/
public static function sourcePath(string $sourceKey, string $stepKey, ?string $context): ?array
{
return null;
}
/**
* @inheritdoc
*/
public static function modifyCustomSource(array $config): array
{
return $config;
}
/**
* @inheritdoc
*/
public static function fieldLayouts(?string $source): array
{
$fieldLayouts = static::defineFieldLayouts($source);