Skip to content

Instantly share code, notes, and snippets.

@kyleskrinak
Created November 4, 2014 20:01
Show Gist options
  • Save kyleskrinak/86bca51aa17aa32bd8aa to your computer and use it in GitHub Desktop.
Save kyleskrinak/86bca51aa17aa32bd8aa to your computer and use it in GitHub Desktop.
This Drush php-script allows you to increase the length of a text field that you specify on line #28. This script can be updated to allow for a user to pass the field name and length as args. That's an exercise for another time. I got the code from this blog post: http://nathan.rambeck.org/blog/42-modify-drupal-7-text-field-maximum-length
<?php
/*
* Utility to change the max length of a text field
*/
function 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();
}
change_text_field_max_length('field_event_co', 512);
echo 'Done.\r\n';
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment