0

I have a text field on my site which is becoming a problem. Users tend to input more text than I had expected. I need to convert this text field into a long text field. This is problematic because there is already content. I'm looking for a way to do the conversion as painlessly as possible. Here is my current plan:

  • Create the new long text field
  • Use a module (probably feeds) to duplicate the data (export, then re-import)
  • Delete the old text field.

Is this a good idea or is there a better faster one out there?

2
  • sql statements consider an option for you?
    – Aboodred1
    Apr 9, 2014 at 22:54
  • I had to do this recently, and tried to follow this tutorial, but I got errors (you can see my comment there, which is the latest one). I ended up creating the new long text field and then just copying over the values from the old field in the database using PHPMyAdmin. It was relatively painless. Apr 10, 2014 at 3:14

3 Answers 3

1

Though the question is pretty old, I would like to add my suggestion to it. I don't think changing the field type directly in the database is a good idea.

  1. Create a new field (long text) with the necessary settings, and add it to the content type.
  2. Then you can use the following script to copy all the old field values (text field) to new field (long text).
<?php
// Assign value from old_field to new_field
$nodes = node_load_multiple(array(), array("type" => "your_type"));
foreach ($nodes as $node) {
  $node->field_new = $node->field_old;
  field_attach_update('node', $node);
}
  1. Finally delete the old field and use the new field for further use.
0

Once a field is created from admin then it can not be edited later. Because field data is saved in Binary form and can not be edited.

e.g if we have field 'field1' then it will have table 'field_data_field_field1' and 'field_revision_field_field1' in the database. Now change its datatype directly in the database.

  1. Export these tables as a sql file and delete the old field in the manage field section of content type using admin.
  2. Create the new field with the same name as it was before and choose the appropriate datatype
  3. Now delete the newly created table 'field_data_field_field1' and 'field_revision_field_field1' and import the file that were exported earlier.
  4. Other way you can create a new field and copy the data from old tables to new table and change settings later.
0

That worked for me really well:

drush scr update_field_description_100.php

update_field_description.php

$field_name = "field_description_100";

$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);

Manage fields:

enter image description here

Content editor:

enter image description here

Taken from:

http://thedombroshow.com/drupal-7-converting-a-text-field-to-a-long-text-aka-textarea-field/

Not the answer you're looking for? Browse other questions tagged or ask your own question.