Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save jamesmorrish/6606596 to your computer and use it in GitHub Desktop.
Save jamesmorrish/6606596 to your computer and use it in GitHub Desktop.
Change the length of a text field in Drupal
assuming you have a text field which machine name is field_text of 10 characters you want to grow to 25:
data will be stored in two tables: field_data_field_text and field_revision_field_text
definition is stored in field_config for the storage data, and field_config_instance for each instance of this field (stuff like label).
Now let's do a little heart surgery.
Alter the data tables columns definitions:
ALTER TABLE `field_data_field_text`
CHANGE `field_text_value` `field_text_value` VARCHAR( 25 )
CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL;
ALTER TABLE `field_revision_field_text`
CHANGE `field_text_value` `field_text_value` VARCHAR( 25 )
CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL;
Change the definition column, this one is very tricky because it's stored in a BLOB, but that's not something that will stop you for doing this.
Dissect the guts of this BLOB thing:
SELECT CAST(`data` AS CHAR(10000) CHARACTER SET utf8)
FROM `field_config` WHERE field_name = 'field_text';
This will give you something like:
a:7:{s:12:"translatable";s:1:"1";s:12:"entity_types";a:0:{}s:8:"settings";a:2:
{s:10:"max_length";s:2:"10";s:17:"field_permissions";a:5:
//a lot more stuff...
This is a PHP serialized array, the interesting part is s:10:"max_length";s:2:"10";, this mean this array has a property named max_length (which name is a 10 characters string - hence the "s") which value is 10 (which is a 2 characters long string). It's pretty easy, isn't it?
Changing its value is as easy as replacing the s:2:"10" part by s:2:"25". Be careful: if your new value is longer you have to adapt the "s" part, for example putting 100 will be s:3:"100" as 100 length is 3.
Let's put this new value back in the DB, don't forget to keep the whole string.
UPDATE `field_config`
SET data = 'a:7:{...a:2:{s:10:"max_length";s:2:"25";...}'
WHERE `field_name` = 'field_text'
Flush your caches.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment