Commit b12a9477 authored by Bec's avatar Bec
Browse files

Issue #1185678: allow transitions away from the 'published' state. Run...

Issue #1185678: allow transitions away from the 'published' state. Run update.php after pulling this change.
parent 2ca211c0
Loading
Loading
Loading
Loading
+1 −1
Changes for tests/workbench_moderation.test: 1 added line, 1 removed line.
Original line number Diff line number Diff line
@@ -266,7 +266,7 @@ class WorkbenchModerationUnpublishTestCase extends WorkbenchModerationTestCase {
    $node = $this->drupalGetNodeByTitle($edit['title']);

    // Unpublish the node via the unpublish confirmation form.
    $this->drupalPost("node/{$node->nid}/moderation/{$node->vid}/unpublish", array(), t('Confirm'));
    $this->drupalPost("node/{$node->nid}/moderation/{$node->vid}/unpublish", array(), t('Unpublish'));

    $unpublished_node = node_load($node->nid, FALSE, TRUE);
    $this->assertFalse($unpublished_node->status, t('The node is not published.'));
+0 −2
Changes for workbench_moderation.admin.inc: 0 added lines, 2 removed lines.
Original line number Diff line number Diff line
@@ -206,7 +206,6 @@ function workbench_moderation_admin_transitions_form($form, &$form_state) {
    '#title' => t('New transition'),
    '#options' => $options,
  );
  unset($element['from_name']['#options'][workbench_moderation_state_published()]);
  $element['to_name'] = array(
    '#type' => 'select',
    '#title' => ' ',
@@ -472,7 +471,6 @@ function workbench_moderation_recommended_permissions($types = array()) {
      "view moderation messages",
      "view moderation history",
      "use workbench_moderation needs review tab",
      "unpublish live revision",
    ),
  );

+40 −0
Changes for workbench_moderation.install: 40 added lines, 0 removed lines.
Original line number Diff line number Diff line
@@ -529,3 +529,43 @@ function workbench_moderation_update_7006() {

  return t("Added column to store moderation state labels.");
}

/**
 * Replace the "Unpublish the current live revision" permission with a transition.
 */
function workbench_moderation_update_7007() {
  // Normally we would use workbench_moderation_states(), but we can't guarantee that workbench_moderation is enabled.
  $states = db_select('workbench_moderation_states', 'states')
    ->fields('states', array('name', 'label', 'description', 'weight'))
    ->orderBy('states.weight', 'ASC')
    ->execute()
    ->fetchAllAssoc('name');

  // Add an unpublish transition from published to draft. The 'published' state
  // currently can't be deleted, but the draft state may or may not be present.
  $transition = (object) array(
    'from_name' => 'published',
    'to_name' => (isset($states['draft']) ? 'draft' : key($states)),
  );
  // Normally we would use workbench_moderation_transition_save(), but we can't guarantee that workbench_moderation is enabled.
  db_merge('workbench_moderation_transitions')
    ->key(array('from_name' => $transition->from_name, 'to_name' => $transition->to_name))
    ->fields((array) $transition)
    ->execute();

  // Change permission grants from the deprecated unpublish permission to the
  // new transition permission.
  $roles = user_roles(FALSE, 'unpublish live revision');
  $permissions = array(
    'unpublish live revision' => FALSE,
    "moderate content from {$transition->from_name} to {$transition->to_name}" => TRUE,
  );
  foreach ($roles as $rid => $role) {
    user_role_change_permissions($rid, $permissions);
  }

  return t("Added a new transition from %from to %to and updated unpublish permissions. Unpublishing content is now controlled by transition permissions, and roles with the 'Bypass moderation restrictions' permission may unpublish content.", array(
    '%from' => $transition->from_name,
    '%to' => $transition->to_name,
  ));
}
+20 −8
Changes for workbench_moderation.module: 20 added lines, 8 removed lines.
Original line number Diff line number Diff line
@@ -329,9 +329,6 @@ function workbench_moderation_permission() {
  $permissions['view moderation messages'] = array(
    'title' => t('View the moderation messages on a node')
  );
  $permissions['unpublish live revision'] = array(
    'title' => t('Unpublish the current live revision'),
  );
  $permissions['use workbench_moderation my drafts tab'] = array(
    'title' => t('Use "My Drafts" workbench tab')
  );
@@ -390,6 +387,8 @@ function workbench_moderation_node_access($node, $op, $account) {
 *   Boolean TRUE or FALSE.
 */
function _workbench_moderation_access($op, $node) {
  global $user;

  // If we do not control this node type, deny access.
  if (workbench_moderation_node_type_moderated($node->type) === FALSE) {
    return FALSE;
@@ -404,7 +403,9 @@ function _workbench_moderation_access($op, $node) {
  $access &= node_access('update', $node);

  if ($op == 'unpublish') {
    $access &= (user_access('unpublish live revision') || user_access('bypass workbench moderation'));
    // workbench_moderation_states_next() checks transition permissions.
    $next_states = workbench_moderation_states_next(workbench_moderation_state_published(), $user, $node->type);
    $access &= !empty($next_states);
  }

  // Allow other modules to change our rule set.
@@ -620,7 +621,7 @@ function workbench_moderation_node_update($node) {
    $node->workbench_moderation_state_current = ($node->status ? workbench_moderation_state_published() : workbench_moderation_state_none());
  };
  if (!isset($node->workbench_moderation_state_new)) {
    $node->workbench_moderation_state_new = variable_get('workbench_moderation_default_state_' . $node->type);
    $node->workbench_moderation_state_new = variable_get('workbench_moderation_default_state_' . $node->type, workbench_moderation_state_none());
  };

  // If this is a new node, give it some information about 'my revision'.
@@ -749,7 +750,7 @@ function workbench_moderation_form_node_form_alter(&$form, $form_state) {
    $moderation_state = $form['#node']->workbench_moderation['current']->state;
  }
  else {
    $moderation_state = variable_get('workbench_moderation_default_state_' . $form['type']['#value']);
    $moderation_state = variable_get('workbench_moderation_default_state_' . $form['type']['#value'], workbench_moderation_state_none());
  }

  // Store the current moderation state
@@ -1588,7 +1589,7 @@ function workbench_moderation_moderate_form($form, &$form_state, $node, $destina
    $form['state'] = array(
      '#type' => 'select',
      '#options' => $next_states,
      '#default_value' => $node->workbench_moderation['current']->state,
      '#default_value' => _workbench_moderation_default_next_state($my_revision->state, $next_states),
    );
    $form['submit'] = array(
      '#type' => 'submit',
@@ -1607,6 +1608,15 @@ function workbench_moderation_moderate_form($form, &$form_state, $node, $destina
  return $form;
}

function _workbench_moderation_default_next_state($current_state, $next_states) {
  $states = workbench_moderation_states();
  foreach ($states as $state_name => $state) {
    if ($state->weight > $states[$current_state]->weight && isset($next_states[$state_name])) {
      return $state_name;
    }
  }
}

function workbench_moderation_moderate_form_validate($form, &$form_state) {
  // Make sure that the revision that was shown to the user is still the current
  // revision before changing the current revision's state.
@@ -1654,6 +1664,7 @@ function workbench_moderation_moderate_form_submit($form, $form_state) {
 *   this variable was not set.
 */
function workbench_moderation_messages($context, $node = NULL) {
  global $user;
  if (!user_access('view moderation messages')
      || (!$node && !($node = menu_get_object()))
      || !workbench_moderation_node_type_moderated($node->type)) {
@@ -1705,7 +1716,8 @@ function workbench_moderation_messages($context, $node = NULL) {
    }

    // Add an unpublish link.
    if ($revision_published && user_access('unpublish live revision') && $link = workbench_moderation_access_link(t('Unpublish this revision'), "node/{$node->nid}/moderation/{$node->vid}/unpublish")) {
    $next_states = workbench_moderation_states_next(workbench_moderation_state_published(), $user, $node->type);
    if ($revision_published && !empty($next_states) && $link = workbench_moderation_access_link(t('Unpublish this revision'), "node/{$node->nid}/moderation/{$node->vid}/unpublish")) {
      $info_block_messages[] = array(
        'label' => t('Actions'),
        'message' => $link,
+25 −4
Changes for workbench_moderation.node.inc: 25 added lines, 4 removed lines.
Original line number Diff line number Diff line
@@ -139,7 +139,8 @@ function workbench_moderation_node_history_view($node) {
    if (!empty($node->workbench_moderation['published']) && $revision->vid == $node->workbench_moderation['published']->vid) {
      $row['data']['moderation'] = '<div class="moderation-state"><strong>' . t('This is the published revision.') . '</strong></div>';
      // Provide an unpublish link.
      if (user_access('unpublish live revision')) {
      $next_states = workbench_moderation_states_next(workbench_moderation_state_published(), $user, $node->type);
      if (!empty($next_states)) {
        $row['data']['moderation'] .= '<div class="moderation-actions">' . l(t('Unpublish'), "node/{$revision->nid}/moderation/{$revision->live_revision}/unpublish") . '</div>';
      }
    }
@@ -206,6 +207,8 @@ function workbench_moderation_node_history_view($node) {
 *   A Drupal confirmation form to unpublish the live revision.
 */
function workbench_moderation_node_unpublish_form($form, &$form_state, $node) {
  global $user;

  // Ensure that this is the live revision.
  if (isset($node->workbench_moderation['published']->vid) && $node->vid != $node->workbench_moderation['published']->vid) {
    drupal_set_message(t('This is not the live revision of this node.'), 'error');
@@ -218,9 +221,27 @@ function workbench_moderation_node_unpublish_form($form, &$form_state, $node) {
    '#type' => 'value',
  );
  $form['message'] = array(
    '#markup' =>  '<p>' . t('Are you sure that you want to unpublish the live revision of this content? The current live revision will be set to moderation state %state.', array('%state' => workbench_moderation_state_label(workbench_moderation_state_none()))) . '</p>',
    '#markup' =>  '<p>' . t('Are you sure that you want to unpublish the live revision of this content?') . '</p>',
  );

  $current_state = $node->workbench_moderation['my_revision']->state;
  if ($next_states = workbench_moderation_states_next($current_state, $user, $node->type)) {
    $form['state'] = array(
      '#title' => t('Set moderation state:'),
      '#type' => 'select',
      '#options' => $next_states,
      '#default_value' => _workbench_moderation_default_next_state($current_state, $next_states),
    );
  }
  else {
    $form['message']['#markup'] .= '<p>' . t('The current live revision will be set to moderation state %state.', array('%state' => workbench_moderation_state_label(workbench_moderation_state_none()))) . '</p>';
    $form['state'] = array(
      '#type' => 'value',
      '#value' => workbench_moderation_state_none(),
    );
  return confirm_form($form, t('Unpublish %title?', array('%title' => $node->title)), "node/{$node->nid}/moderation");
  }

  return confirm_form($form, t('Unpublish %title?', array('%title' => $node->title)), "node/{$node->nid}/moderation", '', t('Unpublish'));
}

/**
@@ -237,7 +258,7 @@ function workbench_moderation_node_unpublish_form_submit($form, &$form_state) {
    ->execute();

  // Moderate the revision.
  workbench_moderation_moderate($node, workbench_moderation_state_none());
  workbench_moderation_moderate($node, $form_state['values']['state']);

  // Make sure the 'current' revision is the 'live' revision -- ie, the revision
  // in {node}.