Skip to content

Instantly share code, notes, and snippets.

@vincenzo
Created February 26, 2015 10:20
Show Gist options
  • Save vincenzo/97fcc533945579275a0a to your computer and use it in GitHub Desktop.
Save vincenzo/97fcc533945579275a0a to your computer and use it in GitHub Desktop.
Drupal: alter the length of a text field with data already in it.
// Our field's machine name is field_short_description and it is a text field which originally had a max length of 250 characters.
// Now we desire to alter that length so that it will be 450 characters.
// If you use Features and you have this field exported as part of a content type, you must update the settings in there too.
// However, updating the settings just in a Feature will not do the trick for a field that has already data in it, hence the following code.
// You can put this code in a hook_update_N().
// Alter 'field_data_field_short_description' table.
db_change_field(
'field_data_field_short_description',
'field_short_description_value',
'field_short_description_value',
array('type' => 'varchar', 'length' => 450)
);
// Alter 'field_revision_field_short_description' table.
db_change_field(
'field_revision_field_short_description',
'field_short_description_value',
'field_short_description_value',
array('type' => 'varchar', 'length' => 450)
);
// Update 'field_config' table.
$field_config_data = unserialize(db_select('field_config', 'fc')
->fields('fc', array('data'))
->condition('field_name', 'field_short_description', '=')
->execute()
->fetchField());
$field_config_data['settings']['max_length'] = 450;
db_update('field_config')
->fields(array('data' => serialize($field_config_data)))
->condition('field_name', 'field_short_description', '=')
->execute();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment