WorkflowTriggerBehavior
extends Behavior
in package
WorkflowTrigger Behavior
Auto-dispatches workflow triggers on entity lifecycle events (afterSave, afterDelete). Attach to any Table class to fire workflow triggers without manual controller code.
Tags
Table of Contents
Properties
- $suppressTriggers : bool
- When true, all trigger dispatching is suppressed.
- $_defaultConfig : array<string, mixed>
- Default configuration.
Methods
- afterDelete() : void
- afterDelete callback — dispatches triggers for delete events.
- afterSave() : void
- afterSave callback — dispatches triggers for create and update events.
- buildContext() : array<string|int, mixed>
- Build the trigger context from entity data.
- dispatchTrigger() : void
- Dispatch a trigger event through CakePHP's EventManager.
- extractChanges() : array<string, array{old: mixed, new: mixed}>
- Extract changed fields with old and new values.
- extractEntityData() : array<string|int, mixed>
- Extract entity data, optionally filtered by contextFields config.
- getCurrentUserId() : int|null
- Get the current authenticated user ID from the request, if available.
- normalizeTriggerConfig() : array<string|int, array{trigger: string, onlyIfChanged?: array}>
- Normalize trigger config to a consistent array-of-arrays format.
- shouldFireTrigger() : bool
- Check whether the trigger should fire based on field-change conditions.
Properties
$suppressTriggers
When true, all trigger dispatching is suppressed.
public
static bool
$suppressTriggers
= false
Set this during workflow engine execution or migrations to prevent infinite loops.
$_defaultConfig
Default configuration.
protected
array<string, mixed>
$_defaultConfig
= ['triggers' => [], 'contextFields' => null, 'contextAliases' => [], 'eventDataKey' => 'trigger', 'includeChangedFields' => true, 'fieldConditions' => []]
Methods
afterDelete()
afterDelete callback — dispatches triggers for delete events.
public
afterDelete(EventInterface $event, EntityInterface $entity, ArrayObject $options) : void
Parameters
- $event : EventInterface
-
The afterDelete event
- $entity : EntityInterface
-
The deleted entity
- $options : ArrayObject
-
Delete options
afterSave()
afterSave callback — dispatches triggers for create and update events.
public
afterSave(EventInterface $event, EntityInterface $entity, ArrayObject $options) : void
Parameters
- $event : EventInterface
-
The afterSave event
- $entity : EntityInterface
-
The saved entity
- $options : ArrayObject
-
Save options
buildContext()
Build the trigger context from entity data.
protected
buildContext(EntityInterface $entity, string $eventType) : array<string|int, mixed>
Parameters
- $entity : EntityInterface
-
The entity
- $eventType : string
-
One of 'create', 'update', 'delete'
Return values
array<string|int, mixed> —The context array
dispatchTrigger()
Dispatch a trigger event through CakePHP's EventManager.
protected
dispatchTrigger(string $triggerName, array<string|int, mixed> $context) : void
Uses the 'Workflow.trigger' event so TriggerDispatcher can pick it up without requiring a direct dependency on the engine.
Parameters
- $triggerName : string
-
The workflow trigger name
- $context : array<string|int, mixed>
-
The trigger context data
extractChanges()
Extract changed fields with old and new values.
protected
extractChanges(EntityInterface $entity) : array<string, array{old: mixed, new: mixed}>
Parameters
- $entity : EntityInterface
-
The entity
Return values
array<string, array{old: mixed, new: mixed}> —Changed fields
extractEntityData()
Extract entity data, optionally filtered by contextFields config.
protected
extractEntityData(EntityInterface $entity) : array<string|int, mixed>
Parameters
- $entity : EntityInterface
-
The entity
Return values
array<string|int, mixed> —Filtered entity data
getCurrentUserId()
Get the current authenticated user ID from the request, if available.
protected
getCurrentUserId() : int|null
Return values
int|null —User ID or null when not in an HTTP context
normalizeTriggerConfig()
Normalize trigger config to a consistent array-of-arrays format.
protected
normalizeTriggerConfig(array<string|int, mixed>|string $config) : array<string|int, array{trigger: string, onlyIfChanged?: array}>
Accepts a string trigger name, a single config array, or an array of configs.
Parameters
- $config : array<string|int, mixed>|string
-
Raw trigger configuration
Return values
array<string|int, array{trigger: string, onlyIfChanged?: array}> —Normalized configs
shouldFireTrigger()
Check whether the trigger should fire based on field-change conditions.
protected
shouldFireTrigger(array<string|int, mixed> $config, EntityInterface $entity) : bool
Parameters
- $config : array<string|int, mixed>
-
Trigger config with optional 'onlyIfChanged' key
- $entity : EntityInterface
-
The entity being checked
Return values
bool —True if the trigger should fire