Use hook_update_N and Features to update key|value options already in use - Drupal 7

drupal 7 230525

Drupal 7 The information below is most suited to Drupal 7. It may or not be applicable to later versions of Drupal

Using updates still seems like a good way to do this, even though features are deprecated. Suggest rewriting to do the update in a regular module.

Say you have a 'Diary Entry' content type and a 'Feeling' dropdown field (field_diary_feeling) with the following allowed values

  • Amazing|Amazing

  • Happy|Happy

  • Excited|Excited

  • Indifferent|Indifferent

  • Unhappy|Unhappy

One day you decide to replace 'Unhappy' with 'A Little Down', however your website already has hundreds of diary entries and some of them have 'Feeling' set to 'Unhappy'. Using the field settings UI will produce the error below: 

"Allowed values list: some values are being removed while currently in use."

What do you do now?

One workaround is to add 'A little Sad' to the list of allowed values, then use VBO (Views Bulk Operations) to filter by 'Feeling' where the value of 'Feeling' is 'Unhappy' and set 'A little down' as the new value. Finally remove 'Unhappy' from the allowed values list.

The above workaround is pretty nifty, however this solution is not multi-environment platform (i.e. Pantheon) friendly. A recommended approach will use features and hook_update_N as shown below.

Create a Feature

Step 1: Package your content type in a Feature. We'll call it 'Diary'.

Note: Make sure to provide a version number such as 7.x-1.0

Implement hook_update_N

Step 2: Create a file named diary.install in your Diary feature directory and insert the code below

<?php

/**

 * Update keys in settings for field_diary_feeling field

 */

function pages_update_7100(&$sandbox) {

  // Update field data table

db_update('field_data_field_diary_feeling')

  ->fields(array('field_diary_feeling_value' => 'A little down'))

  ->condition('field_diary_feeling_value', 'Unhappy')

  ->execute();

  // Update field revision table

  db_update('field_revision_field_diary_feeling')

  ->fields(array('field_diary_feeling_value' => 'A little down'))

  ->condition('field_diary_feeling_value', 'Unhappy')

  ->execute();

}

Note: 7100 represents the drupal core version (7), the module/feature branch (1.x) and the sequential patch number (00 because this is the first database update, the next one will be 01).

Update diary.info and diary.features.field_base.inc

Step 3: Edit diary.info and increase the module/feature version number, so if it was previously 1.0, it should now be 1.1.

Step 4: Find $field_bases['field_diary_feeling'] and edit the key|value pairs in 'allowed_values' under 'settings'.

Run update.php

Step 5: Run update.php and you'll be good to go.



Resources:

https://api.drupal.org/api/drupal/modules%21system%21system.api.php/function/hook_update_N/7


Review History

Who

When

Status

Who

When

Status

 

 

 

Bob

20230525

Drupal 7 with a suggestion of how to update for later Drupals