Drupal7: Renaming Fields and Bundles

David G - DrupalWhen developing a Drupal project it’s likely that your Content Types and Entities will go through revisions — meaning you’ll be altering the schema of how you want to structure your data and relationships in Drupal. In the past I’ve used Features, Configuration, hoping someday to use Config in Code — to store my site configuration in code. But you still need to create content types and entities somehow. And, possibly modify existing ones. Here are 2 tools I found to heavily modify/clone content types in Drupal 7.

So I have this large-ish (well not small) content type representing a Digital Identity (DiET) of a person:

Content type structure I want to move all fields to a new content type with different names.

Content type structure I want to move all fields to a new content type with different names.

I wanted to re-organize the content type and:

  • Rename the content type from Person to Diet (including the machine name).
  • Rename all the fields associated with the content type to the new type. I name my Fields using a convention like field_data_person_first_name. Where person is a prefix used for any fields within the Person content type. So the new field names should all begin with field_data_diet_FIELDNAME.

I could do this by hand in the Drupal administration area — but good god! … I wanted to go catch a movie, maybe the upcoming Mayweather vs Pacquiao fight … I have things to do!

So instead of doing this manually I relied on 1 module and 1 script. First I installed the Bundle Copy module. This module allows you to import/export/clone: Node types, Taxonomy, User, Field API fields and Field groups.

After installing the module and heading over to Admin -> Structure -> Content Types I am presented with new action tabs to Clone or Export configuration:

Additional export and clone options provided by Bundle Copy module.

Additional export and clone options provided by Bundle Copy module.

After selecting Clone I may then choose the Content Type (a Bundle type of the Node Entity) to clone and rename, including the new machine name:

Settings page to Clone a content type and provide its new human and machine names.

Settings page to Clone a content type and provide its new human and machine names.

This is the completed process showing my new content type and it’s fields:

Final cloned content type with fields.

Final cloned content type with fields.

Unfortunately, this does not rename all the fields to follow my desired naming convention.

After a bit of Googling I found this handy GIST script that renames the underlying Drupal SQL Field storage tables to whatever new name you want, so I enumerated my Person fields, and their new names in a PHP array and ran the script via drush php-script myfilename.inc.

// Drupal fields -- old_field_name => new_field_name
$fields = array(
    'field_people_age' => 'field_diet_age',
	'field_people_college' => 'field_diet_college',
	'field_people_common_name' => 'field_diet_common_name',
	'field_people_dates_in_office' => 'field_diet_dates_in_office',
	'field_people_dates_of_lifespan' => 'field_diet_dates_of_lifespan',
	'field_people_full_name' => 'field_diet_full_name',
	'field_people_govt_position' => 'field_diet_govt_position',
	'field_people_inaugurals' => 'field_diet_inaugurals',
	'field_people_location_born' => 'field_diet_location_born',
	'field_people_location_elected' => 'field_diet_location_elected',
	'field_people_media_names' => 'field_diet_media_names',
	'field_people_media_signatures' => 'field_diet_media_signatures',
	'field_people_office' => 'field_diet_office',
	'field_people_pictures' => 'field_diet_pictures',
	'field_people_political_party' => 'field_diet_political_party',
	'field_people_religion' => 'field_diet_religion',
  );

  // 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.

And viola! New content type, new field names all in under 5 minutes or so …. really beats doing everything manually by hand! Since this example uses some code I found in a Github GIST I cannot guarantee you may not loose data if you try this on a production website — always have backups and best of luck!

Looking for quality web hosting? Look no further than Arvixe Web Hosting!

Tags: , , , , | Posted under Drupal, Drush | RSS 2.0

Author Spotlight

David Gurba

I am a web programmer currently employed at UCSB. I have been developing web applications professionally for 8+ years now. For the last 5 years I’ve been actively developing websites primarily in PHP using Drupal. I have experience using LAMP and developing data driven websites for clients in aviation, higher education and e-commerce. If you’d like to contact me I can be reached at david.gurba@arvixe.com

Leave a Reply

Your email address will not be published. Required fields are marked *