You are here

workflow_vbo.module in Workflow 7

Same filename and directory in other branches
  1. 7.2 workflow_vbo/workflow_vbo.module

Provide workflow actions for VBO. Split out from workflow_actions.

File

workflow_vbo/workflow_vbo.module
View source
<?php

/**
 * @file
 * Provide workflow actions for VBO.
 * Split out from workflow_actions.
 */

/**
 * Implements hook_action_info().
 */
function workflow_vbo_action_info() {
  return array(
    'workflow_vbo_next_state_action' => array(
      'type' => 'node',
      'label' => t('Change workflow state of post to next state'),
      'configurable' => FALSE,
      'triggers' => array(
        'any',
      ),
    ),
    'workflow_vbo_given_state_action' => array(
      'type' => 'node',
      'label' => t('Change workflow state of post to new state'),
      'configurable' => TRUE,
      'triggers' => array(
        'any',
      ),
    ),
  );
}

/**
 * Implements a Drupal action. Move a node to the next state in the workflow.
 */
function workflow_vbo_next_state_action($node, array $context) {
  global $user;

  // If this action is being fired because it's attached to a workflow transition
  // then the node's new state (now its current state) should be in $node->workflow
  // because that is where the value from the workflow form field is stored;
  // otherwise the current state is placed in $node->workflow by our nodeapi load.
  if (!isset($node->nid)) {
    watchdog('workflow_vbo', 'Unable to get current node id state of node - node is not yet saved.');
    return;
  }
  if (!isset($node->workflow)) {
    watchdog('workflow_vbo', 'Unable to get current workflow state of node %nid.', array(
      '%nid' => $node->nid,
    ));
    return;
  }
  $entity_type = 'node';
  $entity = $node;
  $current_sid = $node->workflow;
  $current_state = WorkflowState::load($current_sid);

  // Get the node's new state.
  $new_sid = $current_sid;
  $options = $current_state
    ->getOptions($entity_type, $entity);
  foreach ($options as $sid => $name) {
    if (isset($flag)) {
      $new_sid = $sid;
      $new_state_name = $name;
      break;
    }
    if ($sid == $current_sid) {
      $flag = TRUE;
    }
  }

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

/**
 * Implements a Drupal action. Move a node to a specified state.
 */
function workflow_vbo_given_state_action($node, array $context) {
  global $user;
  if (!isset($node->nid)) {
    watchdog('workflow_vbo', 'Unable to get current node id state of node - node is not yet saved.');
    return;
  }
  $comment = t($context['workflow_comment'], array(
    '%title' => check_plain($node->title),
    '%state' => check_plain($context['state_name']),
    '%user' => theme('username', array(
      'account' => $user,
    )),
  ));
  $entity_type = 'node';
  $entity = $node;
  $current_sid = $node->workflow;

  // Fire the transition.
  $transition = new WorkflowTransition($entity_type, $entity, $field_name = '', $current_sid, $new_sid = $context['target_sid'], $user->uid, REQUEST_TIME, $comment);
  $new_sid = $transition
    ->execute($force = $context['force']);
}

/**
 * Configuration form for "Change workflow state of post to new state" action.
 *
 * @see workflow_vbo_given_state_action()
 */
function workflow_vbo_given_state_action_form(array $context) {
  $previous_workflow = '';
  $options = array();

  // @todo: you can choose a state from an illegal Workflow.
  // Get all states, only where active.
  foreach (Workflow::getWorkflows() as $workflow) {
    $options += $workflow
      ->getOptions($grouped = TRUE);
  }
  $form['workflow_options'] = array(
    '#type' => 'select',
    '#title' => t('Target state'),
    '#description' => t('Please select the state that should be assigned when this action runs.'),
    '#default_value' => isset($context['target_sid']) ? $context['target_sid'] : '',
    '#options' => $options,
  );
  $form['workflow_force'] = array(
    '#type' => 'checkbox',
    '#title' => t('Force transition'),
    '#description' => t('If this box is checked, the new state will be assigned even if workflow ' . 'permissions disallow it.'),
    '#default_value' => isset($context['force']) ? $context['force'] : '',
  );
  $form['workflow_comment'] = array(
    '#type' => 'textfield',
    '#title' => t('Message'),
    '#description' => t('This message will be written into the workflow history log when the action ' . 'runs. You may include the following variables: %state, %title, %user'),
    '#default_value' => isset($context['workflow_history']) ? $context['workflow_history'] : t('Action set %title to %state by %user.'),
  );
  return $form;
}

/**
 * Submit handler for "Change workflow state of post to new state" action
 * configuration form.
 *
 * @see workflow_vbo_given_state_action_form()
 */
function workflow_vbo_given_state_action_submit($form_id, $form_state) {
  $new_sid = $form_state['values']['workflow_options'];
  if ($state = WorkflowState::load($new_sid)) {
    return array(
      'target_sid' => $state->sid,
      'state_name' => check_plain($state
        ->getName()),
      'force' => $form_state['values']['workflow_force'],
      'workflow_comment' => $form_state['values']['workflow_comment'],
    );
  }
}

Functions

Namesort descending Description
workflow_vbo_action_info Implements hook_action_info().
workflow_vbo_given_state_action Implements a Drupal action. Move a node to a specified state.
workflow_vbo_given_state_action_form Configuration form for "Change workflow state of post to new state" action.
workflow_vbo_given_state_action_submit Submit handler for "Change workflow state of post to new state" action configuration form.
workflow_vbo_next_state_action Implements a Drupal action. Move a node to the next state in the workflow.