You are here

function workflow_vbo_given_state_action in Workflow 7.2

Same name and namespace in other branches
  1. 7 workflow_vbo/workflow_vbo.module \workflow_vbo_given_state_action()

Implements a Drupal action. Move a node to a specified state in the workflow.

Parameters

$entity:

array $context:

File

workflow_vbo/actions/given.action.inc, line 31
VBO action to modify entity values (properties and fields).

Code

function workflow_vbo_given_state_action($entity, array $context) {
  global $user;

  // As advanced action with Trigger 'node':
  // - $entity is empty;
  // - $context['group'] = 'node'
  // - $context['hook'] = 'node_insert / _update / _delete'
  // - $context['node'] = (Object) stdClass
  // - $context['entity_type'] = NULL
  // As advanced action with Trigger 'taxonomy':
  // - $entity is (Object) stdClass;
  // - $context['type'] = 'entity'
  // - $context['group'] = 'taxonomy'
  // - $context['hook'] = 'taxonomy_term_insert / _update / _delete'
  // - $context['node'] = (Object) stdClass
  // - $context['entity_type'] = NULL
  // As advanced action with Trigger 'workflow API':
  // ...
  // As VBO action:
  // - $entity is (Object) stdClass;
  // - $context['type'] = NULL
  // - $context['group'] = NULL
  // - $context['hook'] = NULL
  // - $context['node'] = (Object) stdClass
  // - $context['entity_type'] = 'node'
  // Get the entity type, entity and entity ID.
  if (isset($context['entity_type'])) {

    // In a VBO Action.
    $entity_type = $context['entity_type'];
  }
  else {

    // In an Advanced Action.
    $entity_type = str_replace(array(
      '_insert',
      '_update',
      '_delete',
    ), '', $context['hook']);
  }

  // Change the state of latest revision, not current revision.
  if (isset($context[$entity_type])) {
    $entity = $context[$entity_type];
  }
  elseif (!isset($entity)) {
    $entity = $context['node'];
  }

  // In 'after saving new content', the node is already saved. Avoid second insert.
  // Todo: clone?
  unset($entity->is_new);
  list($entity_id, , $entity_bundle) = entity_extract_ids($entity_type, $entity);
  if (!$entity_id) {
    watchdog('workflow_vbo', 'Unable to get current entity ID - entity is not yet saved.');
    return;
  }

  // Get data from the context.
  $form = $context['form'];
  $form_state = $context['form_state'];

  // Get the current State Id. Also, $field_name will be set magically, by reference.
  $field_name = NULL;
  $current_sid = workflow_node_current_state($entity, $entity_type, $field_name);
  if (!$current_sid) {
    watchdog('workflow_vbo', 'Unable to get current workflow state of entity %id.', array(
      '%id' => $entity_id,
    ));
    return;
  }

  // Get the new State Id.
  $new_sid = $form_state['input']['workflow_sid'];

  // The following 2 lines should give the same result.
  // $force = $form_state['input']['workflow_force'];
  $force = $context['force'];

  // Get the Comment. Parse the $comment variables.
  $comment_string = $form_state['input']['workflow_comment'];
  $comment = t($comment_string, array(
    '%title' => entity_label($entity_type, $entity),
    // "@" and "%" will automatically run check_plain().
    '%state' => workflow_get_sid_label($new_sid),
    '%user' => $user->name,
  ));

  // Fire the transition.
  $transition = new WorkflowTransition();
  $transition
    ->setValues($entity_type, $entity, $field_name, $current_sid, $new_sid, $user->uid, REQUEST_TIME, $comment);
  workflow_execute_transition($entity_type, $entity, $field_name, $transition, $force);
}