Realize later on that text field you created needs to actually be a long text field? You’re not alone.
After doing some research, I found zero complete solutions. The ones I found leave problemattic remnants in the database, which can cause hard to diagnose issues such as restricted field length. See references.
Luckily for you, the drush script below will do a more complete conversion.
Thanks go out to the references though, for getting me started.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 |
<?php /** * Change a field from textfield to textarea * Unlike other guides online, this one actually corrects the blob values * Run this using drush: `drush scr <filepath>` */ $field_name = "field_test_text_field_2"; $rows = db_query("SELECT id,data FROM {field_config} WHERE field_name = '{$field_name}' ")->fetchAll(PDO::FETCH_OBJ); foreach ($rows as $row ){ $row->data=unserialize($row->data); unset($row->data['settings']['max_length']); $row->type="text_long"; drupal_write_record("field_config", $row, "id"); } // Update field_config_instance $rows = db_query("SELECT id,data FROM {field_config_instance} WHERE field_name = '{$field_name}'")->fetchAll(PDO::FETCH_OBJ); foreach ($rows as $row ){ $row->data=unserialize($row->data); $row->data['widget']['type']='text_textarea'; unset($row->data['widget']['settings']['size']); $row->data['widget']['settings']['rows']=3; drupal_write_record("field_config_instance", $row, "id"); } // Update the field table db_change_field("field_data_{$field_name}", "{$field_name}_value", "{$field_name}_value", array( "type" => "varchar", "length" => "20000", )); db_change_field("field_revision_{$field_name}", "{$field_name}_value", "{$field_name}_value", array( "type" => "varchar", "length" => "20000", )); // Clear caches. field_cache_clear(TRUE); |
References:
- http://www.pixelite.co.nz/article/convert-existing-textfield-textarea-drupal-7/
- http://www.up2technology.com/blog/converting-drupal-fields/
Thanks for this!!
I am not a developer. Can I really run this drush script safely?
I have a lot of data in a textfield and need to convert it to a textarea.
Thanks!
Hey Mike! I can’t guaranty that this will work 100%, therefor I would recommend you have a developer ready in case it fails.
Also, do back up your database first!
Will test it on a duplicated environment. However, I am a little afraid of long term effects or errors which might occur later. 🙁
I know you can’t guaranty anything. 🙂 I am just afraid of data corruptions or stuff like that which cannot be fixed and appear sometime later. Can you think of anything like that?
I totally understand your concerns, and they are valid. It is certainly possible that you may have a conflicting module installed, or that I missed updating a reference some where.
What I can tell you is that this script worked for me and did not seem to have any negative side-affects.
Good luck!
Will try it on our test server and test everything thoroughly. Thanks again! Will let you know here how it went. 🙂
Thanks for the feedback, Mike!
It seems that if you have the same field in multiple node types, the script is not working correctly and only adjusting the first instance. It would need at least a loop in the instance section.
Hmm, that could be true.
Hi, thanks for this script !
Has mentioned it works only for the first node.
I added two loop to solve the problem :
$field_name = “field_sous_titre”;
$rows = db_query(“SELECT id,data FROM {field_config} WHERE field_name = ‘{$field_name}’ “)->fetchAll(PDO::FETCH_OBJ);
foreach ($rows as $row ){
$row->data=unserialize($row->data);
unset($row->data[‘settings’][‘max_length’]);
$row->type=”text_long”;
drupal_write_record(“field_config”, $row, “id”);
}
$rows = db_query(“SELECT id,data FROM {field_config_instance} WHERE field_name = ‘{$field_name}'”)->fetchAll(PDO::FETCH_OBJ);
foreach ($rows as $row ){
$row->data=unserialize($row->data);
$row->data[‘widget’][‘type’]=’text_textarea’;
unset($row->data[‘widget’][‘settings’][‘size’]);
$row->data[‘widget’][‘settings’][‘rows’]=3;
drupal_write_record(“field_config_instance”, $row, “id”);
}
// Update the field table
db_change_field(“field_data_{$field_name}”, “{$field_name}_value”, “{$field_name}_value”, array(
“type” => “varchar”,
“length” => “20000”,
));
db_change_field(“field_revision_{$field_name}”, “{$field_name}_value”, “{$field_name}_value”, array(
“type” => “varchar”,
“length” => “20000”,
));
// Clear caches.
field_cache_clear(TRUE);
Thanks so much for the contribution! I updated the post to include your suggestions.