You are here

function weight_update_7301 in Weight 7.3

Migrate weights to Weight field.

File

./weight.install, line 82
Install, update, and uninstall functions for the Weight module.

Code

function weight_update_7301(&$sandbox) {
  $field_name = $_SESSION['weight_field_name'];

  // Check for weight_weights table.
  if (db_table_exists('weight_weights')) {
    if (!isset($sandbox['progress'])) {
      $sandbox['progress'] = 0;
      $sandbox['last'] = 0;
      $sandbox['max'] = db_query("SELECT COUNT(entity_id) FROM {weight_weights}")
        ->fetchField();
    }
    $weights = db_select('weight_weights', 'w')
      ->fields('w')
      ->condition('entity_id', $sandbox['last'], '>')
      ->orderBy('entity_id')
      ->range(0, 10)
      ->execute();
    foreach ($weights as $weight) {
      if ($node = node_load($weight->entity_id)) {
        $node->{$field_name}[$node->language][0]['value'] = $weight->weight;
        node_save($node);
      }
      $sandbox['progress']++;
      $sandbox['last'] = $weight->entity_id;
    }
    $sandbox['#finished'] = empty($sandbox['max']) ? 1 : $sandbox['progress'] / $sandbox['max'];
  }
}