You are here

function workbench_moderation_node_update in Workbench Moderation 7

Same name and namespace in other branches
  1. 7.3 workbench_moderation.module \workbench_moderation_node_update()

Implements hook_node_update().

Handles the submit of the node form moderation information

1 call to workbench_moderation_node_update()
workbench_moderation_node_insert in ./workbench_moderation.module
Implements hook_node_insert().

File

./workbench_moderation.module, line 701
Content moderation for Workbench.

Code

function workbench_moderation_node_update($node) {
  global $user;

  // Don't proceed if moderation is not enabled on this content, or if
  // we're replacing an already-published revision.
  if (!workbench_moderation_node_moderated($node) || !empty($node->workbench_moderation['updating_live_revision'])) {
    return;
  }

  // Set moderation state values.
  if (!isset($node->workbench_moderation_state_current)) {
    $node->workbench_moderation_state_current = !empty($node->original->workbench_moderation['current']->state) ? $node->original->workbench_moderation['current']->state : workbench_moderation_state_none();
  }
  if (!isset($node->workbench_moderation_state_new)) {

    // Moving from published to unpublished.
    if ($node->status == NODE_NOT_PUBLISHED && isset($node->original->status) && $node->original->status == NODE_PUBLISHED) {

      // @todo Currently we cannot set the state correctly if the default state
      //   is "Published".
      // @see https://www.drupal.org/node/1436260
      $node->workbench_moderation_state_new = variable_get('workbench_moderation_default_state_' . $node->type, workbench_moderation_state_none());
    }
    elseif ($node->status == NODE_PUBLISHED && isset($node->original->status) && $node->original->status == NODE_NOT_PUBLISHED) {
      $node->workbench_moderation_state_new = workbench_moderation_state_published();
    }
    else {
      if (!empty($node->original->workbench_moderation['current']->state)) {
        $node->workbench_moderation_state_new = $node->original->workbench_moderation['current']->state;
      }
      else {
        $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'.
  if (!isset($node->workbench_moderation)) {
    $node->workbench_moderation = array();
    $node->workbench_moderation['my_revision'] = $node->workbench_moderation['current'] = (object) array(
      'from_state' => workbench_moderation_state_none(),
      'state' => workbench_moderation_state_none(),
      'nid' => $node->nid,
      'vid' => $node->vid,
      'uid' => $user->uid,
      'is_current' => TRUE,
      'published' => FALSE,
      'stamp' => $node->changed,
    );
  }

  // Apply moderation changes if this is a new revision or if the moderation
  // state has changed.
  if (!empty($node->revision) || $node->workbench_moderation_state_current != $node->workbench_moderation_state_new) {

    // Update attached fields.
    field_attach_update('node', $node);

    // Moderate the node.
    workbench_moderation_moderate($node, $node->workbench_moderation_state_new);
  }
  return;
}