You are here

function state_flow_entity_load_state_machine in State Machine 7.3

Load the state_flow state_machine for the given node.

Parameters

object $entity: The entity to handle.

string $entity_type: The machine name of the entity_type.

bool $reset: (legacy) Reset

Return value

StateFlowEntity The state flow entity or FALSE if the entity type isn't enabled.

26 calls to state_flow_entity_load_state_machine()
StateFlowWebTestCase::testStateFlowStateMachine in modules/state_flow/tests/state_flow.test
Test basic state changes and forward revisions.
StateFlowWebTestCaseInstallOnExistingSite::testStateFlowPreexistingNode in modules/state_flow/tests/state_flow.test
Test adding node by the UI doesn't create NULL records.
state_flow_access in modules/state_flow/state_flow.module
Determine whether a user has permission to transition a node with an event.
state_flow_entity_entity_delete in modules/state_flow_entity/state_flow_entity.module
Implements hook_entity_delete().
state_flow_entity_entity_load in modules/state_flow_entity/state_flow_entity.module
Implements hook_entity_load().

... See full list

File

modules/state_flow_entity/state_flow_entity.module, line 163
An implementation of entity workflow for Drupal based on the State Machine system.

Code

function state_flow_entity_load_state_machine($entity, $entity_type = 'node', $reset = FALSE) {

  // If this entity already has a state machine attached re-use it.
  if (!empty($entity->state_flow) && $reset === FALSE) {

    // @todo, review how this is set in the first place and if it is a good idea
    // for entities to carry around their machine objects.
    // Something like this is necessary to update history records twice during
    // the node save process.
    $entity->state_flow
      ->set_object($entity);
    return $entity->state_flow;
  }
  if (!state_flow_entity_entity_type_is_enabled($entity_type)) {
    return FALSE;
  }
  $entity_info = entity_get_info($entity_type);
  $revision_key = $entity_info['entity keys']['revision'];

  // @todo, what's the point of using this objects array if it is not statically
  // cached?
  // And this function would break if there were more than one entity type in
  // play because it is just building up an array keyed by revision_id so node
  // vids and user vids could collide.
  $objects =& drupal_static(__FUNCTION__, array());
  $entity_ids = entity_extract_ids($entity_type, $entity);
  $cid = $entity_type . ':' . implode(':', $entity_ids);

  // New objects have no ids and can't be cached yet.
  $is_new = empty($entity_ids[0]);

  // Check if this object has a cached state machine related to the ids.
  if (!$is_new && isset($objects[$cid])) {
    $objects[$cid]
      ->set_object($entity);
    return $objects[$cid];
  }
  ctools_include('plugins');
  $machine_type = 'state_flow_entity';

  // Allow other modules to invoke other machine types.
  drupal_alter('state_flow_entity_machine_type', $machine_type, $entity, $entity_type);

  // @todo, We should return FALSE if this entity is not state-able.
  // This will likely require cleanup around functions that don't expect this.
  // if ($machine_type === FALSE) {
  //   return FALSE;
  // }
  $plugin = ctools_get_plugins('state_flow_entity', 'plugins', $machine_type);
  if (!empty($plugin) && $revision_key) {
    $class = ctools_plugin_get_class($plugin, 'handler');
    $plugin['object'] = $entity;
    $state_flow_object = new $class($plugin);
    $objects[$cid] = $state_flow_object;
    $entity->state_flow = $objects[$cid];
  }
  return $objects[$cid];
}