Created
November 7, 2013 18:33
-
-
Save sherakama/7359561 to your computer and use it in GitHub Desktop.
How to rename a cck field machine's name in Drupal 7.x
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$fields = array( | |
'field_my_fields' => 'field_my_new_field', | |
'field_other_field' => 'field_adam_west', | |
); | |
// 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(); | |
} /// end foreach loop on fields... whew. |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment