Skip to content

Instantly share code, notes, and snippets.

@visualizeq
Created February 27, 2015 06:07
Show Gist options
  • Save visualizeq/5bc28141eb06f413317e to your computer and use it in GitHub Desktop.
Save visualizeq/5bc28141eb06f413317e to your computer and use it in GitHub Desktop.
D7 CCK Field Rename
<?php
define('DRUPAL_ROOT', getcwd());
require_once DRUPAL_ROOT . '/includes/bootstrap.inc';
drupal_bootstrap(DRUPAL_BOOTSTRAP_FULL);
$fields = array(
'field_tags' => 'field_carousel_tag',
);
// Loop through each of the fields/tables with the old name and change them
foreach ($fields as $field_name => $new_field_name) {
// First check that field_name exists
if (!db_table_exists('field_data_' . $field_name)) {
// If we cannot find a data table then just continue.
continue;
}
// Define some things...
$data_table_name = 'field_data_' . $field_name;
$revision_table_name = 'field_revision_' . $field_name;
$field_info = field_info_field($field_name);
$storage_details = $field_info['storage']['details'];
// The storage for each field has unique configuration. Must follow.
foreach ($storage_details['sql']['FIELD_LOAD_CURRENT'] as $field) {
// Change the field names.
foreach ($field as $key => $value) {
// Rename the field table columns and preserve existing spec. Let
// features take care of any configuration changes.
$spec = $field_info['columns'][$key];
db_change_field($data_table_name, $value, $new_field_name . "_" . $key, $spec);
db_change_field($revision_table_name, $value, $new_field_name . "_" . $key, $spec);
}
}
// Change the field storage table names.
db_rename_table($data_table_name, 'field_data_' . $new_field_name);
db_rename_table($revision_table_name, 'field_revision_' . $new_field_name);
// Change the field names int he field_config and
// field_instance_config tables
db_update('field_config')->fields(array(
'field_name' => $new_field_name,
))->condition('field_name', $field_name, '=')->execute();
db_update('field_config_instance')->fields(array(
'field_name' => $new_field_name,
))->condition('field_name', $field_name, '=')->execute();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment