Support trait-named lifecycle hooks on pages and importers - #20189
Open
oddvalue wants to merge 1 commit into
Open
Support trait-named lifecycle hooks on pages and importers#20189oddvalue wants to merge 1 commit into
oddvalue wants to merge 1 commit into
Conversation
Lifecycle hooks such as afterCreate() and afterSave() can currently only
be defined directly on the page class. A trait that defines one of these
hooks is silently overridden as soon as the page declares its own,
which makes it impossible for reusable traits or plugins to reliably
hook into the page lifecycle.
callHook() now also invokes {hook}{TraitName}() for every trait used by
the class (recursively), mirroring how Eloquent boots model traits via
boot{TraitName}(). The class's own hook runs first, followed by each
trait hook. Applies to panel pages (BasePage) and importers (Importer).
Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Description
Lifecycle hooks (
beforeFill,afterCreate,afterSave, etc.) can currently only be defined directly on the page class. A trait that defines one of these hooks is silently overridden the moment the page declares its own, which makes it impossible for reusable traits and plugins to reliably hook into the page lifecycle.This PR makes
callHook()also invoke{hook}{TraitName}()for every trait used by the class (recursively), mirroring how Eloquent boots model traits viaboot{TraitName}()and how Livewire supportsmount{TraitName}()/boot{TraitName}()trait hooks:Behaviour
class_uses_recursive()order).callHook()is used: panel pages (BasePage— so allCreateRecord/EditRecord/ViewRecord, tenancy and auth pages) and importers (Importer).Changes
packages/panels/src/Pages/BasePage.php—callHook()calls trait-named hooks after the class hook.packages/actions/src/Imports/Importer.php— same change for importer hooks.docs/03-resources/03-creating-records.md,docs/03-resources/04-editing-records.md— new "Defining lifecycle hooks in traits" sections.TracksLifecycleHookstrait,CreatePostWithTraitHooks,EditPostWithTraitHooks) and cases inCreateRecordTest/EditRecordTestasserting the page hook and the trait hooks all fire, in order.Visual changes
None — no UI changes.
Functional changes
composer cscommand. (Rector and Pint run clean on all touched files; Prettier ignores*.md, and no JS/Blade files are touched.)tests/src/Panels/Resources/Pages/CreateRecordTest.php+EditRecordTest.php: 65 passed, 194 assertions, including the two new cases.tests/src/Actions/ImportActionTest.php+tests/src/Actions/Imports/ImporterTest.php: 35 passed, 54 assertions.)