Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

convert field text to List (text) drupal 7

I have a field that is type text and I want to convert it to type List (text) in Drupal 7. I can't simply change the value, as it won't let me, is there an easy way to do this?

like image 234
Chris Muench Avatar asked Oct 18 '11 18:10

Chris Muench


2 Answers

There's no easy way, no. As far as I can tell these are the manual DB changes you'll need to make:

  1. In field_config change the type column to 'list_text' and module column to 'list' for your field.

  2. Then you'll need to change the serialised array in the data column to match that of a list type (it'll have the settings for text types at the moment obviously). There's no easy way to show you how to do it here, the best way would be to compare the serialised array with one for a different field of a list type, then you'll be able to see what values you need to remove/change. The allowed_values array under settings will be where you put your list's values. Once that's changed, re-serialise it and put it back in the data column for your field.

  3. After that remove any columns from your field's field_data and field_revision tables that begin with field_myfield_ but aren't field_myfield_value (the list type still needs that column).

  4. Add an index to those tables for the value column.

  5. Clear your caches

I must warn that's completely untested it just seems to make sense looking at the table structures, it'd be a good idea to take a back up of the three tables you'll be playing with just in case.

like image 175
Clive Avatar answered Sep 22 '22 13:09

Clive


I just wrote some code that should handle this for you in my Helper module: http://cgit.drupalcode.org/helper/tree/lib/FieldChangeHelper.php?h=7.x-1.x

FieldChangeHelper::changeType('my_text_field_name', 'list_text');

It should work fine for most simple cases and fields, but should be tested thoroughly. It also currently doesn't update any field formatters, so you'll want to review the display configuration for the field after running this.

like image 28
Dave Reid Avatar answered Sep 24 '22 13:09

Dave Reid