function field_conditional_state_update_7200 in Field Conditional States 7.2
Upgrade module from 7.x-1.x to 7.x-2.x.
File
- ./
field_conditional_state.install, line 124 - Install function for the field_conditional_state module.
Code
function field_conditional_state_update_7200() {
// Add new group_id field.
$spec = array(
'type' => 'int',
'not null' => TRUE,
'initial' => 0,
'description' => 'The field conditional states group id.',
);
db_add_field('field_conditional_state', 'group_id', $spec);
$spec = array(
'type' => 'text',
'not null' => TRUE,
'initial' => '',
'description' => 'The trigger value.',
);
db_add_field('field_conditional_state', 'trigger_value', $spec);
$spec = array(
'type' => 'varchar',
'length' => '15',
'not null' => TRUE,
'initial' => '',
'description' => 'The trigger state.',
);
db_add_field('field_conditional_state', 'trigger_state', $spec);
// Change control_field field properties.
$spec = array(
'type' => 'varchar',
'length' => '255',
'not null' => TRUE,
'description' => 'The control field name.',
);
db_change_field('field_conditional_state', 'control_field', 'control_field', $spec);
// Add new states group table.
$schema['field_conditional_states_group'] = array(
'fields' => array(
'group_id' => array(
'type' => 'serial',
'not null' => TRUE,
'description' => 'The internal identifier.',
),
'state' => array(
'type' => 'varchar',
'length' => '15',
'not null' => TRUE,
'description' => '',
),
'entity_type' => array(
'type' => 'varchar',
'length' => '255',
'not null' => TRUE,
'description' => '',
),
'bundle' => array(
'type' => 'varchar',
'length' => '255',
'not null' => TRUE,
'description' => '',
),
'field_name' => array(
'type' => 'varchar',
'length' => '255',
'not null' => TRUE,
'description' => '',
),
'type' => array(
'type' => 'varchar',
'length' => '3',
'not null' => TRUE,
'description' => 'The logical connection.',
),
),
'primary key' => array(
'group_id',
),
);
db_create_table('field_conditional_states_group', $schema['field_conditional_states_group']);
$bundle_data = field_info_bundles();
// Upgrade settings data from 7.x-1.x to 7.x-2.x.
$fields = db_query("SELECT * FROM {field_conditional_state}");
foreach ($fields as $field) {
// In case FCS was used for a non-node we have to use the right entity type
// so we have to search for the bundle within the existing entity types
// if more than one entity type has a bundle with the same name
// we can't decide which one is the right bundle
// (so we'll simply take the first found entity type).
$entity_type = 'node';
foreach ($bundle_data as $type => $bundles) {
if (isset($bundles[$field->bundle])) {
$entity_type = $type;
break;
}
}
$group_id = db_insert('field_conditional_states_group')
->fields(array(
'state' => $field->state,
'entity_type' => $entity_type,
'bundle' => $field->bundle,
'field_name' => $field->field_name,
'type' => $field->condition_type,
))
->execute();
$control_field_info = field_info_field($field->control_field);
$trigger_values = array_keys(unserialize($field->trigger_values));
foreach ($trigger_values as $trigger_value) {
$trigger_state = 'value';
if ($control_field_info['type'] == 'list_boolean') {
$trigger_state = $trigger_value ? 'checked' : 'unchecked';
}
db_insert('field_conditional_state')
->fields(array(
'group_id' => $group_id,
'control_field' => $field->control_field,
'trigger_state' => $trigger_state,
'trigger_value' => $trigger_value,
// We are going to delete the following fields later,
// but we have to provide a value for them because
// no NULL values are allowed.
'field_name' => '',
'state' => '',
'bundle' => '',
'trigger_values' => '',
'condition_type' => '',
))
->execute();
}
db_delete('field_conditional_state')
->condition('id', $field->id)
->execute();
}
// Remove no longer needed fields.
db_drop_field('field_conditional_state', 'field_name');
db_drop_field('field_conditional_state', 'state');
db_drop_field('field_conditional_state', 'bundle');
db_drop_field('field_conditional_state', 'trigger_values');
db_drop_field('field_conditional_state', 'condition_type');
return t('Upgraded module settings from 7.x-1.x to 7.x-2.x.');
}