workflow_revert.module in Workflow 7
Same filename and directory in other branches
Adds an 'Revert' link to the first workflow history row.
File
workflow_revert/workflow_revert.moduleView source
<?php
/**
* @file
* Adds an 'Revert' link to the first workflow history row.
*/
/**
* Implements hook_permission().
*/
function workflow_revert_permission() {
return array(
'revert workflow' => array(
'title' => t('Revert workflow'),
'description' => t('Allow user to revert workflow state.'),
),
);
}
/**
* Implements hook_menu().
*/
function workflow_revert_menu() {
$items = array();
$items['workflow_revert'] = array(
'title' => 'Workflow Undo',
'access arguments' => array(
'revert workflow',
),
'page callback' => 'drupal_get_form',
'page arguments' => array(
'workflow_revert_form',
),
'type' => MENU_CALLBACK,
);
return $items;
}
/**
* Menu callback to do the revert function.
*/
function workflow_revert_form($form, $form_state, $nid = NULL, $sid = NULL) {
if (drupal_valid_token($_GET['token'], 'workflow_revert ' . $sid)) {
$state = WorkflowState::load($sid);
$entity_type = 'node';
$node = node_load($nid);
$args['#sid'] = $sid;
$args['#nid'] = $nid;
$args['#node'] = $node;
$question = t('Are you sure you want to revert %title to the "@state" state?', array(
'@state' => $state
->label(),
'%title' => $node->title,
));
return confirm_form($args, $question, "node/{$nid}", t('The workflow state will be changed.'));
}
else {
watchdog('workflow_revert', 'Invalid token', array(), WATCHDOG_ERROR);
drupal_set_message(t('Invalid token. Your information has been recorded.'), 'error');
drupal_goto("node/{$nid}");
}
}
function workflow_revert_form_submit($form, $form_state) {
$sid = $form['#sid'];
$nid = $form['#nid'];
$node = $form['#node'];
$comment = t('State reverted.');
// If Rules is available, signal the reversion.
if (module_exists('rules')) {
rules_invoke_event('workflow_state_reverted', $node);
}
// Force the transition because it's probably not valid.
//@todo: use WorkflowTransition->execute() in workflow_revert.
workflow_execute_transition($node, $sid, $comment, TRUE);
drupal_set_message($comment);
drupal_goto('node/' . $form['#nid'] . '/workflow');
}
/**
* Implements hook_rules_event_info().
*/
function workflow_revert_rules_event_info() {
$events = array(
'workflow_state_reverted' => array(
'group' => t('Workflow'),
'label' => t('Workflow state reverted'),
'variables' => rules_events_node_variables(t('updated content'), TRUE),
),
);
return $events;
}
/**
* Implements hook_workflow_history_alter().
* Add an 'undo' operation for the most recent history change.
*
* @param $variables
* The current workflow history information as an array.
* 'old_sid' - The state ID of the previous state.
* 'old_state_name' - The state name of the previous state.
* 'sid' - The state ID of the current state.
* 'state_name' - The state name of the current state.
* 'history' - The row from the workflow_node_history table.
*
* If you want to add additional data, place it in the 'extra' value.
*/
function workflow_revert_workflow_history_alter(&$variables) {
static $first = TRUE;
// Only mark the first row.
if ($first) {
$first = FALSE;
// Let's ask other modules if the reversion is allowed.
$node = node_load($variables['history']->nid);
$result = module_invoke_all('workflow', 'transition permitted', $variables['sid'], $variables['old_sid'], $node);
// Did anybody veto this choice?
if (!in_array(FALSE, $result)) {
// If not vetoed, mark it.
$options = array(
'query' => array(
'token' => drupal_get_token('workflow_revert ' . $variables['old_sid']),
),
);
$path = 'workflow_revert/' . $variables['history']->nid . '/' . $variables['old_sid'];
$variables['extra'] = l('Revert state change', $path, $options);
}
}
}
Functions
Name | Description |
---|---|
workflow_revert_form | Menu callback to do the revert function. |
workflow_revert_form_submit | |
workflow_revert_menu | Implements hook_menu(). |
workflow_revert_permission | Implements hook_permission(). |
workflow_revert_rules_event_info | Implements hook_rules_event_info(). |
workflow_revert_workflow_history_alter | Implements hook_workflow_history_alter(). Add an 'undo' operation for the most recent history change. |