3

I'd like to change the keys for a list (text) field type that already has content. Through the admin interface Drupal will tell you that "There is data for this field in the database. The field settings can no longer be changed." Is there any way to do this safely? Here's what I'm considering:

  1. db_change_field
  2. Direct SQL manipulation
  3. Changing values of content

I haven't messed with db_change_field before and am not even sure about it.

  • 1
    How about using the Rules module for this? ... Also ... Ps: for D7 ... as indicated by the 7-tag in your question ... – Pierre.Vriens Mar 29 '17 at 12:13
  • 🤔🤔🤔 Rules... it really is powerful. Could it really be used for changing keys of a field that already has content?? – cdmo Mar 29 '17 at 12:16
  • Have a look at this answer, which explains how to use Rules (and VBO) for a similar scenario. Replace the "vocabulary terms" with your "list field" and it's pretty much the very same approach you could also use to address this question ... safely. Not sure it it makes sense to post an extra answer here by cloning that answer and adapting it for your case (what do you think?). – Pierre.Vriens Mar 29 '17 at 18:30
2

db_change_field() is for changing the schema of a single column in a table, it doesn't relate to the field system as such.

The only way to change the name of a field is through direct (or pseudo-direct, using db API functions) manipulation of the database. These are things that need to change:

  • Name of the field in field_config
  • Name of corresponding field in field_config_instance
  • Name of the field_data_FIELD_NAME and field_revision_FIELD_NAME tables
  • Names of the data columns in the data and revision tables (probably FIELD_NAME_value and FIELD_NAME_format in your case)

After that clear the cache (preferably manually, i.e. truncate all tables that match the name cache_*) and you should see the changes in the site.

That should work on a stock install, but your mileage will likely vary if you have any contrib modules installed that provide functionality based on a field's name. I can't think of any except Field Collection of the top of my head, which won't apply to a text field, but there are probably others.

Whatever you do, take a backup of the database before you start playing around with it!

  • This answer is about the field name changing, but I believe the question was about the keys of a field, e.g. 1|Firstvalue, 2|Secondvalue where you want to delete the value 1|Firstvalue or something like that. – greggles Oct 6 '16 at 19:33
  • I should add, it's a very good answer to the question of how to change the name of a field! – greggles Oct 6 '16 at 19:43
1

The user interface and API won't let you do this directly so you have to do it with direct queries. I had to do it and this seemed to work. there are 2 big steps. Luckily the second has already been covered on StackOverflow.com so this answer will focus on the second.

  1. Change the field definition in field_config table
  2. Change the field data in the field_data_field_name tables

To change the field definition I did:

  1. Get the field definition information from the db: select * from field_config where field_name = 'field_name' limit 1 \G
  2. I copy and pasted the data column from the output and used a small PHP script to modify it as I wanted. In my case I was deleting some values, so I used unset. But the

    // Unserialize the data.
    $a = unserialize('a:8:{...... the actual data here...');
    // Modify the array as you need it.
    $tids = [524, 13, 20, 523, 19, 17, 703, 2312];
    foreach ($tids as $tid) {
      unset($a['settings']['allowed_values'][$tid]);
    }
    // Print the serialized resule.
    print serialize($a);
    
  3. In an update hook on the site you can update your db:

      db_update('field_config')
          ->condition('field_name', 'field_name')
          ->fields(['data' => '...your modified-and-serialized data goes here...'])
          ->execute();
    
  4. Clear all the caches.

And, as Clive pointed out and as is always a good idea: make a backup.

  • Thanks for the answer, and it's funny that I actually have yet another use case for this right now. I'm grouping and sorting a list of nodes by taxonomy terms, and Views by default seems to sort by the key, not the value of a field, so although I've changed the value of a field, the sort is still seeing the key. Anyway, this is to say, I will try this out and report back. Just to clarify, you say that step 2 is covered by that other SO post. But, it looks like your instructions are comprehensive. Especially that item 3 really covers step 1 and 2. Right? – cdmo Oct 7 '16 at 12:59
  • My instructions only fix the field config. It does not migrate the data in the field to the new keys. So if "1" used "Llama" and now "A" means "Llama" - any node that used to mean Llama will now mean...nothing. – greggles Oct 8 '16 at 13:04

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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