You are here

public function StateFlowEntity::entityPresave in State Machine 7.3

Called by hook_entity_presave().

Return value

bool TRUE if successful, FALSE otherwise.

See also

state_flow_entity_entity_presave()

File

modules/state_flow_entity/plugins/state_flow_entity.inc, line 466
State Flow implementation of the State Machine class.

Class

StateFlowEntity
@file State Flow implementation of the State Machine class.

Code

public function entityPresave() {
  $entity = $this
    ->get_object();

  // Set the target status if it can be determined.
  if (!empty($entity->event)) {
    $event = $this
      ->get_event($entity->event);
    $state = $event
      ->get_target_state();
    if ($state) {
      $entity->status = $state
        ->is_published() ? NODE_PUBLISHED : NODE_NOT_PUBLISHED;
    }
  }
  elseif ($this
    ->object_is_new()) {

    // If this is a new entity without an event preset the default event. This
    // ensures programmatic saves work.
    $entity->event = $this
      ->get_default_event_name();
  }
  else {

    // @TODO Do we need a special handling in such cases?
    $entity->event = $this
      ->get_default_event_name();
  }
  $event = $entity->event;

  // Check if this is a draft revision.
  $entity->is_draft_revision = $this
    ->isDraftRevision();
  if (($event = $this
    ->get_event($event)) && ($new_state = $event
    ->execute())) {
    $history_entity = $this
      ->get_history_entity();
    $history_entity->event = $event->name;
    $this
      ->set_history_entity($history_entity);

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

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

    // Allow the new state to run its 'on_enter' callbacks.
    $this
      ->get_state($this
      ->get_current_state())
      ->on_enter();
    return TRUE;
  }
  $this
    ->on_event_fail($event);
  return FALSE;
}