From 6d1b9c180960b45f2d29dd70b26b973b21c95661 Mon Sep 17 00:00:00 2001 From: Rias Date: Tue, 5 May 2026 16:10:31 +0200 Subject: [PATCH 1/6] Alias User element --- src/User/Concerns/LegacyConstants.php | 13 ++ src/User/Elements/User.php | 2 + yii2-adapter/composer.json | 1 + .../User/Concerns/LegacyConstants.php | 122 ++++++++++++++++++ yii2-adapter/legacy/elements/User.php | 104 +-------------- yii2-adapter/src/Mixins/UserMixin.php | 10 ++ 6 files changed, 153 insertions(+), 99 deletions(-) create mode 100644 src/User/Concerns/LegacyConstants.php create mode 100644 yii2-adapter/constants/User/Concerns/LegacyConstants.php diff --git a/src/User/Concerns/LegacyConstants.php b/src/User/Concerns/LegacyConstants.php new file mode 100644 index 00000000000..fc0c8d93e36 --- /dev/null +++ b/src/User/Concerns/LegacyConstants.php @@ -0,0 +1,13 @@ +fullName; + } + + public static function registerEvents(): void + { + Event::listen(function(UserNameResolving $event) { + if (YiiEvent::hasHandlers(self::class, self::EVENT_DEFINE_NAME)) { + $yiiEvent = new DefineValueEvent(); + $yiiEvent->sender = $event->user; + + YiiEvent::trigger(self::class, self::EVENT_DEFINE_NAME, $yiiEvent); + + if ($yiiEvent->value !== null) { + $event->name = $yiiEvent->value; + } + } + }); + + Event::listen(function(UserFriendlyNameResolving $event) { + if (YiiEvent::hasHandlers(self::class, self::EVENT_DEFINE_FRIENDLY_NAME)) { + $yiiEvent = new DefineValueEvent(); + $yiiEvent->sender = $event->user; + + YiiEvent::trigger(self::class, self::EVENT_DEFINE_FRIENDLY_NAME, $yiiEvent); + + if ($yiiEvent->value !== null) { + $event->name = $yiiEvent->value; + } + } + }); + + Event::listen(UserAuthenticating::class, function(UserAuthenticating $event) { + if (YiiEvent::hasHandlers(self::class, self::EVENT_BEFORE_AUTHENTICATE)) { + $yiiEvent = new AuthenticateUserEvent(['password' => $event->credentials['password']]); + + YiiEvent::trigger(self::class, self::EVENT_BEFORE_AUTHENTICATE, $yiiEvent); + + $event->performAuthentication = $yiiEvent->performAuthentication; + } + }); + } +} diff --git a/yii2-adapter/legacy/elements/User.php b/yii2-adapter/legacy/elements/User.php index e1845c423e9..f97359e74a5 100644 --- a/yii2-adapter/legacy/elements/User.php +++ b/yii2-adapter/legacy/elements/User.php @@ -9,109 +9,15 @@ namespace craft\elements; -use craft\base\ElementEventConstants; -use craft\base\Event as YiiEvent; -use craft\base\LegacyEventConstants; -use craft\events\AuthenticateUserEvent; -use craft\events\DefineValueEvent; -use CraftCms\Cms\Auth\Events\UserAuthenticating; -use CraftCms\Cms\Element\Validation\ElementRules; -use CraftCms\Cms\Twig\Attributes\AllowedInSandbox; use CraftCms\Cms\User\Elements\User as UserElement; -use CraftCms\Cms\User\Events\UserFriendlyNameResolving; -use CraftCms\Cms\User\Events\UserNameResolving; -use CraftCms\Cms\User\Validation\UserRules; -use Deprecated; -use Illuminate\Support\Facades\Event; - -/** - * @deprecated 6.0.0 use {@see UserElement} instead. - */ -class User extends UserElement -{ - use LegacyEventConstants; - use ElementEventConstants; - - public const string SCENARIO_DEFAULT = ElementRules::SCENARIO_DEFAULT; - - public const string SCENARIO_ESSENTIALS = ElementRules::SCENARIO_ESSENTIALS; - - public const string SCENARIO_LIVE = ElementRules::SCENARIO_LIVE; - - public const string SCENARIO_ACTIVATION = UserRules::SCENARIO_ACTIVATION; - - public const string SCENARIO_REGISTRATION = UserRules::SCENARIO_REGISTRATION; - - public const string SCENARIO_PASSWORD = UserRules::SCENARIO_PASSWORD; - - /** - * @event DefineValueEvent The event that is triggered when defining the user’s name, as returned by [[getName()]] or [[__toString()]]. - * - * @since 3.7.0 - */ - public const string EVENT_DEFINE_NAME = 'defineName'; - - /** - * @event DefineValueEvent The event that is triggered when defining the user’s friendly name, as returned by [[getFriendlyName()]]. - * - * @since 3.7.0 - */ - public const string EVENT_DEFINE_FRIENDLY_NAME = 'defineFriendlyName'; +if (false) { /** - * @event AuthenticateUserEvent The event that is triggered before a user is authenticated. - * - * If you wish to offload authentication logic, then set [[AuthenticateUserEvent::$performAuthentication]] to `false`, and set [[$authError]] to - * something if there is an authentication error. + * @deprecated 6.0.0 use {@see UserElement} instead. */ - public const string EVENT_BEFORE_AUTHENTICATE = 'beforeAuthenticate'; - - /** - * Returns the user’s full name. - */ - #[Deprecated(message: 'in 4.0.0. [[fullName]] should be used instead.')] - #[AllowedInSandbox] - public function getFullName(): ?string + class User extends UserElement { - return $this->fullName; - } - - public static function registerEvents(): void - { - Event::listen(function(UserNameResolving $event) { - if (YiiEvent::hasHandlers(self::class, self::EVENT_DEFINE_NAME)) { - $yiiEvent = new DefineValueEvent(); - $yiiEvent->sender = $event->user; - - YiiEvent::trigger(self::class, self::EVENT_DEFINE_NAME, $yiiEvent); - - if ($yiiEvent->value !== null) { - $event->name = $yiiEvent->value; - } - } - }); - - Event::listen(function(UserFriendlyNameResolving $event) { - if (YiiEvent::hasHandlers(self::class, self::EVENT_DEFINE_FRIENDLY_NAME)) { - $yiiEvent = new DefineValueEvent(); - $yiiEvent->sender = $event->user; - - YiiEvent::trigger(self::class, self::EVENT_DEFINE_FRIENDLY_NAME, $yiiEvent); - - if ($yiiEvent->value !== null) { - $event->name = $yiiEvent->value; - } - } - }); - - Event::listen(UserAuthenticating::class, function(UserAuthenticating $event) { - if (YiiEvent::hasHandlers(self::class, self::EVENT_BEFORE_AUTHENTICATE)) { - $yiiEvent = new AuthenticateUserEvent(['password' => $event->credentials['password']]); - - YiiEvent::trigger(self::class, self::EVENT_BEFORE_AUTHENTICATE, $yiiEvent); - - $event->performAuthentication = $yiiEvent->performAuthentication; - } - }); } } + +class_alias(UserElement::class, User::class); diff --git a/yii2-adapter/src/Mixins/UserMixin.php b/yii2-adapter/src/Mixins/UserMixin.php index 8cf006e3a64..f0580565420 100644 --- a/yii2-adapter/src/Mixins/UserMixin.php +++ b/yii2-adapter/src/Mixins/UserMixin.php @@ -56,4 +56,14 @@ public function handleInvalidLoginParam(): Closure app(AuthMethods::class)->handleInvalidLogin($this); }; } + + public function getFullName(): Closure + { + return function(): ?string { + Deprecator::log('User-getFullName', 'Calling ->getFullName on a User is deprecated. Use $user->fullName instead.'); + + /** @phpstan-ignore-next-line */ + return $this->fullName; + }; + } } From 24378ee03fe75867f69a32fb9bd8ab54bd83f937 Mon Sep 17 00:00:00 2001 From: Rias Date: Tue, 5 May 2026 16:14:18 +0200 Subject: [PATCH 2/6] Alias Address element --- src/Address/Concerns/LegacyConstants.php | 15 ++++++++ src/Address/Elements/Address.php | 2 ++ src/User/Concerns/LegacyConstants.php | 4 ++- .../Address/Concerns/LegacyConstants.php | 32 +++++++++++++++++ yii2-adapter/legacy/elements/Address.php | 35 ++++++------------- 5 files changed, 63 insertions(+), 25 deletions(-) create mode 100644 src/Address/Concerns/LegacyConstants.php create mode 100644 yii2-adapter/constants/Address/Concerns/LegacyConstants.php diff --git a/src/Address/Concerns/LegacyConstants.php b/src/Address/Concerns/LegacyConstants.php new file mode 100644 index 00000000000..ddfabf2d591 --- /dev/null +++ b/src/Address/Concerns/LegacyConstants.php @@ -0,0 +1,15 @@ +getFieldLabel($attribute, $countryCode); + } +} diff --git a/yii2-adapter/legacy/elements/Address.php b/yii2-adapter/legacy/elements/Address.php index f7d6da1c148..27ac28f4e9a 100644 --- a/yii2-adapter/legacy/elements/Address.php +++ b/yii2-adapter/legacy/elements/Address.php @@ -2,34 +2,21 @@ namespace craft\elements; -use CommerceGuys\Addressing\AddressFormat\AddressField; -use craft\base\ElementEventConstants; -use CraftCms\Cms\Address\Addresses; use Deprecated; -/** - * Address element class - * - * @author Pixel & Tonic, Inc. - * - * @since 4.0.0 - * @deprecated 6.0.0 use {@see \CraftCms\Cms\Address\Elements\Address} instead. - */ -class Address extends \CraftCms\Cms\Address\Elements\Address -{ - use ElementEventConstants; - +/** @phpstan-ignore-next-line */ +if (false) { /** - * Returns an address attribute label. + * Address element class + * + * @author Pixel & Tonic, Inc. + * + * @since 4.0.0 + * @deprecated 6.0.0 use {@see \CraftCms\Cms\Address\Elements\Address} instead. */ - #[Deprecated(message: 'in 4.3.0. [[\craft\services\Addresses::getFieldLabel()]] should be used instead.')] - public static function addressAttributeLabel(string $attribute, string $countryCode): ?string + class Address extends \CraftCms\Cms\Address\Elements\Address { - if (!AddressField::exists($attribute)) { - return null; - } - - /** @phpstan-var AddressField::* $attribute */ - return app(Addresses::class)->getFieldLabel($attribute, $countryCode); } } + +class_alias(\CraftCms\Cms\Address\Elements\Address::class, Address::class); From 7183de2c415a88057e42f393e0cc42465747f7be Mon Sep 17 00:00:00 2001 From: Rias Date: Tue, 5 May 2026 16:26:16 +0200 Subject: [PATCH 3/6] Alias other elements --- src/Asset/Concerns/LegacyConstants.php | 15 ++ src/Asset/Elements/Asset.php | 3 + .../Concerns/LegacyNestedElementManager.php | 15 ++ src/Element/NestedElementManager.php | 3 + src/Entry/Concerns/LegacyConstants.php | 15 ++ src/Entry/Elements/Entry.php | 2 + .../Elements/Concerns/LegacyConstants.php | 15 ++ src/Field/Elements/ContentBlock.php | 2 + .../Asset/Concerns/LegacyConstants.php | 196 ++++++++++++++++++ .../Concerns/LegacyNestedElementManager.php | 68 ++++++ .../Entry/Concerns/LegacyConstants.php | 93 +++++++++ .../Elements/Concerns/LegacyConstants.php | 16 ++ yii2-adapter/legacy/elements/Asset.php | 191 +---------------- yii2-adapter/legacy/elements/ContentBlock.php | 20 +- yii2-adapter/legacy/elements/Entry.php | 88 +------- .../legacy/elements/NestedElementManager.php | 66 +----- yii2-adapter/legacy/elements/User.php | 1 + 17 files changed, 477 insertions(+), 332 deletions(-) create mode 100644 src/Asset/Concerns/LegacyConstants.php create mode 100644 src/Element/Concerns/LegacyNestedElementManager.php create mode 100644 src/Entry/Concerns/LegacyConstants.php create mode 100644 src/Field/Elements/Concerns/LegacyConstants.php create mode 100644 yii2-adapter/constants/Asset/Concerns/LegacyConstants.php create mode 100644 yii2-adapter/constants/Element/Concerns/LegacyNestedElementManager.php create mode 100644 yii2-adapter/constants/Entry/Concerns/LegacyConstants.php create mode 100644 yii2-adapter/constants/Field/Elements/Concerns/LegacyConstants.php diff --git a/src/Asset/Concerns/LegacyConstants.php b/src/Asset/Concerns/LegacyConstants.php new file mode 100644 index 00000000000..6b61acdcf5a --- /dev/null +++ b/src/Asset/Concerns/LegacyConstants.php @@ -0,0 +1,15 @@ +value; + + /** @deprecated 6.0.0 use {@see \CraftCms\Cms\Asset\Enums\FileKind} instead. */ + public const string KIND_AUDIO = FileKind::Audio->value; + + /** @deprecated 6.0.0 use {@see \CraftCms\Cms\Asset\Enums\FileKind} instead. */ + public const string KIND_CAPTIONS_SUBTITLES = FileKind::CaptionsSubtitles->value; + + /** @deprecated 6.0.0 use {@see \CraftCms\Cms\Asset\Enums\FileKind} instead. */ + public const string KIND_COMPRESSED = FileKind::Compressed->value; + + /** @deprecated 6.0.0 use {@see \CraftCms\Cms\Asset\Enums\FileKind} instead. */ + public const string KIND_EXCEL = FileKind::Excel->value; + + /** @deprecated 6.0.0 use {@see \CraftCms\Cms\Asset\Enums\FileKind} instead. */ + public const string KIND_FLASH = FileKind::Flash->value; + + /** @deprecated 6.0.0 use {@see \CraftCms\Cms\Asset\Enums\FileKind} instead. */ + public const string KIND_HTML = FileKind::Html->value; + + /** @deprecated 6.0.0 use {@see \CraftCms\Cms\Asset\Enums\FileKind} instead. */ + public const string KIND_ILLUSTRATOR = FileKind::Illustrator->value; + + /** @deprecated 6.0.0 use {@see \CraftCms\Cms\Asset\Enums\FileKind} instead. */ + public const string KIND_IMAGE = FileKind::Image->value; + + /** @deprecated 6.0.0 use {@see \CraftCms\Cms\Asset\Enums\FileKind} instead. */ + public const string KIND_JAVASCRIPT = FileKind::Javascript->value; + + /** @deprecated 6.0.0 use {@see \CraftCms\Cms\Asset\Enums\FileKind} instead. */ + public const string KIND_JSON = FileKind::Json->value; + + /** @deprecated 6.0.0 use {@see \CraftCms\Cms\Asset\Enums\FileKind} instead. */ + public const string KIND_PDF = FileKind::Pdf->value; + + /** @deprecated 6.0.0 use {@see \CraftCms\Cms\Asset\Enums\FileKind} instead. */ + public const string KIND_PHOTOSHOP = FileKind::Photoshop->value; + + /** @deprecated 6.0.0 use {@see \CraftCms\Cms\Asset\Enums\FileKind} instead. */ + public const string KIND_PHP = FileKind::Php->value; + + /** @deprecated 6.0.0 use {@see \CraftCms\Cms\Asset\Enums\FileKind} instead. */ + public const string KIND_POWERPOINT = FileKind::Powerpoint->value; + + /** @deprecated 6.0.0 use {@see \CraftCms\Cms\Asset\Enums\FileKind} instead. */ + public const string KIND_TEXT = FileKind::Text->value; + + /** @deprecated 6.0.0 use {@see \CraftCms\Cms\Asset\Enums\FileKind} instead. */ + public const string KIND_VIDEO = FileKind::Video->value; + + /** @deprecated 6.0.0 use {@see \CraftCms\Cms\Asset\Enums\FileKind} instead. */ + public const string KIND_WORD = FileKind::Word->value; + + /** @deprecated 6.0.0 use {@see \CraftCms\Cms\Asset\Enums\FileKind} instead. */ + public const string KIND_XML = FileKind::Xml->value; + + /** @deprecated 6.0.0 use {@see \CraftCms\Cms\Asset\Enums\FileKind} instead. */ + public const string KIND_UNKNOWN = FileKind::Unknown->value; + + // Events + // ------------------------------------------------------------------------- + + /** + * @event AssetEvent The event that is triggered before an asset is uploaded to volume. + */ + public const string EVENT_BEFORE_HANDLE_FILE = 'beforeHandleFile'; + + /** + * @event GenerateTransformEvent The event that is triggered before a transform is generated for an asset. + */ + public const string EVENT_BEFORE_GENERATE_TRANSFORM = 'beforeGenerateTransform'; + + /** + * @event GenerateTransformEvent The event that is triggered after a transform is generated for an asset. + */ + public const string EVENT_AFTER_GENERATE_TRANSFORM = 'afterGenerateTransform'; + + public static function registerEvents(): void + { + Event::listen(function(AssetUrlResolving $event) { + if (YiiEvent::hasHandlers(self::class, self::EVENT_BEFORE_DEFINE_URL)) { + $yiiEvent = new DefineAssetUrlEvent([ + 'transform' => $event->transform, + 'asset' => $event->asset, + 'sender' => $event->asset, + ]); + + YiiEvent::trigger(self::class, self::EVENT_BEFORE_DEFINE_URL, $yiiEvent); + + $event->url = $yiiEvent->url; + $event->handled = $yiiEvent->handled; + } + }); + + Event::listen(function(AssetUrlDefined $event) { + if (YiiEvent::hasHandlers(self::class, self::EVENT_DEFINE_URL)) { + $yiiEvent = new DefineAssetUrlEvent([ + 'transform' => $event->transform, + 'asset' => $event->asset, + 'sender' => $event->asset, + ]); + + YiiEvent::trigger(self::class, self::EVENT_DEFINE_URL, $yiiEvent); + + $event->url = $yiiEvent->url; + $event->handled = $yiiEvent->handled; + } + }); + + Event::listen(function(TransformGenerating $event) { + if (YiiEvent::hasHandlers(self::class, self::EVENT_BEFORE_GENERATE_TRANSFORM)) { + $yiiEvent = new GenerateTransformEvent([ + 'transform' => $event->transform, + 'asset' => $event->asset, + 'url' => $event->url, + 'sender' => $event->asset, + ]); + + YiiEvent::trigger(self::class, self::EVENT_BEFORE_GENERATE_TRANSFORM, $yiiEvent); + + $event->url = $yiiEvent->url; + } + }); + + Event::listen(function(AfterGenerateTransform $event) { + if (YiiEvent::hasHandlers(self::class, self::EVENT_AFTER_GENERATE_TRANSFORM)) { + $yiiEvent = new GenerateTransformEvent([ + 'transform' => $event->transform, + 'asset' => $event->asset, + 'url' => $event->url, + 'sender' => $event->asset, + ]); + + YiiEvent::trigger(self::class, self::EVENT_AFTER_GENERATE_TRANSFORM, $yiiEvent); + } + }); + + Event::listen(function(AssetFileHandling $event) { + if (YiiEvent::hasHandlers(self::class, self::EVENT_BEFORE_HANDLE_FILE)) { + $yiiEvent = new AssetEvent([ + 'asset' => $event->asset, + 'isNew' => $event->isNew, + ]); + + YiiEvent::trigger(self::class, self::EVENT_BEFORE_HANDLE_FILE, $yiiEvent); + } + }); + } +} diff --git a/yii2-adapter/constants/Element/Concerns/LegacyNestedElementManager.php b/yii2-adapter/constants/Element/Concerns/LegacyNestedElementManager.php new file mode 100644 index 00000000000..687103700f0 --- /dev/null +++ b/yii2-adapter/constants/Element/Concerns/LegacyNestedElementManager.php @@ -0,0 +1,68 @@ + $event->elements, + 'sender' => $event->manager, + ])); + }); + + Event::listen(function(NewDuplicateNestedElementsEvent $event) { + if (!YiiEvent::hasHandlers(self::class, self::EVENT_AFTER_DUPLICATE_NESTED_ELEMENTS)) { + return; + } + + YiiEvent::trigger(self::class, self::EVENT_AFTER_DUPLICATE_NESTED_ELEMENTS, new DuplicateNestedElementsEvent([ + 'source' => $event->source, + 'target' => $event->target, + 'newElementIds' => $event->newElementIds, + 'sender' => $event->manager, + ])); + }); + + Event::listen(function(NestedElementRevisionsCreated $event) { + if (!YiiEvent::hasHandlers(self::class, self::EVENT_AFTER_CREATE_REVISIONS)) { + return; + } + + YiiEvent::trigger(self::class, self::EVENT_AFTER_CREATE_REVISIONS, new DuplicateNestedElementsEvent([ + 'source' => $event->source, + 'target' => $event->target, + 'newElementIds' => $event->newElementIds, + 'sender' => $event->manager, + ])); + }); + } +} diff --git a/yii2-adapter/constants/Entry/Concerns/LegacyConstants.php b/yii2-adapter/constants/Entry/Concerns/LegacyConstants.php new file mode 100644 index 00000000000..dc60e3a44db --- /dev/null +++ b/yii2-adapter/constants/Entry/Concerns/LegacyConstants.php @@ -0,0 +1,93 @@ + $event->entryTypes, + 'sender' => $event->entry, + ]); + + YiiEvent::trigger(self::class, self::EVENT_DEFINE_ENTRY_TYPES, $yiiEvent); + + $event->entryTypes = $yiiEvent->entryTypes; + } + }); + + Event::listen(function(EntryMetaFieldsResolving $event) { + if (YiiEvent::hasHandlers(self::class, self::EVENT_DEFINE_META_FIELDS)) { + $yiiEvent = new DefineMetaFields([ + 'element' => $event->entry, + 'sender' => $event->entry, + 'static' => $event->static, + 'fields' => $event->fields, + ]); + + YiiEvent::trigger(self::class, self::EVENT_DEFINE_META_FIELDS, $yiiEvent); + + $event->fields = $yiiEvent->fields; + } + }); + + Event::listen(function(EntryParentSelectionCriteriaResolving $event) { + if (YiiEvent::hasHandlers(self::class, self::EVENT_DEFINE_PARENT_SELECTION_CRITERIA)) { + $yiiEvent = new ElementCriteriaEvent([ + 'sender' => $event->entry, + 'criteria' => $event->criteria, + ]); + + YiiEvent::trigger(self::class, self::EVENT_DEFINE_PARENT_SELECTION_CRITERIA, $yiiEvent); + + $event->criteria = $yiiEvent->criteria; + } + }); + } +} diff --git a/yii2-adapter/constants/Field/Elements/Concerns/LegacyConstants.php b/yii2-adapter/constants/Field/Elements/Concerns/LegacyConstants.php new file mode 100644 index 00000000000..1065fcd46d2 --- /dev/null +++ b/yii2-adapter/constants/Field/Elements/Concerns/LegacyConstants.php @@ -0,0 +1,16 @@ +value; - - /** @deprecated 6.0.0 use {@see \CraftCms\Cms\Asset\Enums\FileKind} instead. */ - public const string KIND_AUDIO = FileKind::Audio->value; - - /** @deprecated 6.0.0 use {@see \CraftCms\Cms\Asset\Enums\FileKind} instead. */ - public const string KIND_CAPTIONS_SUBTITLES = FileKind::CaptionsSubtitles->value; - - /** @deprecated 6.0.0 use {@see \CraftCms\Cms\Asset\Enums\FileKind} instead. */ - public const string KIND_COMPRESSED = FileKind::Compressed->value; - - /** @deprecated 6.0.0 use {@see \CraftCms\Cms\Asset\Enums\FileKind} instead. */ - public const string KIND_EXCEL = FileKind::Excel->value; - - /** @deprecated 6.0.0 use {@see \CraftCms\Cms\Asset\Enums\FileKind} instead. */ - public const string KIND_FLASH = FileKind::Flash->value; - - /** @deprecated 6.0.0 use {@see \CraftCms\Cms\Asset\Enums\FileKind} instead. */ - public const string KIND_HTML = FileKind::Html->value; - - /** @deprecated 6.0.0 use {@see \CraftCms\Cms\Asset\Enums\FileKind} instead. */ - public const string KIND_ILLUSTRATOR = FileKind::Illustrator->value; - - /** @deprecated 6.0.0 use {@see \CraftCms\Cms\Asset\Enums\FileKind} instead. */ - public const string KIND_IMAGE = FileKind::Image->value; - - /** @deprecated 6.0.0 use {@see \CraftCms\Cms\Asset\Enums\FileKind} instead. */ - public const string KIND_JAVASCRIPT = FileKind::Javascript->value; - - /** @deprecated 6.0.0 use {@see \CraftCms\Cms\Asset\Enums\FileKind} instead. */ - public const string KIND_JSON = FileKind::Json->value; - - /** @deprecated 6.0.0 use {@see \CraftCms\Cms\Asset\Enums\FileKind} instead. */ - public const string KIND_PDF = FileKind::Pdf->value; - - /** @deprecated 6.0.0 use {@see \CraftCms\Cms\Asset\Enums\FileKind} instead. */ - public const string KIND_PHOTOSHOP = FileKind::Photoshop->value; - - /** @deprecated 6.0.0 use {@see \CraftCms\Cms\Asset\Enums\FileKind} instead. */ - public const string KIND_PHP = FileKind::Php->value; - - /** @deprecated 6.0.0 use {@see \CraftCms\Cms\Asset\Enums\FileKind} instead. */ - public const string KIND_POWERPOINT = FileKind::Powerpoint->value; - - /** @deprecated 6.0.0 use {@see \CraftCms\Cms\Asset\Enums\FileKind} instead. */ - public const string KIND_TEXT = FileKind::Text->value; - - /** @deprecated 6.0.0 use {@see \CraftCms\Cms\Asset\Enums\FileKind} instead. */ - public const string KIND_VIDEO = FileKind::Video->value; - - /** @deprecated 6.0.0 use {@see \CraftCms\Cms\Asset\Enums\FileKind} instead. */ - public const string KIND_WORD = FileKind::Word->value; - - /** @deprecated 6.0.0 use {@see \CraftCms\Cms\Asset\Enums\FileKind} instead. */ - public const string KIND_XML = FileKind::Xml->value; - - /** @deprecated 6.0.0 use {@see \CraftCms\Cms\Asset\Enums\FileKind} instead. */ - public const string KIND_UNKNOWN = FileKind::Unknown->value; - - // Events - // ------------------------------------------------------------------------- - +/** @phpstan-ignore-next-line */ +if (false) { /** - * @event AssetEvent The event that is triggered before an asset is uploaded to volume. + * @since 3.0.0 + * @deprecated 6.0.0 use {@see \CraftCms\Cms\Asset\Elements\Asset} instead. */ - public const string EVENT_BEFORE_HANDLE_FILE = 'beforeHandleFile'; - - /** - * @event GenerateTransformEvent The event that is triggered before a transform is generated for an asset. - */ - public const string EVENT_BEFORE_GENERATE_TRANSFORM = 'beforeGenerateTransform'; - - /** - * @event GenerateTransformEvent The event that is triggered after a transform is generated for an asset. - */ - public const string EVENT_AFTER_GENERATE_TRANSFORM = 'afterGenerateTransform'; - - public static function registerEvents(): void + class Asset extends \CraftCms\Cms\Asset\Elements\Asset { - Event::listen(function(AssetUrlResolving $event) { - if (YiiEvent::hasHandlers(self::class, self::EVENT_BEFORE_DEFINE_URL)) { - $yiiEvent = new DefineAssetUrlEvent([ - 'transform' => $event->transform, - 'asset' => $event->asset, - 'sender' => $event->asset, - ]); - - YiiEvent::trigger(self::class, self::EVENT_BEFORE_DEFINE_URL, $yiiEvent); - - $event->url = $yiiEvent->url; - $event->handled = $yiiEvent->handled; - } - }); - - Event::listen(function(AssetUrlDefined $event) { - if (YiiEvent::hasHandlers(self::class, self::EVENT_DEFINE_URL)) { - $yiiEvent = new DefineAssetUrlEvent([ - 'transform' => $event->transform, - 'asset' => $event->asset, - 'sender' => $event->asset, - ]); - - YiiEvent::trigger(self::class, self::EVENT_DEFINE_URL, $yiiEvent); - - $event->url = $yiiEvent->url; - $event->handled = $yiiEvent->handled; - } - }); - - Event::listen(function(TransformGenerating $event) { - if (YiiEvent::hasHandlers(self::class, self::EVENT_BEFORE_GENERATE_TRANSFORM)) { - $yiiEvent = new GenerateTransformEvent([ - 'transform' => $event->transform, - 'asset' => $event->asset, - 'url' => $event->url, - 'sender' => $event->asset, - ]); - - YiiEvent::trigger(self::class, self::EVENT_BEFORE_GENERATE_TRANSFORM, $yiiEvent); - - $event->url = $yiiEvent->url; - } - }); - - Event::listen(function(AfterGenerateTransform $event) { - if (YiiEvent::hasHandlers(self::class, self::EVENT_AFTER_GENERATE_TRANSFORM)) { - $yiiEvent = new GenerateTransformEvent([ - 'transform' => $event->transform, - 'asset' => $event->asset, - 'url' => $event->url, - 'sender' => $event->asset, - ]); - - YiiEvent::trigger(self::class, self::EVENT_AFTER_GENERATE_TRANSFORM, $yiiEvent); - } - }); - - Event::listen(function(AssetFileHandling $event) { - if (YiiEvent::hasHandlers(self::class, self::EVENT_BEFORE_HANDLE_FILE)) { - $yiiEvent = new AssetEvent([ - 'asset' => $event->asset, - 'isNew' => $event->isNew, - ]); - - YiiEvent::trigger(self::class, self::EVENT_BEFORE_HANDLE_FILE, $yiiEvent); - } - }); } } + +class_alias(\CraftCms\Cms\Asset\Elements\Asset::class, Asset::class); diff --git a/yii2-adapter/legacy/elements/ContentBlock.php b/yii2-adapter/legacy/elements/ContentBlock.php index 7a1a30b994d..63b9461aa0d 100644 --- a/yii2-adapter/legacy/elements/ContentBlock.php +++ b/yii2-adapter/legacy/elements/ContentBlock.php @@ -9,13 +9,15 @@ namespace craft\elements; -use craft\base\ElementEventConstants; - -/** - * @since 5.8.0 - * @deprecated 6.0.0 use {@see \CraftCms\Cms\Field\Elements\ContentBlock} instead. - */ -class ContentBlock extends \CraftCms\Cms\Field\Elements\ContentBlock -{ - use ElementEventConstants; +/** @phpstan-ignore-next-line */ +if (false) { + /** + * @since 5.8.0 + * @deprecated 6.0.0 use {@see \CraftCms\Cms\Field\Elements\ContentBlock} instead. + */ + class ContentBlock extends \CraftCms\Cms\Field\Elements\ContentBlock + { + } } + +class_alias(\CraftCms\Cms\Field\Elements\ContentBlock::class, ContentBlock::class); diff --git a/yii2-adapter/legacy/elements/Entry.php b/yii2-adapter/legacy/elements/Entry.php index 9f086c2f642..4fa8eb76204 100644 --- a/yii2-adapter/legacy/elements/Entry.php +++ b/yii2-adapter/legacy/elements/Entry.php @@ -9,89 +9,15 @@ namespace craft\elements; -use craft\base\ElementEventConstants; -use craft\base\Event as YiiEvent; -use craft\events\DefineEntryTypesEvent; -use craft\events\DefineMetaFields; -use craft\events\ElementCriteriaEvent; -use CraftCms\Cms\Entry\Events\EntryMetaFieldsResolving; -use CraftCms\Cms\Entry\Events\EntryParentSelectionCriteriaResolving; -use CraftCms\Cms\Entry\Events\EntryTypesResolving; -use Illuminate\Support\Facades\Event; - -/** - * @since 3.0.0 - * @deprecated 6.0.0 use {@see \CraftCms\Cms\Entry\Elements\Entry} instead. - */ -class Entry extends \CraftCms\Cms\Entry\Elements\Entry -{ - use ElementEventConstants; - - /** - * @event DefineEntryTypesEvent The event that is triggered when defining the available entry types for the entry - * - * @see getAvailableEntryTypes() - * @since 3.6.0 - */ - public const string EVENT_DEFINE_ENTRY_TYPES = 'defineEntryTypes'; - - /** - * @event ElementCriteriaEvent The event that is triggered when defining the parent selection criteria. - * - * @see _parentOptionCriteria() - * @since 4.4.0 - */ - public const string EVENT_DEFINE_PARENT_SELECTION_CRITERIA = 'defineParentSelectionCriteria'; - +/** @phpstan-ignore-next-line */ +if (false) { /** - * @event DefineMetaFields The event that is triggered when defining the meta fields. - * - * @see metaFieldsHtml() - * @since 5.9.0 + * @since 3.0.0 + * @deprecated 6.0.0 use {@see \CraftCms\Cms\Entry\Elements\Entry} instead. */ - public const string EVENT_DEFINE_META_FIELDS = 'defineEntryMetaFields'; - - public static function registerEvents(): void + class Entry extends \CraftCms\Cms\Entry\Elements\Entry { - Event::listen(function(EntryTypesResolving $event) { - if (YiiEvent::hasHandlers(self::class, self::EVENT_DEFINE_ENTRY_TYPES)) { - $yiiEvent = new DefineEntryTypesEvent([ - 'entryTypes' => $event->entryTypes, - 'sender' => $event->entry, - ]); - - YiiEvent::trigger(self::class, self::EVENT_DEFINE_ENTRY_TYPES, $yiiEvent); - - $event->entryTypes = $yiiEvent->entryTypes; - } - }); - - Event::listen(function(EntryMetaFieldsResolving $event) { - if (YiiEvent::hasHandlers(self::class, self::EVENT_DEFINE_META_FIELDS)) { - $yiiEvent = new DefineMetaFields([ - 'element' => $event->entry, - 'sender' => $event->entry, - 'static' => $event->static, - 'fields' => $event->fields, - ]); - - YiiEvent::trigger(self::class, self::EVENT_DEFINE_META_FIELDS, $yiiEvent); - - $event->fields = $yiiEvent->fields; - } - }); - - Event::listen(function(EntryParentSelectionCriteriaResolving $event) { - if (YiiEvent::hasHandlers(self::class, self::EVENT_DEFINE_PARENT_SELECTION_CRITERIA)) { - $yiiEvent = new ElementCriteriaEvent([ - 'sender' => $event->entry, - 'criteria' => $event->criteria, - ]); - - YiiEvent::trigger(self::class, self::EVENT_DEFINE_PARENT_SELECTION_CRITERIA, $yiiEvent); - - $event->criteria = $yiiEvent->criteria; - } - }); } } + +class_alias(\CraftCms\Cms\Entry\Elements\Entry::class, Entry::class); diff --git a/yii2-adapter/legacy/elements/NestedElementManager.php b/yii2-adapter/legacy/elements/NestedElementManager.php index 9f5e05cf721..87a5f2b7c23 100644 --- a/yii2-adapter/legacy/elements/NestedElementManager.php +++ b/yii2-adapter/legacy/elements/NestedElementManager.php @@ -2,64 +2,14 @@ namespace craft\elements; -use craft\base\Event as YiiEvent; -use craft\events\BulkElementsEvent; -use craft\events\DuplicateNestedElementsEvent; -use CraftCms\Cms\Element\Events\NestedElementRevisionsCreated; -use CraftCms\Cms\Element\Events\NestedElementsDuplicated as NewDuplicateNestedElementsEvent; -use CraftCms\Cms\Element\Events\NestedElementsSaved; -use Illuminate\Support\Facades\Event; - -/** - * @deprecated 6.0.0 use {@see \CraftCms\Cms\Element\NestedElementManager} instead. - */ -class NestedElementManager extends \CraftCms\Cms\Element\NestedElementManager -{ - use \craft\base\LegacyEventConstants; - - public const EVENT_AFTER_SAVE_ELEMENTS = 'afterSaveElements'; - - public const EVENT_AFTER_DUPLICATE_NESTED_ELEMENTS = 'afterDuplicateNestedElements'; - - public const EVENT_AFTER_CREATE_REVISIONS = 'afterCreateRevisions'; - - public static function registerEvents(): void +/** @phpstan-ignore-next-line */ +if (false) { + /** + * @deprecated 6.0.0 use {@see \CraftCms\Cms\Element\NestedElementManager} instead. + */ + class NestedElementManager extends \CraftCms\Cms\Element\NestedElementManager { - Event::listen(function(NestedElementsSaved $event) { - if (!YiiEvent::hasHandlers(self::class, self::EVENT_AFTER_SAVE_ELEMENTS)) { - return; - } - - YiiEvent::trigger(self::class, self::EVENT_AFTER_SAVE_ELEMENTS, new BulkElementsEvent([ - 'elements' => $event->elements, - 'sender' => $event->manager, - ])); - }); - - Event::listen(function(NewDuplicateNestedElementsEvent $event) { - if (!YiiEvent::hasHandlers(self::class, self::EVENT_AFTER_DUPLICATE_NESTED_ELEMENTS)) { - return; - } - - YiiEvent::trigger(self::class, self::EVENT_AFTER_DUPLICATE_NESTED_ELEMENTS, new DuplicateNestedElementsEvent([ - 'source' => $event->source, - 'target' => $event->target, - 'newElementIds' => $event->newElementIds, - 'sender' => $event->manager, - ])); - }); - - Event::listen(function(NestedElementRevisionsCreated $event) { - if (!YiiEvent::hasHandlers(self::class, self::EVENT_AFTER_CREATE_REVISIONS)) { - return; - } - - YiiEvent::trigger(self::class, self::EVENT_AFTER_CREATE_REVISIONS, new DuplicateNestedElementsEvent([ - 'source' => $event->source, - 'target' => $event->target, - 'newElementIds' => $event->newElementIds, - 'sender' => $event->manager, - ])); - }); } } + +class_alias(\CraftCms\Cms\Element\NestedElementManager::class, NestedElementManager::class); diff --git a/yii2-adapter/legacy/elements/User.php b/yii2-adapter/legacy/elements/User.php index f97359e74a5..b0a321a7c50 100644 --- a/yii2-adapter/legacy/elements/User.php +++ b/yii2-adapter/legacy/elements/User.php @@ -11,6 +11,7 @@ use CraftCms\Cms\User\Elements\User as UserElement; +/** @phpstan-ignore-next-line */ if (false) { /** * @deprecated 6.0.0 use {@see UserElement} instead. From cfb3d4c3fafad562f2d3313a1f977cd2bf650546 Mon Sep 17 00:00:00 2001 From: Rias Date: Tue, 5 May 2026 16:41:07 +0200 Subject: [PATCH 4/6] Use correct classes --- .../Asset/Concerns/LegacyConstants.php | 21 ++++++++++--------- .../Concerns/LegacyNestedElementManager.php | 13 ++++++------ .../Entry/Concerns/LegacyConstants.php | 13 ++++++------ .../User/Concerns/LegacyConstants.php | 13 ++++++------ 4 files changed, 32 insertions(+), 28 deletions(-) diff --git a/yii2-adapter/constants/Asset/Concerns/LegacyConstants.php b/yii2-adapter/constants/Asset/Concerns/LegacyConstants.php index 838319b13e8..dca5111f415 100644 --- a/yii2-adapter/constants/Asset/Concerns/LegacyConstants.php +++ b/yii2-adapter/constants/Asset/Concerns/LegacyConstants.php @@ -6,6 +6,7 @@ use craft\base\ElementEventConstants; use craft\base\Event as YiiEvent; +use craft\elements\Asset; use craft\events\AssetEvent; use craft\events\DefineAssetUrlEvent; use craft\events\GenerateTransformEvent; @@ -125,14 +126,14 @@ trait LegacyConstants public static function registerEvents(): void { Event::listen(function(AssetUrlResolving $event) { - if (YiiEvent::hasHandlers(self::class, self::EVENT_BEFORE_DEFINE_URL)) { + if (YiiEvent::hasHandlers(Asset::class, self::EVENT_BEFORE_DEFINE_URL)) { $yiiEvent = new DefineAssetUrlEvent([ 'transform' => $event->transform, 'asset' => $event->asset, 'sender' => $event->asset, ]); - YiiEvent::trigger(self::class, self::EVENT_BEFORE_DEFINE_URL, $yiiEvent); + YiiEvent::trigger(Asset::class, self::EVENT_BEFORE_DEFINE_URL, $yiiEvent); $event->url = $yiiEvent->url; $event->handled = $yiiEvent->handled; @@ -140,14 +141,14 @@ public static function registerEvents(): void }); Event::listen(function(AssetUrlDefined $event) { - if (YiiEvent::hasHandlers(self::class, self::EVENT_DEFINE_URL)) { + if (YiiEvent::hasHandlers(Asset::class, self::EVENT_DEFINE_URL)) { $yiiEvent = new DefineAssetUrlEvent([ 'transform' => $event->transform, 'asset' => $event->asset, 'sender' => $event->asset, ]); - YiiEvent::trigger(self::class, self::EVENT_DEFINE_URL, $yiiEvent); + YiiEvent::trigger(Asset::class, self::EVENT_DEFINE_URL, $yiiEvent); $event->url = $yiiEvent->url; $event->handled = $yiiEvent->handled; @@ -155,7 +156,7 @@ public static function registerEvents(): void }); Event::listen(function(TransformGenerating $event) { - if (YiiEvent::hasHandlers(self::class, self::EVENT_BEFORE_GENERATE_TRANSFORM)) { + if (YiiEvent::hasHandlers(Asset::class, self::EVENT_BEFORE_GENERATE_TRANSFORM)) { $yiiEvent = new GenerateTransformEvent([ 'transform' => $event->transform, 'asset' => $event->asset, @@ -163,14 +164,14 @@ public static function registerEvents(): void 'sender' => $event->asset, ]); - YiiEvent::trigger(self::class, self::EVENT_BEFORE_GENERATE_TRANSFORM, $yiiEvent); + YiiEvent::trigger(Asset::class, self::EVENT_BEFORE_GENERATE_TRANSFORM, $yiiEvent); $event->url = $yiiEvent->url; } }); Event::listen(function(AfterGenerateTransform $event) { - if (YiiEvent::hasHandlers(self::class, self::EVENT_AFTER_GENERATE_TRANSFORM)) { + if (YiiEvent::hasHandlers(Asset::class, self::EVENT_AFTER_GENERATE_TRANSFORM)) { $yiiEvent = new GenerateTransformEvent([ 'transform' => $event->transform, 'asset' => $event->asset, @@ -178,18 +179,18 @@ public static function registerEvents(): void 'sender' => $event->asset, ]); - YiiEvent::trigger(self::class, self::EVENT_AFTER_GENERATE_TRANSFORM, $yiiEvent); + YiiEvent::trigger(Asset::class, self::EVENT_AFTER_GENERATE_TRANSFORM, $yiiEvent); } }); Event::listen(function(AssetFileHandling $event) { - if (YiiEvent::hasHandlers(self::class, self::EVENT_BEFORE_HANDLE_FILE)) { + if (YiiEvent::hasHandlers(Asset::class, self::EVENT_BEFORE_HANDLE_FILE)) { $yiiEvent = new AssetEvent([ 'asset' => $event->asset, 'isNew' => $event->isNew, ]); - YiiEvent::trigger(self::class, self::EVENT_BEFORE_HANDLE_FILE, $yiiEvent); + YiiEvent::trigger(Asset::class, self::EVENT_BEFORE_HANDLE_FILE, $yiiEvent); } }); } diff --git a/yii2-adapter/constants/Element/Concerns/LegacyNestedElementManager.php b/yii2-adapter/constants/Element/Concerns/LegacyNestedElementManager.php index 687103700f0..31743c8c588 100644 --- a/yii2-adapter/constants/Element/Concerns/LegacyNestedElementManager.php +++ b/yii2-adapter/constants/Element/Concerns/LegacyNestedElementManager.php @@ -5,6 +5,7 @@ use craft\base\Event as YiiEvent; use craft\base\LegacyEventConstants; +use craft\elements\NestedElementManager; use craft\events\BulkElementsEvent; use craft\events\DuplicateNestedElementsEvent; use CraftCms\Cms\Element\Events\NestedElementRevisionsCreated; @@ -29,22 +30,22 @@ trait LegacyNestedElementManager public static function registerEvents(): void { Event::listen(function(NestedElementsSaved $event) { - if (!YiiEvent::hasHandlers(self::class, self::EVENT_AFTER_SAVE_ELEMENTS)) { + if (!YiiEvent::hasHandlers(NestedElementManager::class, self::EVENT_AFTER_SAVE_ELEMENTS)) { return; } - YiiEvent::trigger(self::class, self::EVENT_AFTER_SAVE_ELEMENTS, new BulkElementsEvent([ + YiiEvent::trigger(NestedElementManager::class, self::EVENT_AFTER_SAVE_ELEMENTS, new BulkElementsEvent([ 'elements' => $event->elements, 'sender' => $event->manager, ])); }); Event::listen(function(NewDuplicateNestedElementsEvent $event) { - if (!YiiEvent::hasHandlers(self::class, self::EVENT_AFTER_DUPLICATE_NESTED_ELEMENTS)) { + if (!YiiEvent::hasHandlers(NestedElementManager::class, self::EVENT_AFTER_DUPLICATE_NESTED_ELEMENTS)) { return; } - YiiEvent::trigger(self::class, self::EVENT_AFTER_DUPLICATE_NESTED_ELEMENTS, new DuplicateNestedElementsEvent([ + YiiEvent::trigger(NestedElementManager::class, self::EVENT_AFTER_DUPLICATE_NESTED_ELEMENTS, new DuplicateNestedElementsEvent([ 'source' => $event->source, 'target' => $event->target, 'newElementIds' => $event->newElementIds, @@ -53,11 +54,11 @@ public static function registerEvents(): void }); Event::listen(function(NestedElementRevisionsCreated $event) { - if (!YiiEvent::hasHandlers(self::class, self::EVENT_AFTER_CREATE_REVISIONS)) { + if (!YiiEvent::hasHandlers(NestedElementManager::class, self::EVENT_AFTER_CREATE_REVISIONS)) { return; } - YiiEvent::trigger(self::class, self::EVENT_AFTER_CREATE_REVISIONS, new DuplicateNestedElementsEvent([ + YiiEvent::trigger(NestedElementManager::class, self::EVENT_AFTER_CREATE_REVISIONS, new DuplicateNestedElementsEvent([ 'source' => $event->source, 'target' => $event->target, 'newElementIds' => $event->newElementIds, diff --git a/yii2-adapter/constants/Entry/Concerns/LegacyConstants.php b/yii2-adapter/constants/Entry/Concerns/LegacyConstants.php index dc60e3a44db..71a25143748 100644 --- a/yii2-adapter/constants/Entry/Concerns/LegacyConstants.php +++ b/yii2-adapter/constants/Entry/Concerns/LegacyConstants.php @@ -6,6 +6,7 @@ use craft\base\ElementEventConstants; use craft\base\Event as YiiEvent; +use craft\elements\Entry; use craft\events\DefineEntryTypesEvent; use craft\events\DefineMetaFields; use craft\events\ElementCriteriaEvent; @@ -50,20 +51,20 @@ trait LegacyConstants public static function registerEvents(): void { Event::listen(function(EntryTypesResolving $event) { - if (YiiEvent::hasHandlers(self::class, self::EVENT_DEFINE_ENTRY_TYPES)) { + if (YiiEvent::hasHandlers(Entry::class, self::EVENT_DEFINE_ENTRY_TYPES)) { $yiiEvent = new DefineEntryTypesEvent([ 'entryTypes' => $event->entryTypes, 'sender' => $event->entry, ]); - YiiEvent::trigger(self::class, self::EVENT_DEFINE_ENTRY_TYPES, $yiiEvent); + YiiEvent::trigger(Entry::class, self::EVENT_DEFINE_ENTRY_TYPES, $yiiEvent); $event->entryTypes = $yiiEvent->entryTypes; } }); Event::listen(function(EntryMetaFieldsResolving $event) { - if (YiiEvent::hasHandlers(self::class, self::EVENT_DEFINE_META_FIELDS)) { + if (YiiEvent::hasHandlers(Entry::class, self::EVENT_DEFINE_META_FIELDS)) { $yiiEvent = new DefineMetaFields([ 'element' => $event->entry, 'sender' => $event->entry, @@ -71,20 +72,20 @@ public static function registerEvents(): void 'fields' => $event->fields, ]); - YiiEvent::trigger(self::class, self::EVENT_DEFINE_META_FIELDS, $yiiEvent); + YiiEvent::trigger(Entry::class, self::EVENT_DEFINE_META_FIELDS, $yiiEvent); $event->fields = $yiiEvent->fields; } }); Event::listen(function(EntryParentSelectionCriteriaResolving $event) { - if (YiiEvent::hasHandlers(self::class, self::EVENT_DEFINE_PARENT_SELECTION_CRITERIA)) { + if (YiiEvent::hasHandlers(Entry::class, self::EVENT_DEFINE_PARENT_SELECTION_CRITERIA)) { $yiiEvent = new ElementCriteriaEvent([ 'sender' => $event->entry, 'criteria' => $event->criteria, ]); - YiiEvent::trigger(self::class, self::EVENT_DEFINE_PARENT_SELECTION_CRITERIA, $yiiEvent); + YiiEvent::trigger(Entry::class, self::EVENT_DEFINE_PARENT_SELECTION_CRITERIA, $yiiEvent); $event->criteria = $yiiEvent->criteria; } diff --git a/yii2-adapter/constants/User/Concerns/LegacyConstants.php b/yii2-adapter/constants/User/Concerns/LegacyConstants.php index 8a5f1c6ffce..ec52cfdab1e 100644 --- a/yii2-adapter/constants/User/Concerns/LegacyConstants.php +++ b/yii2-adapter/constants/User/Concerns/LegacyConstants.php @@ -7,6 +7,7 @@ use craft\base\ElementEventConstants; use craft\base\Event as YiiEvent; use craft\base\LegacyEventConstants; +use craft\elements\User; use craft\events\AuthenticateUserEvent; use craft\events\DefineValueEvent; use CraftCms\Cms\Auth\Events\UserAuthenticating; @@ -84,11 +85,11 @@ public function getFullName(): ?string public static function registerEvents(): void { Event::listen(function(UserNameResolving $event) { - if (YiiEvent::hasHandlers(self::class, self::EVENT_DEFINE_NAME)) { + if (YiiEvent::hasHandlers(User::class, self::EVENT_DEFINE_NAME)) { $yiiEvent = new DefineValueEvent(); $yiiEvent->sender = $event->user; - YiiEvent::trigger(self::class, self::EVENT_DEFINE_NAME, $yiiEvent); + YiiEvent::trigger(User::class, self::EVENT_DEFINE_NAME, $yiiEvent); if ($yiiEvent->value !== null) { $event->name = $yiiEvent->value; @@ -97,11 +98,11 @@ public static function registerEvents(): void }); Event::listen(function(UserFriendlyNameResolving $event) { - if (YiiEvent::hasHandlers(self::class, self::EVENT_DEFINE_FRIENDLY_NAME)) { + if (YiiEvent::hasHandlers(User::class, self::EVENT_DEFINE_FRIENDLY_NAME)) { $yiiEvent = new DefineValueEvent(); $yiiEvent->sender = $event->user; - YiiEvent::trigger(self::class, self::EVENT_DEFINE_FRIENDLY_NAME, $yiiEvent); + YiiEvent::trigger(User::class, self::EVENT_DEFINE_FRIENDLY_NAME, $yiiEvent); if ($yiiEvent->value !== null) { $event->name = $yiiEvent->value; @@ -110,10 +111,10 @@ public static function registerEvents(): void }); Event::listen(UserAuthenticating::class, function(UserAuthenticating $event) { - if (YiiEvent::hasHandlers(self::class, self::EVENT_BEFORE_AUTHENTICATE)) { + if (YiiEvent::hasHandlers(User::class, self::EVENT_BEFORE_AUTHENTICATE)) { $yiiEvent = new AuthenticateUserEvent(['password' => $event->credentials['password']]); - YiiEvent::trigger(self::class, self::EVENT_BEFORE_AUTHENTICATE, $yiiEvent); + YiiEvent::trigger(User::class, self::EVENT_BEFORE_AUTHENTICATE, $yiiEvent); $event->performAuthentication = $yiiEvent->performAuthentication; } From f7852c5d3e9c14d68a575843c489fbb2f66f5c7d Mon Sep 17 00:00:00 2001 From: Rias Date: Tue, 5 May 2026 16:43:07 +0200 Subject: [PATCH 5/6] Fix legacy behavior compatibility --- yii2-adapter/legacy/base/Element.php | 296 +++++++++++------- .../LegacyBehaviorCompatibilityTest.php | 22 +- 2 files changed, 192 insertions(+), 126 deletions(-) diff --git a/yii2-adapter/legacy/base/Element.php b/yii2-adapter/legacy/base/Element.php index e85fb7c26ce..968fed64e8e 100644 --- a/yii2-adapter/legacy/base/Element.php +++ b/yii2-adapter/legacy/base/Element.php @@ -153,11 +153,11 @@ public static function registerEvents(): void Event::listen(function(ElementCacheTagsResolving $event) use ($elementClasses) { foreach ($elementClasses as $class) { - if (!YiiEvent::hasHandlers($class, self::EVENT_DEFINE_CACHE_TAGS)) { + if (!self::hasEventHandlers($class, self::EVENT_DEFINE_CACHE_TAGS)) { continue; } - if (!is_subclass_of($class, $event->element::class)) { + if (!self::matchesElementClass($class, $event->element::class)) { continue; } @@ -166,7 +166,7 @@ public static function registerEvents(): void 'value' => $event->tags, ]); - YiiEvent::trigger($class, self::EVENT_DEFINE_CACHE_TAGS, $yiiEvent); + self::triggerEvent($class, self::EVENT_DEFINE_CACHE_TAGS, $yiiEvent); $event->tags = $yiiEvent->value; } @@ -174,11 +174,11 @@ public static function registerEvents(): void Event::listen(function(ElementSourcesResolving $event) use ($elementClasses) { foreach ($elementClasses as $class) { - if (!YiiEvent::hasHandlers($class, self::EVENT_REGISTER_SOURCES)) { + if (!self::hasEventHandlers($class, self::EVENT_REGISTER_SOURCES)) { continue; } - if (!is_subclass_of($class, $event->elementType)) { + if (!self::matchesElementClass($class, $event->elementType)) { continue; } @@ -187,7 +187,7 @@ public static function registerEvents(): void 'sources' => $event->sources, ]); - YiiEvent::trigger($class, self::EVENT_REGISTER_SOURCES, $yiiEvent); + self::triggerEvent($class, self::EVENT_REGISTER_SOURCES, $yiiEvent); $event->sources = $yiiEvent->sources; } @@ -195,11 +195,11 @@ public static function registerEvents(): void Event::listen(function(ElementFieldLayoutsResolving $event) use ($elementClasses) { foreach ($elementClasses as $class) { - if (!YiiEvent::hasHandlers($class, self::EVENT_REGISTER_FIELD_LAYOUTS)) { + if (!self::hasEventHandlers($class, self::EVENT_REGISTER_FIELD_LAYOUTS)) { continue; } - if (!is_subclass_of($class, $event->elementType)) { + if (!self::matchesElementClass($class, $event->elementType)) { continue; } @@ -208,7 +208,7 @@ public static function registerEvents(): void 'fieldLayouts' => $event->fieldLayouts, ]); - YiiEvent::trigger($class, self::EVENT_REGISTER_FIELD_LAYOUTS, $yiiEvent); + self::triggerEvent($class, self::EVENT_REGISTER_FIELD_LAYOUTS, $yiiEvent); $event->fieldLayouts = $yiiEvent->fieldLayouts; } @@ -216,11 +216,11 @@ public static function registerEvents(): void Event::listen(function(ElementPreviewTargetsResolving $event) use ($elementClasses) { foreach ($elementClasses as $class) { - if (!YiiEvent::hasHandlers($class, self::EVENT_REGISTER_PREVIEW_TARGETS)) { + if (!self::hasEventHandlers($class, self::EVENT_REGISTER_PREVIEW_TARGETS)) { continue; } - if (!is_subclass_of($class, $event->element::class)) { + if (!self::matchesElementClass($class, $event->element::class)) { continue; } @@ -229,7 +229,7 @@ public static function registerEvents(): void 'previewTargets' => $event->previewTargets, ]); - YiiEvent::trigger($class, self::EVENT_REGISTER_PREVIEW_TARGETS, $yiiEvent); + self::triggerEvent($class, self::EVENT_REGISTER_PREVIEW_TARGETS, $yiiEvent); $event->previewTargets = $yiiEvent->previewTargets; } @@ -237,11 +237,11 @@ public static function registerEvents(): void Event::listen(function(ElementActionsResolving $event) use ($elementClasses) { foreach ($elementClasses as $class) { - if (!YiiEvent::hasHandlers($class, self::EVENT_REGISTER_ACTIONS)) { + if (!self::hasEventHandlers($class, self::EVENT_REGISTER_ACTIONS)) { continue; } - if (!is_subclass_of($class, $event->elementType)) { + if (!self::matchesElementClass($class, $event->elementType)) { continue; } @@ -250,7 +250,7 @@ public static function registerEvents(): void 'actions' => $event->actions, ]); - YiiEvent::trigger($class, self::EVENT_REGISTER_ACTIONS, $yiiEvent); + self::triggerEvent($class, self::EVENT_REGISTER_ACTIONS, $yiiEvent); $event->actions = $yiiEvent->actions; } @@ -258,11 +258,11 @@ public static function registerEvents(): void Event::listen(function(ElementExportersResolving $event) use ($elementClasses) { foreach ($elementClasses as $class) { - if (!YiiEvent::hasHandlers($class, self::EVENT_REGISTER_EXPORTERS)) { + if (!self::hasEventHandlers($class, self::EVENT_REGISTER_EXPORTERS)) { continue; } - if (!is_subclass_of($class, $event->elementType)) { + if (!self::matchesElementClass($class, $event->elementType)) { continue; } @@ -271,7 +271,7 @@ public static function registerEvents(): void 'exporters' => $event->exporters, ]); - YiiEvent::trigger($class, self::EVENT_REGISTER_EXPORTERS, $yiiEvent); + self::triggerEvent($class, self::EVENT_REGISTER_EXPORTERS, $yiiEvent); $event->exporters = $yiiEvent->exporters; } @@ -279,11 +279,11 @@ public static function registerEvents(): void Event::listen(function(ElementRendering $event) use ($elementClasses) { foreach ($elementClasses as $class) { - if (!YiiEvent::hasHandlers($class, self::EVENT_RENDER)) { + if (!self::hasEventHandlers($class, self::EVENT_RENDER)) { continue; } - if (!is_subclass_of($class, $event->element::class)) { + if (!self::matchesElementClass($class, $event->element::class)) { continue; } @@ -293,7 +293,7 @@ public static function registerEvents(): void 'variables' => $event->variables, ]); - YiiEvent::trigger($class, self::EVENT_RENDER, $yiiEvent); + self::triggerEvent($class, self::EVENT_RENDER, $yiiEvent); if (isset($yiiEvent->output)) { $event->output = $yiiEvent->output; @@ -305,11 +305,11 @@ public static function registerEvents(): void Event::listen(function(ElementKeywordsResolving $event) use ($elementClasses) { foreach ($elementClasses as $class) { - if (!YiiEvent::hasHandlers($class, self::EVENT_DEFINE_KEYWORDS)) { + if (!self::hasEventHandlers($class, self::EVENT_DEFINE_KEYWORDS)) { continue; } - if (!is_subclass_of($class, $event->element::class)) { + if (!self::matchesElementClass($class, $event->element::class)) { continue; } @@ -319,7 +319,7 @@ public static function registerEvents(): void 'keywords' => $event->keywords, ]); - YiiEvent::trigger($class, self::EVENT_DEFINE_KEYWORDS, $yiiEvent); + self::triggerEvent($class, self::EVENT_DEFINE_KEYWORDS, $yiiEvent); if ($yiiEvent->handled) { $event->keywords = $yiiEvent->keywords; @@ -330,11 +330,11 @@ public static function registerEvents(): void Event::listen(function(ElementSortOptionsResolving $event) use ($elementClasses) { foreach ($elementClasses as $class) { - if (!YiiEvent::hasHandlers($class, self::EVENT_REGISTER_SORT_OPTIONS)) { + if (!self::hasEventHandlers($class, self::EVENT_REGISTER_SORT_OPTIONS)) { continue; } - if (!is_subclass_of($class, $event->elementType)) { + if (!self::matchesElementClass($class, $event->elementType)) { continue; } @@ -342,7 +342,7 @@ public static function registerEvents(): void 'sortOptions' => $event->sortOptions, ]); - YiiEvent::trigger($class, self::EVENT_REGISTER_SORT_OPTIONS, $yiiEvent); + self::triggerEvent($class, self::EVENT_REGISTER_SORT_OPTIONS, $yiiEvent); $event->sortOptions = $yiiEvent->sortOptions; } @@ -350,11 +350,11 @@ public static function registerEvents(): void Event::listen(function(ElementTableAttributesResolving $event) use ($elementClasses) { foreach ($elementClasses as $class) { - if (!YiiEvent::hasHandlers($class, self::EVENT_REGISTER_TABLE_ATTRIBUTES)) { + if (!self::hasEventHandlers($class, self::EVENT_REGISTER_TABLE_ATTRIBUTES)) { continue; } - if (!is_subclass_of($class, $event->elementType)) { + if (!self::matchesElementClass($class, $event->elementType)) { continue; } @@ -362,7 +362,7 @@ public static function registerEvents(): void 'tableAttributes' => $event->tableAttributes, ]); - YiiEvent::trigger($class, self::EVENT_REGISTER_TABLE_ATTRIBUTES, $yiiEvent); + self::triggerEvent($class, self::EVENT_REGISTER_TABLE_ATTRIBUTES, $yiiEvent); $event->tableAttributes = $yiiEvent->tableAttributes; } @@ -370,11 +370,11 @@ public static function registerEvents(): void Event::listen(function(ElementDefaultTableAttributesResolving $event) use ($elementClasses) { foreach ($elementClasses as $class) { - if (!YiiEvent::hasHandlers($class, self::EVENT_REGISTER_DEFAULT_TABLE_ATTRIBUTES)) { + if (!self::hasEventHandlers($class, self::EVENT_REGISTER_DEFAULT_TABLE_ATTRIBUTES)) { continue; } - if (!is_subclass_of($class, $event->elementType)) { + if (!self::matchesElementClass($class, $event->elementType)) { continue; } @@ -383,7 +383,7 @@ public static function registerEvents(): void 'tableAttributes' => $event->tableAttributes, ]); - YiiEvent::trigger($class, self::EVENT_REGISTER_DEFAULT_TABLE_ATTRIBUTES, $yiiEvent); + self::triggerEvent($class, self::EVENT_REGISTER_DEFAULT_TABLE_ATTRIBUTES, $yiiEvent); $event->tableAttributes = $yiiEvent->tableAttributes; } @@ -391,11 +391,11 @@ public static function registerEvents(): void Event::listen(function(ElementCardAttributesResolving $event) use ($elementClasses) { foreach ($elementClasses as $class) { - if (!YiiEvent::hasHandlers($class, self::EVENT_REGISTER_CARD_ATTRIBUTES)) { + if (!self::hasEventHandlers($class, self::EVENT_REGISTER_CARD_ATTRIBUTES)) { continue; } - if (!is_subclass_of($class, $event->elementType)) { + if (!self::matchesElementClass($class, $event->elementType)) { continue; } @@ -404,7 +404,7 @@ public static function registerEvents(): void 'fieldLayout' => $event->fieldLayout, ]); - YiiEvent::trigger($class, self::EVENT_REGISTER_CARD_ATTRIBUTES, $yiiEvent); + self::triggerEvent($class, self::EVENT_REGISTER_CARD_ATTRIBUTES, $yiiEvent); $event->cardAttributes = $yiiEvent->cardAttributes; } @@ -412,11 +412,11 @@ public static function registerEvents(): void Event::listen(function(ElementDefaultCardAttributesResolving $event) use ($elementClasses) { foreach ($elementClasses as $class) { - if (!YiiEvent::hasHandlers($class, self::EVENT_REGISTER_DEFAULT_CARD_ATTRIBUTES)) { + if (!self::hasEventHandlers($class, self::EVENT_REGISTER_DEFAULT_CARD_ATTRIBUTES)) { continue; } - if (!is_subclass_of($class, $event->elementType)) { + if (!self::matchesElementClass($class, $event->elementType)) { continue; } @@ -424,7 +424,7 @@ public static function registerEvents(): void 'cardAttributes' => $event->cardAttributes, ]); - YiiEvent::trigger($class, self::EVENT_REGISTER_DEFAULT_CARD_ATTRIBUTES, $yiiEvent); + self::triggerEvent($class, self::EVENT_REGISTER_DEFAULT_CARD_ATTRIBUTES, $yiiEvent); $event->cardAttributes = $yiiEvent->cardAttributes; } @@ -432,11 +432,11 @@ public static function registerEvents(): void Event::listen(function(ElementSearchableAttributesResolving $event) use ($elementClasses) { foreach ($elementClasses as $class) { - if (!YiiEvent::hasHandlers($class, self::EVENT_REGISTER_SEARCHABLE_ATTRIBUTES)) { + if (!self::hasEventHandlers($class, self::EVENT_REGISTER_SEARCHABLE_ATTRIBUTES)) { continue; } - if (!is_subclass_of($class, $event->elementType)) { + if (!self::matchesElementClass($class, $event->elementType)) { continue; } @@ -444,7 +444,7 @@ public static function registerEvents(): void 'attributes' => $event->attributes, ]); - YiiEvent::trigger($class, self::EVENT_REGISTER_SEARCHABLE_ATTRIBUTES, $yiiEvent); + self::triggerEvent($class, self::EVENT_REGISTER_SEARCHABLE_ATTRIBUTES, $yiiEvent); $event->attributes = $yiiEvent->attributes; } @@ -452,11 +452,11 @@ public static function registerEvents(): void Event::listen(function(QueryForTableAttributePreparing $event) use ($elementClasses) { foreach ($elementClasses as $class) { - if (!YiiEvent::hasHandlers($class, self::EVENT_PREP_QUERY_FOR_TABLE_ATTRIBUTE)) { + if (!self::hasEventHandlers($class, self::EVENT_PREP_QUERY_FOR_TABLE_ATTRIBUTE)) { continue; } - if (!is_subclass_of($class, $event->elementType)) { + if (!self::matchesElementClass($class, $event->elementType)) { continue; } @@ -465,7 +465,7 @@ public static function registerEvents(): void 'attribute' => $event->attribute, ]); - YiiEvent::trigger($class, self::EVENT_PREP_QUERY_FOR_TABLE_ATTRIBUTE, $yiiEvent); + self::triggerEvent($class, self::EVENT_PREP_QUERY_FOR_TABLE_ATTRIBUTE, $yiiEvent); if ($yiiEvent->handled) { $event->handled = true; @@ -475,11 +475,11 @@ public static function registerEvents(): void Event::listen(function(ElementEagerLoadingMapResolving $event) use ($elementClasses) { foreach ($elementClasses as $class) { - if (!YiiEvent::hasHandlers($class, self::EVENT_DEFINE_EAGER_LOADING_MAP)) { + if (!self::hasEventHandlers($class, self::EVENT_DEFINE_EAGER_LOADING_MAP)) { continue; } - if (!is_subclass_of($class, $event->elementType)) { + if (!self::matchesElementClass($class, $event->elementType)) { continue; } @@ -488,7 +488,7 @@ public static function registerEvents(): void 'handle' => $event->handle, ]); - YiiEvent::trigger($class, self::EVENT_DEFINE_EAGER_LOADING_MAP, $yiiEvent); + self::triggerEvent($class, self::EVENT_DEFINE_EAGER_LOADING_MAP, $yiiEvent); if ($yiiEvent->elementType !== null) { $event->targetElementType = $yiiEvent->elementType; @@ -500,11 +500,11 @@ public static function registerEvents(): void Event::listen(function(SetEagerLoadedElements $event) use ($elementClasses) { foreach ($elementClasses as $class) { - if (!YiiEvent::hasHandlers($class, self::EVENT_SET_EAGER_LOADED_ELEMENTS)) { + if (!self::hasEventHandlers($class, self::EVENT_SET_EAGER_LOADED_ELEMENTS)) { continue; } - if (!is_subclass_of($class, $event->element::class)) { + if (!self::matchesElementClass($class, $event->element::class)) { continue; } @@ -515,7 +515,7 @@ public static function registerEvents(): void 'plan' => $event->plan, ]); - YiiEvent::trigger($class, self::EVENT_SET_EAGER_LOADED_ELEMENTS, $yiiEvent); + self::triggerEvent($class, self::EVENT_SET_EAGER_LOADED_ELEMENTS, $yiiEvent); if ($yiiEvent->handled) { $event->handled = true; @@ -525,11 +525,11 @@ public static function registerEvents(): void Event::listen(function(ElementLifecycleSaving $event) use ($elementClasses) { foreach ($elementClasses as $class) { - if (!YiiEvent::hasHandlers($class, self::EVENT_BEFORE_SAVE)) { + if (!self::hasEventHandlers($class, self::EVENT_BEFORE_SAVE)) { continue; } - if (!is_subclass_of($class, $event->element::class)) { + if (!self::matchesElementClass($class, $event->element::class)) { continue; } @@ -538,7 +538,7 @@ public static function registerEvents(): void 'isNew' => $event->isNew, ]); - YiiEvent::trigger($class, self::EVENT_BEFORE_SAVE, $yiiEvent); + self::triggerEvent($class, self::EVENT_BEFORE_SAVE, $yiiEvent); if (!$yiiEvent->isValid) { $event->isValid = false; @@ -548,11 +548,11 @@ public static function registerEvents(): void Event::listen(function(ElementLifecycleSaved $event) use ($elementClasses) { foreach ($elementClasses as $class) { - if (!YiiEvent::hasHandlers($class, self::EVENT_AFTER_SAVE)) { + if (!self::hasEventHandlers($class, self::EVENT_AFTER_SAVE)) { continue; } - if (!is_subclass_of($class, $event->element::class)) { + if (!self::matchesElementClass($class, $event->element::class)) { continue; } @@ -561,17 +561,17 @@ public static function registerEvents(): void 'isNew' => $event->isNew, ]); - YiiEvent::trigger($class, self::EVENT_AFTER_SAVE, $yiiEvent); + self::triggerEvent($class, self::EVENT_AFTER_SAVE, $yiiEvent); } }); Event::listen(function(ElementLifecyclePropagated $event) use ($elementClasses) { foreach ($elementClasses as $class) { - if (!YiiEvent::hasHandlers($class, self::EVENT_AFTER_PROPAGATE)) { + if (!self::hasEventHandlers($class, self::EVENT_AFTER_PROPAGATE)) { continue; } - if (!is_subclass_of($class, $event->element::class)) { + if (!self::matchesElementClass($class, $event->element::class)) { continue; } @@ -580,17 +580,17 @@ public static function registerEvents(): void 'isNew' => $event->isNew, ]); - YiiEvent::trigger($class, self::EVENT_AFTER_PROPAGATE, $yiiEvent); + self::triggerEvent($class, self::EVENT_AFTER_PROPAGATE, $yiiEvent); } }); Event::listen(function(ElementLifecycleDeleting $event) use ($elementClasses) { foreach ($elementClasses as $class) { - if (!YiiEvent::hasHandlers($class, self::EVENT_BEFORE_DELETE)) { + if (!self::hasEventHandlers($class, self::EVENT_BEFORE_DELETE)) { continue; } - if (!is_subclass_of($class, $event->element::class)) { + if (!self::matchesElementClass($class, $event->element::class)) { continue; } @@ -598,7 +598,7 @@ public static function registerEvents(): void 'sender' => $event->element, ]); - YiiEvent::trigger($class, self::EVENT_BEFORE_DELETE, $yiiEvent); + self::triggerEvent($class, self::EVENT_BEFORE_DELETE, $yiiEvent); if (!$yiiEvent->isValid) { $event->isValid = false; @@ -608,11 +608,11 @@ public static function registerEvents(): void Event::listen(function(ElementLifecycleDeleted $event) use ($elementClasses) { foreach ($elementClasses as $class) { - if (!YiiEvent::hasHandlers($class, self::EVENT_AFTER_DELETE)) { + if (!self::hasEventHandlers($class, self::EVENT_AFTER_DELETE)) { continue; } - if (!is_subclass_of($class, $event->element::class)) { + if (!self::matchesElementClass($class, $event->element::class)) { continue; } @@ -620,17 +620,17 @@ public static function registerEvents(): void 'sender' => $event->element, ]); - YiiEvent::trigger($class, self::EVENT_AFTER_DELETE, $yiiEvent); + self::triggerEvent($class, self::EVENT_AFTER_DELETE, $yiiEvent); } }); Event::listen(function(ElementLifecycleRestoring $event) use ($elementClasses) { foreach ($elementClasses as $class) { - if (!YiiEvent::hasHandlers($class, self::EVENT_BEFORE_RESTORE)) { + if (!self::hasEventHandlers($class, self::EVENT_BEFORE_RESTORE)) { continue; } - if (!is_subclass_of($class, $event->element::class)) { + if (!self::matchesElementClass($class, $event->element::class)) { continue; } @@ -638,7 +638,7 @@ public static function registerEvents(): void 'sender' => $event->element, ]); - YiiEvent::trigger($class, self::EVENT_BEFORE_RESTORE, $yiiEvent); + self::triggerEvent($class, self::EVENT_BEFORE_RESTORE, $yiiEvent); if (!$yiiEvent->isValid) { $event->isValid = false; @@ -648,11 +648,11 @@ public static function registerEvents(): void Event::listen(function(ElementLifecycleRestored $event) use ($elementClasses) { foreach ($elementClasses as $class) { - if (!YiiEvent::hasHandlers($class, self::EVENT_AFTER_RESTORE)) { + if (!self::hasEventHandlers($class, self::EVENT_AFTER_RESTORE)) { continue; } - if (!is_subclass_of($class, $event->element::class)) { + if (!self::matchesElementClass($class, $event->element::class)) { continue; } @@ -660,17 +660,17 @@ public static function registerEvents(): void 'sender' => $event->element, ]); - YiiEvent::trigger($class, self::EVENT_AFTER_RESTORE, $yiiEvent); + self::triggerEvent($class, self::EVENT_AFTER_RESTORE, $yiiEvent); } }); Event::listen(function(ElementAdditionalButtonsResolving $event) use ($elementClasses) { foreach ($elementClasses as $class) { - if (!YiiEvent::hasHandlers($class, self::EVENT_DEFINE_ADDITIONAL_BUTTONS)) { + if (!self::hasEventHandlers($class, self::EVENT_DEFINE_ADDITIONAL_BUTTONS)) { continue; } - if (!is_subclass_of($class, $event->element::class)) { + if (!self::matchesElementClass($class, $event->element::class)) { continue; } @@ -679,7 +679,7 @@ public static function registerEvents(): void 'html' => $event->html, ]); - YiiEvent::trigger($class, self::EVENT_DEFINE_ADDITIONAL_BUTTONS, $yiiEvent); + self::triggerEvent($class, self::EVENT_DEFINE_ADDITIONAL_BUTTONS, $yiiEvent); $event->html = $yiiEvent->html; } @@ -687,11 +687,11 @@ public static function registerEvents(): void Event::listen(function(ElementAltActionsResolving $event) use ($elementClasses) { foreach ($elementClasses as $class) { - if (!YiiEvent::hasHandlers($class, self::EVENT_DEFINE_ALT_ACTIONS)) { + if (!self::hasEventHandlers($class, self::EVENT_DEFINE_ALT_ACTIONS)) { continue; } - if (!is_subclass_of($class, $event->element::class)) { + if (!self::matchesElementClass($class, $event->element::class)) { continue; } @@ -700,7 +700,7 @@ public static function registerEvents(): void 'altActions' => $event->altActions, ]); - YiiEvent::trigger($class, self::EVENT_DEFINE_ALT_ACTIONS, $yiiEvent); + self::triggerEvent($class, self::EVENT_DEFINE_ALT_ACTIONS, $yiiEvent); $event->altActions = $yiiEvent->altActions; } @@ -708,11 +708,11 @@ public static function registerEvents(): void Event::listen(function(ElementActionMenuItemsResolving $event) use ($elementClasses) { foreach ($elementClasses as $class) { - if (!YiiEvent::hasHandlers($class, self::EVENT_DEFINE_ACTION_MENU_ITEMS)) { + if (!self::hasEventHandlers($class, self::EVENT_DEFINE_ACTION_MENU_ITEMS)) { continue; } - if (!is_subclass_of($class, $event->element::class)) { + if (!self::matchesElementClass($class, $event->element::class)) { continue; } @@ -721,7 +721,7 @@ public static function registerEvents(): void 'items' => $event->items, ]); - YiiEvent::trigger($class, self::EVENT_DEFINE_ACTION_MENU_ITEMS, $yiiEvent); + self::triggerEvent($class, self::EVENT_DEFINE_ACTION_MENU_ITEMS, $yiiEvent); $event->items = $yiiEvent->items; } @@ -729,11 +729,11 @@ public static function registerEvents(): void Event::listen(function(ElementSidebarHtmlResolving $event) use ($elementClasses) { foreach ($elementClasses as $class) { - if (!YiiEvent::hasHandlers($class, self::EVENT_DEFINE_SIDEBAR_HTML)) { + if (!self::hasEventHandlers($class, self::EVENT_DEFINE_SIDEBAR_HTML)) { continue; } - if (!is_subclass_of($class, $event->element::class)) { + if (!self::matchesElementClass($class, $event->element::class)) { continue; } @@ -742,7 +742,7 @@ public static function registerEvents(): void 'html' => $event->html, ]); - YiiEvent::trigger($class, self::EVENT_DEFINE_SIDEBAR_HTML, $yiiEvent); + self::triggerEvent($class, self::EVENT_DEFINE_SIDEBAR_HTML, $yiiEvent); $event->html = $yiiEvent->html; } @@ -750,11 +750,11 @@ public static function registerEvents(): void Event::listen(function(ElementMetaFieldsHtmlResolving $event) use ($elementClasses) { foreach ($elementClasses as $class) { - if (!YiiEvent::hasHandlers($class, self::EVENT_DEFINE_META_FIELDS_HTML)) { + if (!self::hasEventHandlers($class, self::EVENT_DEFINE_META_FIELDS_HTML)) { continue; } - if (!is_subclass_of($class, $event->element::class)) { + if (!self::matchesElementClass($class, $event->element::class)) { continue; } @@ -764,7 +764,7 @@ public static function registerEvents(): void 'html' => $event->html, ]); - YiiEvent::trigger($class, self::EVENT_DEFINE_META_FIELDS_HTML, $yiiEvent); + self::triggerEvent($class, self::EVENT_DEFINE_META_FIELDS_HTML, $yiiEvent); $event->html = $yiiEvent->html; } @@ -772,11 +772,11 @@ public static function registerEvents(): void Event::listen(function(ElementMetadataResolving $event) use ($elementClasses) { foreach ($elementClasses as $class) { - if (!YiiEvent::hasHandlers($class, self::EVENT_DEFINE_METADATA)) { + if (!self::hasEventHandlers($class, self::EVENT_DEFINE_METADATA)) { continue; } - if (!is_subclass_of($class, $event->element::class)) { + if (!self::matchesElementClass($class, $event->element::class)) { continue; } @@ -785,7 +785,7 @@ public static function registerEvents(): void 'metadata' => $event->metadata, ]); - YiiEvent::trigger($class, self::EVENT_DEFINE_METADATA, $yiiEvent); + self::triggerEvent($class, self::EVENT_DEFINE_METADATA, $yiiEvent); $event->metadata = $yiiEvent->metadata; } @@ -793,11 +793,11 @@ public static function registerEvents(): void Event::listen(function(ElementHtmlAttributesResolving $event) use ($elementClasses) { foreach ($elementClasses as $class) { - if (!YiiEvent::hasHandlers($class, self::EVENT_REGISTER_HTML_ATTRIBUTES)) { + if (!self::hasEventHandlers($class, self::EVENT_REGISTER_HTML_ATTRIBUTES)) { continue; } - if (!is_subclass_of($class, $event->element::class)) { + if (!self::matchesElementClass($class, $event->element::class)) { continue; } @@ -806,7 +806,7 @@ public static function registerEvents(): void 'htmlAttributes' => $event->htmlAttributes, ]); - YiiEvent::trigger($class, self::EVENT_REGISTER_HTML_ATTRIBUTES, $yiiEvent); + self::triggerEvent($class, self::EVENT_REGISTER_HTML_ATTRIBUTES, $yiiEvent); $event->htmlAttributes = $yiiEvent->htmlAttributes; } @@ -814,11 +814,11 @@ public static function registerEvents(): void Event::listen(function(ElementAttributeHtmlResolving $event) use ($elementClasses) { foreach ($elementClasses as $class) { - if (!YiiEvent::hasHandlers($class, self::EVENT_DEFINE_ATTRIBUTE_HTML)) { + if (!self::hasEventHandlers($class, self::EVENT_DEFINE_ATTRIBUTE_HTML)) { continue; } - if (!is_subclass_of($class, $event->element::class)) { + if (!self::matchesElementClass($class, $event->element::class)) { continue; } @@ -827,7 +827,7 @@ public static function registerEvents(): void 'attribute' => $event->attribute, ]); - YiiEvent::trigger($class, self::EVENT_DEFINE_ATTRIBUTE_HTML, $yiiEvent); + self::triggerEvent($class, self::EVENT_DEFINE_ATTRIBUTE_HTML, $yiiEvent); if (isset($yiiEvent->html)) { $event->html = $yiiEvent->html; @@ -837,11 +837,11 @@ public static function registerEvents(): void Event::listen(function(ElementInlineAttributeInputHtmlResolving $event) use ($elementClasses) { foreach ($elementClasses as $class) { - if (!YiiEvent::hasHandlers($class, self::EVENT_DEFINE_INLINE_ATTRIBUTE_INPUT_HTML)) { + if (!self::hasEventHandlers($class, self::EVENT_DEFINE_INLINE_ATTRIBUTE_INPUT_HTML)) { continue; } - if (!is_subclass_of($class, $event->element::class)) { + if (!self::matchesElementClass($class, $event->element::class)) { continue; } @@ -850,7 +850,7 @@ public static function registerEvents(): void 'attribute' => $event->attribute, ]); - YiiEvent::trigger($class, self::EVENT_DEFINE_INLINE_ATTRIBUTE_INPUT_HTML, $yiiEvent); + self::triggerEvent($class, self::EVENT_DEFINE_INLINE_ATTRIBUTE_INPUT_HTML, $yiiEvent); if (isset($yiiEvent->html)) { $event->html = $yiiEvent->html; @@ -860,11 +860,11 @@ public static function registerEvents(): void Event::listen(function(SetRoute $event) use ($elementClasses) { foreach ($elementClasses as $class) { - if (!YiiEvent::hasHandlers($class, self::EVENT_SET_ROUTE)) { + if (!self::hasEventHandlers($class, self::EVENT_SET_ROUTE)) { continue; } - if (!is_subclass_of($class, $event->element::class)) { + if (!self::matchesElementClass($class, $event->element::class)) { continue; } @@ -873,7 +873,7 @@ public static function registerEvents(): void 'route' => $event->route, ]); - YiiEvent::trigger($class, self::EVENT_SET_ROUTE, $yiiEvent); + self::triggerEvent($class, self::EVENT_SET_ROUTE, $yiiEvent); $event->route = $yiiEvent->route; if ($yiiEvent->handled) { @@ -884,11 +884,11 @@ public static function registerEvents(): void Event::listen(function(ElementUrlResolving $event) use ($elementClasses) { foreach ($elementClasses as $class) { - if (!YiiEvent::hasHandlers($class, self::EVENT_BEFORE_DEFINE_URL)) { + if (!self::hasEventHandlers($class, self::EVENT_BEFORE_DEFINE_URL)) { continue; } - if (!is_subclass_of($class, $event->element::class)) { + if (!self::matchesElementClass($class, $event->element::class)) { continue; } @@ -897,7 +897,7 @@ public static function registerEvents(): void 'url' => $event->url, ]); - YiiEvent::trigger($class, self::EVENT_BEFORE_DEFINE_URL, $yiiEvent); + self::triggerEvent($class, self::EVENT_BEFORE_DEFINE_URL, $yiiEvent); $event->url = $yiiEvent->url; if ($yiiEvent->handled) { @@ -908,11 +908,11 @@ public static function registerEvents(): void Event::listen(function(ElementUrlResolved $event) use ($elementClasses) { foreach ($elementClasses as $class) { - if (!YiiEvent::hasHandlers($class, self::EVENT_DEFINE_URL)) { + if (!self::hasEventHandlers($class, self::EVENT_DEFINE_URL)) { continue; } - if (!is_subclass_of($class, $event->element::class)) { + if (!self::matchesElementClass($class, $event->element::class)) { continue; } @@ -921,7 +921,7 @@ public static function registerEvents(): void 'url' => $event->url, ]); - YiiEvent::trigger($class, self::EVENT_DEFINE_URL, $yiiEvent); + self::triggerEvent($class, self::EVENT_DEFINE_URL, $yiiEvent); $event->url = $yiiEvent->url; if ($yiiEvent->handled) { @@ -932,11 +932,11 @@ public static function registerEvents(): void Event::listen(function(ElementMovingInStructure $event) use ($elementClasses) { foreach ($elementClasses as $class) { - if (!YiiEvent::hasHandlers($class, self::EVENT_BEFORE_MOVE_IN_STRUCTURE)) { + if (!self::hasEventHandlers($class, self::EVENT_BEFORE_MOVE_IN_STRUCTURE)) { continue; } - if (!is_subclass_of($class, $event->element::class)) { + if (!self::matchesElementClass($class, $event->element::class)) { continue; } @@ -945,7 +945,7 @@ public static function registerEvents(): void 'structureId' => $event->structureId, ]); - YiiEvent::trigger($class, self::EVENT_BEFORE_MOVE_IN_STRUCTURE, $yiiEvent); + self::triggerEvent($class, self::EVENT_BEFORE_MOVE_IN_STRUCTURE, $yiiEvent); if (!$yiiEvent->isValid) { $event->isValid = false; @@ -955,11 +955,11 @@ public static function registerEvents(): void Event::listen(function(ElementMovedInStructure $event) use ($elementClasses) { foreach ($elementClasses as $class) { - if (!YiiEvent::hasHandlers($class, self::EVENT_AFTER_MOVE_IN_STRUCTURE)) { + if (!self::hasEventHandlers($class, self::EVENT_AFTER_MOVE_IN_STRUCTURE)) { continue; } - if (!is_subclass_of($class, $event->element::class)) { + if (!self::matchesElementClass($class, $event->element::class)) { continue; } @@ -968,8 +968,76 @@ public static function registerEvents(): void 'structureId' => $event->structureId, ]); - YiiEvent::trigger($class, self::EVENT_AFTER_MOVE_IN_STRUCTURE, $yiiEvent); + self::triggerEvent($class, self::EVENT_AFTER_MOVE_IN_STRUCTURE, $yiiEvent); } }); } + + private static function hasEventHandlers(string $class, string $name): bool + { + foreach (self::eventTargetClasses($class) as $targetClass) { + if (YiiEvent::hasHandlers($targetClass, $name)) { + return true; + } + } + + return false; + } + + private static function triggerEvent(string $class, string $name, YiiEvent $event): void + { + foreach (self::eventTargetClasses($class) as $targetClass) { + if (!YiiEvent::hasHandlers($targetClass, $name)) { + continue; + } + + YiiEvent::trigger($targetClass, $name, $event); + + if ($event->handled) { + return; + } + } + } + + /** + * @return list + */ + private static function eventTargetClasses(string $class): array + { + $classes = [$class]; + $resolvedClass = self::resolvedClass($class); + + if ($resolvedClass !== $class) { + $classes[] = $resolvedClass; + } + + if ( + $class !== self::class && + !is_subclass_of($class, self::class) && + is_a($resolvedClass, \CraftCms\Cms\Element\Element::class, true) + ) { + $classes[] = self::class; + } + + return array_values(array_unique($classes)); + } + + private static function matchesElementClass(string $class, string $elementClass): bool + { + $class = self::resolvedClass($class); + $elementClass = self::resolvedClass($elementClass); + + return $class === $elementClass || + is_subclass_of($class, $elementClass) || + is_subclass_of($elementClass, $class); + } + + private static function resolvedClass(string $class): string + { + if (!class_exists($class) && !interface_exists($class)) { + return $class; + } + + return (new \ReflectionClass($class))->getName(); + } } diff --git a/yii2-adapter/tests-laravel/Behavior/LegacyBehaviorCompatibilityTest.php b/yii2-adapter/tests-laravel/Behavior/LegacyBehaviorCompatibilityTest.php index 2ab146af00b..eb7ca2148d4 100644 --- a/yii2-adapter/tests-laravel/Behavior/LegacyBehaviorCompatibilityTest.php +++ b/yii2-adapter/tests-laravel/Behavior/LegacyBehaviorCompatibilityTest.php @@ -87,23 +87,21 @@ public function updateOwnerDescription(string $description): void ->toBe([]); }); -test('discovered legacy behavior targets are real wrappers rather than class aliases', function() { - $unexpectedAliasTargets = collect(LegacyBehaviorCatalog::discoveredTargets()) +test('discovered class alias behavior targets resolve to their migrated classes', function() { + $aliasTargets = collect(LegacyBehaviorCatalog::discoveredTargets()) ->filter(function(array $target) { $contents = (string) file_get_contents($target['path']); - if (!str_contains($contents, 'class_alias(')) { - return false; - } - - return !str_contains($target['path'], '/legacy/elements/actions/') - && !str_contains($target['path'], '/legacy/elements/exporters/'); + return str_contains($contents, 'class_alias('); }) - ->pluck('legacyClass') - ->values() - ->all(); + ->values(); - expect($unexpectedAliasTargets)->toBe([]); + expect($aliasTargets)->not->toBeEmpty(); + + $aliasTargets->each(function(array $target) { + expect((new ReflectionClass($target['legacyClass']))->getName()) + ->toBe($target['targetClass']); + }); }); test('component-backed classes inherit behaviors from base model, base component, and concrete legacy classes', function() { From 7e7810570a2b0065d55277b538a9c44f9a3530c6 Mon Sep 17 00:00:00 2001 From: Rias Date: Tue, 5 May 2026 19:49:49 +0200 Subject: [PATCH 6/6] Fix phpstan typehint --- yii2-adapter/legacy/base/Element.php | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/yii2-adapter/legacy/base/Element.php b/yii2-adapter/legacy/base/Element.php index 968fed64e8e..4a5e3accd8c 100644 --- a/yii2-adapter/legacy/base/Element.php +++ b/yii2-adapter/legacy/base/Element.php @@ -86,6 +86,7 @@ use CraftCms\Cms\Element\Events\SetRoute; use CraftCms\Cms\Element\Validation\ElementRules; use Illuminate\Support\Facades\Event; +use ReflectionClass; /** * @since 3.0.0 @@ -984,7 +985,7 @@ private static function hasEventHandlers(string $class, string $name): bool return false; } - private static function triggerEvent(string $class, string $name, YiiEvent $event): void + private static function triggerEvent(string $class, string $name, \yii\base\Event $event): void { foreach (self::eventTargetClasses($class) as $targetClass) { if (!YiiEvent::hasHandlers($targetClass, $name)) { @@ -1038,6 +1039,6 @@ private static function resolvedClass(string $class): string return $class; } - return (new \ReflectionClass($class))->getName(); + return (new ReflectionClass($class))->getName(); } }