Last active
September 4, 2015 08:36
-
-
Save paullinney/22fb9803317bb7f5607b to your computer and use it in GitHub Desktop.
change text field length when data is present
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
* Utility to change the max length of a text field | |
* @see http://nathan.rambeck.org/blog/42-modify-drupal-7-text-field-maximum-length | |
* | |
* @param string the field name to update | |
* @param integer the new field length | |
*/ | |
function mymodul_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(); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment