You are here

function _workflow_node_initialize_nodes in Workflow 7

Same name and namespace in other branches
  1. 7.2 workflow_admin_ui/workflow_admin_ui.page.type_map.inc \_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

$type - node type:

$name - node type name:

1 call to _workflow_node_initialize_nodes()
workflow_admin_ui_types_save in workflow_admin_ui/workflow_admin_ui.module
Save mapping of workflow to node type. E.g., the story node type is using the Foo workflow.

File

workflow_admin_ui/workflow_admin_ui.module, line 283
Provides administrative UI for workflow. Why it's own module? Lower code footprint and better performance. Additional credit to gcassie ( http://drupal.org/user/80260 ) for the initial push to split UI out of core workflow. We're moving…

Code

function _workflow_node_initialize_nodes($type, $name) {

  // 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', $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.');

  // Get the initial state for this content type.
  $first_state = db_query("SELECT s.sid " . "FROM {workflow_type_map} m " . "INNER JOIN {workflow_states} s ON s.wid = m.wid " . "WHERE m.type = :type AND s.sysid <> :creation " . "ORDER BY s.weight ASC ", array(
    ':type' => $type,
    ':creation' => WORKFLOW_CREATION,
  ))
    ->fetchField(0);

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

    // Force it to transition to the first state and get a history record.
    workflow_execute_transition($node, $first_state, $comment, TRUE);
  }
  return;
  drupal_set_message(t('!count @type nodes have been initialized.', array(
    '@type' => $name,
    '!count' => $how_many,
  )));
}