Drupal 7: Converting a text field to a long text (aka textarea) field

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.

<?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:

  1. http://www.pixelite.co.nz/article/convert-existing-textfield-textarea-drupal-7/
  2. http://www.up2technology.com/blog/converting-drupal-fields/