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?
There's no easy way, no. As far as I can tell these are the manual DB changes you'll need to make:
In field_config
change the type
column to 'list_text' and module
column to 'list' for your field.
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.
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).
Add an index to those tables for the value
column.
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With