You are here

function _workflow_node_initialize_nodes in Workflow 7.2

Same name and namespace in other branches
  1. 7 workflow_admin_ui/workflow_admin_ui.module \_workflow_node_initialize_nodes()

Initialize all pre-existing nodes of a type to their first state.

@todo: adjust _workflow_node_initialize_nodes() to handle Workflow Field.

Parameters

string $node_type: The node type.

1 call to _workflow_node_initialize_nodes()
workflow_admin_ui_type_map_form_submit in workflow_admin_ui/workflow_admin_ui.page.type_map.inc
Submit handler for workflow type mapping form.

File

workflow_admin_ui/workflow_admin_ui.page.type_map.inc, line 200
Provides the type_map maintenance form.

Code

function _workflow_node_initialize_nodes($entity_type, $node_type, $field_name, $wid) {
  global $user;

  // Build the select query.
  // We want all published nodes of this type that don't already have a workflow state.
  $query = db_select('node', 'n');
  $query
    ->leftJoin('workflow_node', 'wn', 'wn.nid = n.nid');

  // Add the fields.
  $query
    ->addField('n', 'nid');

  // Add conditions.
  $query
    ->condition('n.type', $node_type);
  $query
    ->condition('n.status', 1);
  $query
    ->isNull('wn.sid');
  $nids = $query
    ->execute()
    ->fetchCol();
  $how_many = count($nids);
  if ($how_many == 0) {
    return;
  }
  $comment = t('Pre-existing content set to initial state.');
  $workflow = workflow_load_single($wid);
  $force = TRUE;
  $creation_sid = $workflow
    ->getCreationSid();

  // Load them all up.
  $nodes = node_load_multiple($nids);
  foreach ($nodes as $entity) {

    // Get the initial state for this entity.
    // Due to permissions, it might be different for each user.
    $new_sid = $workflow
      ->getFirstSid($entity_type, $entity, $field_name, $user, $force);
    $transition = new WorkflowTransition();
    $transition
      ->setValues($entity_type, $entity, $field_name, $creation_sid, $new_sid, $user->uid, REQUEST_TIME, $comment);

    // Force it to transition to the first state and get a history record.
    workflow_execute_transition($entity_type, $entity, $field_name, $transition, $force);
  }
  drupal_set_message(t('!count @type nodes have been initialized.', array(
    '@type' => node_type_get_name($node_type),
    '!count' => $how_many,
  )));
}