Extending Drupal 7 text field character limit

After getting the message, "There is data for this field in the database. The field settings can no longer be changed." while trying to extend the character limit for a text field in a Drupal 7 instance, I used the code below to increase the character limit. It modifies the relevant parts of the database to allow more characters. I ran the SQL code through a content node with the PHP Code Text Filter enabled, but it could also be run in a custom module. Of course, I took care to back up my site database first!
<?php
$field_to_update = 'field_text_field_to_extend'; //Replace with field slug
$new_chars = '500';  //Replace with extended character limit
 
$result1 = db_query('ALTER TABLE {field_data_'.$field_to_update.'} CHANGE '.$field_to_update.'_value '.$field_to_update.'_value VARCHAR('.$new_chars.')');
$result2 = db_query('ALTER TABLE {field_revision_'.$field_to_update.'} CHANGE '.$field_to_update.'_value '.$field_to_update.'_value VARCHAR('.$new_chars.')');
 
$result3 = db_query('SELECT CAST(data AS CHAR(10000) CHARACTER SET utf8) data FROM {field_config} WHERE field_name = \''.$field_to_update.'\'');
foreach ($result3 as $result) {
	$data = $result->data;
}
$data_array = unserialize($data);
$data_array['settings']['max_length'] = $new_chars;
$new_data = serialize($data_array);
 
$result4 = db_query('UPDATE {field_config} SET data = :data WHERE field_name = :field', array(':data' => $new_data, ':field' => $field_to_update));
?>

Comments

Post new comment

The content of this field is kept private and will not be shown publicly.
  • Web page addresses and e-mail addresses turn into links automatically.
  • Allowed HTML tags: <a> <em> <strong> <cite> <code> <ul> <ol> <li> <dl> <dt> <dd>
  • Lines and paragraphs break automatically.
  • Each email address will be obfuscated in a human readable fashion or (if JavaScript is enabled) replaced with a spamproof clickable link.

More information about formatting options

CAPTCHA
This question tests whether you are a human visitor, to prevent automated spam submissions.
Question text provided by textcaptcha.com