You are here

function _serial_init_old_nodes in Serial Field 6

Initializes the value of a new serial field in existing nodes.

Return value

the number of existing nodes that have been initialized.

1 call to _serial_init_old_nodes()
serial_form_alter in ./serial.module
Implementation of hook_form_alter().

File

./serial.inc, line 149
Internal functions for the Serial module.

Code

function _serial_init_old_nodes($field) {

  // Retrieve all the node ids of that type:
  $query = "SELECT DISTINCT nid FROM {node} WHERE type = '%s' ORDER BY nid";
  $result = db_query($query, $field['type_name']);

  // Allocate a serial number for every old node:
  $count = 0;
  $field_name = $field['field_name'];
  while ($nid = db_result($result)) {
    $node = node_load($nid);
    $node->{$field_name}[0]['value'] = _serial_generate_value($nid, $field, FALSE);
    node_save($node);
    $last_nid = $nid;
    $count++;
  }

  // Delete temporary records (except the last):
  if (isset($last_nid)) {
    $serial_table = _serial_get_field_table_name($field);
    $query = "DELETE FROM {$serial_table} WHERE nid < %d";
    db_query($query, $last_nid);
  }

  // Return the number of existing nodes that have been initialized:
  return $count;
}