Actions¶
An action is an operation beyond create, update and delete: publish,
reprice, escalate. It is a class extending VectorAction, listed in its
resource’s actions().
use Kinetis\Vector\Actions\{ActionRequest, ActionResponse, VectorAction};
final class AdjustStock extends VectorAction
{
public function fields(): array
{
return [Number::make('delta', 'Change by')->required()];
}
public function canRun(CurrentUserInterface $user, ?array $record): bool
{
return $record === null || $record['status'] !== 'archived';
}
public function handle(ActionRequest $request): ActionResponse
{
foreach ($request->entities() as $product) {
if (!$product->adjustStock($request->field('delta'))) {
return ActionResponse::danger('Stock cannot go below zero.'); // nothing is saved
}
}
return ActionResponse::message("{$request->count()} adjusted.");
}
}
fields() become the action’s dialog, validated like a form, with
conditions and defaults; an action’s fields are scalar, enum and
belongs-to fields. canRun() decides whether the action is offered
($record is null) and whether it may run on a given record. The
action’s URL segment is name(): by default the class name without
Action, in kebab-case (PublishAction is publish). The constructor
may inject what a controller may.
How it runs¶
For every run, Vector:
checks the resource policy’s
viewAny()andrunAction(), andcanRun()without a record;validates the dialog’s values;
reads each selected record as the user may view it and refuses the run when
canRun()refuses one;loads the entities and hands them to
handle();saves everything in one flush, unless the response is
danger;logs the run in
vector_action_eventsand adds an entry naming the action to each record’s audit trail.
An action runs on one record, on the selected records, or on every record a filter matches (“all matching”), within the resource’s bulk limit.
Shaping the action¶
Method |
Effect |
|---|---|
|
How it is named and shown. |
|
A question asked before it runs. |
|
Shown and confirmed as dangerous. |
|
Runs on no records, from the list’s toolbar. |
|
Where it is offered. |
|
Runs as a background job, in chunks; see Background jobs. |
Responses¶
Response |
The user sees |
|---|---|
|
A confirmation. |
|
An error; nothing is saved ( |
|
The admin navigates to a page. |
|
A URL opens. |
|
A file downloads. |
queued |
A job to follow on Jobs. |
A queued action runs in chunks; Background jobs covers how it is delivered and what it must tolerate.
See also¶
Background jobs — background execution.
Audit trail and impersonation — what an action leaves behind.