You are here

public function StateMachine::fire_event in State Machine 7.3

Same name and namespace in other branches
  1. 6 inc/base.inc \StateMachine::fire_event()
  2. 7 inc/base.inc \StateMachine::fire_event()
  3. 7.2 inc/base.inc \StateMachine::fire_event()

Trigger an event to process a transition.

Callbacks and guard conditions will be processed in the following order:

  • event:before_transition
  • event:guard, exits if guard condition return FALSE
  • oldstate:on_exit
  • update the current state
  • newstate:on_enter
  • event:after_transition
1 method overrides StateMachine::fire_event()
StateFlowEntity::fire_event in modules/state_flow_entity/plugins/state_flow_entity.inc
Extending fire_event() from state_machine's base.inc.

File

inc/base.inc, line 189
Defines the base classes of the state machine.

Class

StateMachine
The base class.

Code

public function fire_event($key) {
  $event = $this
    ->get_event($key);
  if ($event && ($new_state = $event
    ->execute())) {

    // Allow the previous state to run its 'on_exit' callbacks.
    $this
      ->get_state($this
      ->get_current_state())
      ->on_exit();

    // Set and save the new state.
    $this
      ->set_current_state($new_state);
    $this
      ->persist();

    // Allow the new state to run its 'on_enter' callbacks.
    $this
      ->get_state($this
      ->get_current_state())
      ->on_enter();

    // Allow the event to "finish"
    $event
      ->finish();
  }
  else {
    $this
      ->on_event_fail($event);
    return FALSE;
  }
}