Commit dab24c2d authored by davisben's avatar davisben

Upgrade path.

parent c1c1b6e1
......@@ -21,3 +21,92 @@ function weight_field_schema($field) {
),
);
}
/**
* Create a Weight field for each enabled content type.
*/
function weight_update_7300() {
field_cache_clear();
$field_name = 'field_weight';
$node_types = node_type_get_names();
// Check for weight_settings table, then create a weight field for each
// content type that has weight enabled.
if (db_table_exists('weight_settings')) {
$types = db_select('weight_settings', 'ws')
->fields('ws')
->execute();
// Make sure the field name doesn't exist.
$field = field_read_field($field_name, array('include_inactive' => TRUE));
if (!empty($field)) {
$i = 2;
while ($field = field_read_field($field_name . $i, array('include_inactive' => TRUE))) {
$i++;
}
$field_name = $field_name . $i;
}
$field = array(
'field_name' => $field_name,
'type' => 'weight',
);
field_create_field($field);
foreach ($types as $type) {
if ($type->weight_enabled && array_key_exists($type->type, $node_types)) {
$instance = array(
'field_name' => $field_name,
'entity_type' => 'node',
'label' => t('Weight'),
'bundle' => $type->type,
'settings' => array(
'range' => $type->weight_range,
),
'widget' => array(
'type' => 'weight_selector',
),
);
field_create_instance($instance);
}
}
}
$_SESSION['weight_field_name'] = $field_name;
}
/**
* Migrate weights to Weight field.
*/
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, 200)
->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']);
}
}
......@@ -5,6 +5,22 @@
* Provides a weight field that allows entities to be ordered.
*/
function weight_menu() {
$items['weight-test'] = array(
'title' => 'weight test',
'page callback' => 'weight_test',
'access callback' => TRUE,
);
return $items;
}
function weight_test() {
dpm(node_type_get_names());
return '';
}
/**
* Implements hook_field_info().
*/
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment