You are here

function content_field_batch_update in Content Construction Kit (CCK) 6.2

Same name and namespace in other branches
  1. 6.3 includes/content.admin.inc \content_field_batch_update()
  2. 6 includes/content.admin.inc \content_field_batch_update()

Content Field Batch Update Operation

Find all nodes that contain a field and update their values.

Parameters

$updates: an array like: 'field_name' => array( 'mask' => array() // Keyed array of column names and replacement values for use // below delta, or for all values if no delta is supplied. 'alt_mask' => array() // Optional, keyed array of column names and replacement values for use // at or above delta, if a delta is supplied. 'delta' => # // Optional, the number to use as the delta value where you switch from // one mask to the other. ),

1 string reference to 'content_field_batch_update'
content_alter_fields in includes/content.admin.inc
Batching process for changing the field schema, running each affected node through node_save() first, to fire all hooks.

File

includes/content.admin.inc, line 1786
Administrative interface for content type creation.

Code

function content_field_batch_update($updates, &$context) {
  if (empty($field)) {
    $context['finished'] = 1;
    return;
  }
  $field_name = $updates['field_name'];
  $field = content_fields($field_name);
  if (!isset($context['sandbox']['progress'])) {
    $db_info = content_database_info($field);

    // Might run into non-existent tables when cleaning up a corrupted
    // database, like some of the old content storage changes in the
    // .install files.
    if (!db_table_exists($db_info['table'])) {
      return $context['finished'] = 1;
    }
    $nodes = array();
    $result = db_query("SELECT nid FROM {" . $db_info['table'] . "}");
    while ($node = db_fetch_array($result)) {
      $nodes[] = $node['nid'];
    }
    $context['sandbox']['progress'] = 0;
    $context['sandbox']['max'] = count($nodes);
    $context['sandbox']['nodes'] = $nodes;
  }

  // Process nodes by groups of 5.
  $count = min(5, count($context['sandbox']['nodes']));
  for ($i = 1; $i <= $count; $i++) {

    // For each nid, load the node, empty the column values
    // or the whole field, and re-save it.
    $nid = array_shift($context['sandbox']['nodes']);
    $node = content_field_replace($nid, array(
      $updates,
    ));

    // Store result for post-processing in the finished callback.
    $context['results'][] = l($node->title, 'node/' . $node->nid);

    // Update our progress information.
    $context['sandbox']['progress']++;
    $context['message'] = t('Processing %title', array(
      '%title' => $node->title,
    ));
  }

  // Inform the batch engine that we are not finished,
  // and provide an estimation of the completion level we reached.
  if ($context['sandbox']['progress'] != $context['sandbox']['max']) {
    $context['finished'] = $context['sandbox']['progress'] / $context['sandbox']['max'];
  }
}