any plans to D7?

Comments

drupalisme’s picture

Yes, but any portings are welcome. Please submit any D7 version here, I will share this module Git permissions.

Fidelix’s picture

Subscribing...

W.M.’s picture

I agree that would be great with a D7 version

DarrellDuane’s picture

I'm hopeful this will still happen, I need it for some D7 projects I'm working on. I am guessing this will also be needed for Entities, so that changes can be made to Profile2 fields and others. Check out this article to get a head start on this port.

nedjo’s picture

Here's an approach I'm using in D7. I tried to write it to be flexible for various field types but only tested witih 'text' and 'image'.


/**
 * Rename an array of fields.
 *
 * Adapted from code in forum_update_7003(). See also
 * zgadzaj.com/how-to-change-the-machine-name-of-a-content-field-in-
 * drupal-7.
 *
 * @param $fields
 *   Array of field names, with old names as keys and new names as values.
 * @param $drop_first
 *   Boolean: whether to drop any existing tables for the renamed fields. If
 *   you have created the renamed field already - for example, by renaming
 *   a field that was exported to Features and then reverting the feature -
 *   you may wish to set this argument to TRUE so that your data will be
 *   copied.
 */
function mymodule_site_rename_fields($fields, $drop_first = FALSE) {
  foreach ($fields as $old_field_name => $new_field_name) {
    // Read field data.
    $old_field = field_read_field($old_field_name);

    // Update {field_config}.
    db_update('field_config')->fields(array('field_name' => $new_field_name))->condition('id', $old_field['id'])->execute();

    // Update {field_config_instance}.
    db_update('field_config_instance')->fields(array('field_name' => $new_field_name))->condition('field_id', $old_field['id'])->execute();

    // The tables that need updating in the form 'old_name' => 'new_name'.
    $tables = array(
      'field_data_' . $old_field_name => 'field_data_' . $new_field_name,
      'field_revision_' . $old_field_name => 'field_revision_' . $new_field_name,
    );
    // Iterate through tables to be redefined and renamed.
    foreach ($tables as $old_table => $new_table) {
      // Iterate through the field's columns. For example, a 'text' field will
      // have columns 'value' and 'format'.
      foreach ($old_field['columns'] as $column_name => $column_definition) {
        // Column names are in the format {field_name}_{column_name}.
        $old_column_name = $old_field_name . '_' . $column_name;
        $new_column_name = $new_field_name . '_' . $column_name;

        // If there is an index for the field, drop and then re-add it.
        $has_index = isset($old_field['indexes'][$column_name]) && ($old_field['indexes'][$column_name] == array($column_name));
        if ($has_index) {
          db_drop_index($old_table, $old_column_name);
        }

        // Rename the column.
        db_change_field($old_table, $old_column_name, $new_column_name, $column_definition);
        if ($has_index) {
          db_drop_index($old_table, $new_column_name);
          db_add_index($old_table, $new_column_name, array($new_column_name));
        }

        watchdog('mymodule', 'Renamed field !old_field_name to !new_field_name.', array('!old_field_name' => $old_field_name, '!new_field_name' => $new_field_name));
      }

      // The new table may exist e.g. due to having been included in a feature
      // that was reverted prior to this update being run. If so, we need to
      // drop the new table so that the old one can be renamed.
      if ($drop_first && db_table_exists($new_table)) {
        db_drop_table($new_table);
      }

      // Rename the table.
      db_rename_table($old_table, $new_table);
    }
  }
}

Sample usage:

$fields = array(
  'field_my_old_field_name' => 'field_my_new_field_name',
  'field_my_other_old_field_name' => 'field_my_other_new_field_name',
);

mymodule_site_rename_fields($fields);
akin.demirci’s picture

Works perfect! Thank you very much nedjo...

zilla’s picture

will this get a commit to dev version on project page as a D7 working version?

dsrikanth’s picture

Issue summary: View changes

@nedjo - Thanks for this! It works great...

rituraj.gupta’s picture

@nedjo - Good job. It works fine.

szt’s picture

Status: Active » Reviewed & tested by the community
fizk’s picture

Status: Reviewed & tested by the community » Closed (won't fix)

I've created a new Drupal 7 port here:

Field Rename

I'm not sure this is the best place for the Drupal 7 port because Drupal 7 fields are not "CCK" fields.