How to Modify Drupal 7 text field maximum length

Why we need to modify text field length in drupal

When we upgrade the drupal 6 to 7 we need to migrate cck fields also.

In drupal 6 we have long text field also and when we migrate that field to drupal 7 it returns error related to field length.  OR

When creating a text field in Drupal 7, you must a choose a maximum length for your data. As soon as you create any data for this field, the maximum length become immutable in the Drupal field settings.

It’s understandable that this would be disabled for decreasing the maximum length because it could results in a loss of data, however, it should be possible to increase the maximum length for any field.

How to modify text field length in drupal

A todo in the Drupal 7 Text module code shows this was intended but never accomplished.

text.module line 77

[code type=php]

  if ($field['type'] == 'text') {
    $form['max_length'] = array(
      '#type' => 'textfield',
      '#title' => t('Maximum length'),
      '#default_value' => $settings['max_length'],
      '#required' => TRUE,
      '#description' => t('The maximum length of the field in characters.'),
      '#element_validate' => array('element_validate_integer_positive'),
      // @todo: If $has_data, add a validate handler that only allows
      // max_length to increase.
      '#disabled' => $has_data,
    );
  }

[/code]

I recently needed to increase the maximum length for a few fields and ended up creating a re-usable function in a custom module’s install file.

The 3 things that need to happen:

  1. Change the VARCHAR length of the value column in the field_data_{fieldname} table
  2. Change the VARCHAR length of the value column in the field_revision_{fieldname} table
  3. Update the configuration of the field to reflect the new max length setting

The following function accomplishes all 3 of these steps and takes 2 easy parameters including the name of the field and the new maximum length.

[code type=php]

/*
 * Utility to change the max length of a text field
 */
function mymodule_change_text_field_max_length($field_name, $new_length) {
  $field_table = 'field_data_' . $field_name;
  $field_revision_table = 'field_revision_' . $field_name;
  $field_column = $field_name . '_value';
 
  // Alter value field length in fields table
  db_query("ALTER TABLE `{$field_table}` CHANGE `{$field_column}` `{$field_column}` VARCHAR( {$new_length} )");
  // Alter value field length in fields revision table
  db_query("ALTER TABLE `{$field_revision_table}` CHANGE `{$field_column}` `{$field_column}` VARCHAR( {$new_length} )");
 
  // Update field config with new max length
  $result = db_query("SELECT CAST(`data` AS CHAR(10000) CHARACTER SET utf8) FROM `field_config` WHERE field_name = '{$field_name}'");
  $config = $result->fetchField();
  $config_array = unserialize($config);
  $config_array['settings']['max_length'] = $new_length;
  $config = serialize($config_array);
  db_update('field_config')
    ->fields(array('data' => $config))
    ->condition('field_name', $field_name)
    ->execute();
}
[/code]

Once this function is available in your custom install file you can create a new database update function that uses this new function to make your required changes.

[code type=php]
/**
 * Change max_length of Name field
 */
function mymodule_update_7002() {
  mymodule_change_text_field_max_length('field_name', 50);
}
[/code]