Actions & Hooks
Actions are buttons in the toolbar. Hooks are lifecycle callbacks that run at specific points in a record's life.
Custom Actions
Add buttons to the toolbar by overriding getActions():
Then implement the action as a public method with the same name:
Warning
The action method must return $this. Use ->notify('message') to queue a toast notification.
Action Options
Example: External Link Action
Lifecycle Hooks
Override these methods in your model to run custom logic at specific lifecycle points.
beforeNew()
Called when a blank record is created for the "New" form. Use it to set default values.
beforeInsert() / afterInsert()
Before/after the first save (insert). Use beforeInsert() for validation:
Tip
Throwing InvalidArgumentException in any before* hook aborts the operation and shows the exception message as an error toast.
beforeUpdate() / afterUpdate()
Before/after saving an existing record:
beforeSave() / afterSave()
Runs on every save, whether insert or update:
afterChangeState(?State $from, State $to)
Runs after a state machine transition:
beforeCheckout() / afterCheckout()
Before/after a record is locked for editing:
beforeCheckin() / afterCheckin()
Before/after a record is unlocked:
beforeTrash() / afterTrash()
Before/after soft-deleting:
beforeDelete() / afterDelete()
Before/after permanent deletion.
beforeRender()
Called before the form is rendered. Use for dynamic field visibility:
Hook Execution Order
For a new record being saved:
beforeNew()→ defaults setfromPost()→ POST data appliedbeforeSave()→ runs firstbeforeInsert()→ validation- Database INSERT
afterInsert()→ ID now availableafterSave()→ runs last- PRG redirect with success toast
For an existing record being saved:
fromPost()→ POST data appliedbeforeSave()beforeUpdate()- Database UPDATE
afterUpdate()afterSave()- PRG redirect
Application-Level Hooks
Define app-wide hooks in App/Hooks.php:
HookContext
The $ctx parameter provides:
| Property/Method | Description |
|---|---|
$ctx->period |
'always', 'day', 'week', 'month', 'year' |
$ctx->scope |
'app', 'group', 'user' |
$ctx->user |
Current User object |
$ctx->userId |
Current user ID |
$ctx->database |
Database instance |
$ctx->flashSuccess(msg) |
Show green toast |
$ctx->flashInfo(msg) |
Show blue toast |
$ctx->flashWarning(msg) |
Show yellow toast |
$ctx->flashError(msg) |
Show red toast |
$ctx->notifyUser(userId, msg, type) |
In-app notification |
$ctx->notifyGroup(groupName, msg, type) |
Notify a group |
$ctx->notifyAll(msg, type) |
Notify everyone |