function weight_update_7210 in Weight 7.2
Ensure all nodes of enabled types have a weight.
File
- ./
weight.install, line 425
Code
function weight_update_7210(&$sandbox) {
$types = array();
$defaults = array();
$result = db_query('SELECT type, weight_default FROM {weight_settings} WHERE weight_enabled=1');
foreach ($result as $row) {
$types[] = $row->type;
$defaults[$row->type] = $row->weight_default;
}
if (!empty($types)) {
// Form the base query.
$query = db_select('node', 'n');
$query
->leftJoin('weight_weights', 'w', "w.entity_type = 'node' AND w.entity_id = n.nid");
$query
->fields('n', array(
'nid',
'type',
))
->condition('n.type', $types, 'IN')
->isNull('w.entity_id');
// The first time through, establish the total number of nodes.
if (!isset($sandbox['progress'])) {
$sandbox['progress'] = 0;
$sandbox['last'] = 0;
$sandbox['max'] = $query
->countQuery()
->execute()
->fetchField();
}
// Add the query conditions for the current iteration only.
$query
->condition('n.nid', $sandbox['last'], '>')
->orderBy('n.nid')
->range(0, 200);
$nodes = $query
->execute();
foreach ($nodes as $node) {
db_insert('weight_weights')
->fields(array(
'entity_id' => $node->nid,
'entity_type' => 'node',
'weight' => $defaults[$node->type],
))
->execute();
$sandbox['progress']++;
$sandbox['last'] = $node->nid;
}
$sandbox['#finished'] = empty($sandbox['max']) ? 1 : $sandbox['progress'] / $sandbox['max'];
}
}