function bat_event_update_7103 in Booking and Availability Management Tools for Drupal 7
Add target entity type field and update existing event types.
File
- modules/
bat_event/ bat_event.install, line 358 - Sets up the base table for our entity and a table to store information about the entity types.
Code
function bat_event_update_7103() {
$field = array(
'type' => 'varchar',
'not null' => FALSE,
'length' => 32,
'description' => 'The machine name of the target entity type for this event type.',
);
db_add_field('bat_event_type', 'target_entity_type', $field);
db_update('bat_event_type')
->fields(array(
'target_entity_type' => 'bat_unit',
))
->execute();
// Copy data from the old field.
$old_name = 'event_unit_reference';
$new_name = 'event_bat_unit_reference';
$entity_type = 'bat_event';
// Get old field info.
$old_field = field_info_field($old_name);
// Create new field.
$new_field = $old_field;
$new_field['field_name'] = $new_name;
if (!field_info_field($new_name)) {
field_create_field($new_field);
}
else {
field_update_field($new_field);
}
foreach (bat_event_get_types() as $bundle => $event_type) {
// Get old field instance.
$old_instance = field_info_instance($entity_type, $old_name, $bundle);
if ($old_instance) {
$new_instance = $old_instance;
$new_instance['field_name'] = $new_name;
if (!field_info_instance($entity_type, $new_name, $bundle)) {
field_create_instance($new_instance);
}
else {
field_update_instance($new_instance);
}
// Migrate old fields' data to the new ones.
$field_data = db_select('field_data_' . $old_name, 'old')
->fields('old')
->condition('entity_type', $entity_type)
->condition('bundle', $bundle)
->execute();
while ($data = $field_data
->fetchAssoc()) {
$data_new = array();
foreach ($data as $column => $value) {
$column = str_replace($old_name, $new_name, $column);
$data_new[$column] = $value;
}
db_insert('field_data_' . $new_name)
->fields($data_new)
->execute();
}
// Migrate old fields' revision data to the new ones.
$field_revision = db_select('field_revision_' . $old_name, 'old')
->fields('old')
->condition('entity_type', $entity_type)
->condition('bundle', $bundle)
->execute();
while ($revision = $field_revision
->fetchAssoc()) {
$revision_new = array();
foreach ($revision as $column => $value) {
$column = str_replace($old_name, $new_name, $column);
$revision_new[$column] = $value;
}
db_insert('field_revision_' . $new_name)
->fields($revision_new)
->execute();
}
// Delete old instance.
field_delete_instance($old_instance);
// Purge fields.
field_purge_batch(1000);
}
}
}