Options module should not prevent field storage updates when switching to/from function

Created on 11 June 2019, about 5 years ago
Updated 3 February 2024, 4 months ago

The options module provides a field type that allows site builders to specify a list of allowed options. A hidden feature of this module is the ability to also dynamically populate the list of allowed options by specifying a value for the allowed_values_function setting in configuration instead of hardcoding a list.

I recently made this change on one of my site's field storage config objects to switch from the hardcoded list to a list provided by a function. I wrote an update hook to apply the config change, but it failed because of this code in the options module:

/**
 * Implements hook_field_storage_config_update_forbid().
 */
function options_field_storage_config_update_forbid(FieldStorageConfigInterface $field_storage, FieldStorageConfigInterface $prior_field_storage) {
  if ($field_storage->getTypeProvider() == 'options' && $field_storage->hasData()) {
    // Forbid any update that removes allowed values with actual data.
    $allowed_values = $field_storage->getSetting('allowed_values');
    $prior_allowed_values = $prior_field_storage->getSetting('allowed_values');
    $lost_keys = array_keys(array_diff_key($prior_allowed_values, $allowed_values));
    if (_options_values_in_use($field_storage->getTargetEntityTypeId(), $field_storage->getName(), $lost_keys)) {
      //throw new FieldStorageDefinitionUpdateForbiddenException(t('A list field (@field_name) with existing data cannot have its keys changed.', ['@field_name' => $field_storage->getName()]));
    }
  }
}

Options module is preventing the update from going through because the new list of allowed values is essentially an empty array, since the values are not dynamically generated.

I think that if a switch is made TO a function from a hardcoded list, or FROM a function to a hardcoded list, then options module should assume developer knows what they're doing and not intervene.

πŸ› Bug report
Status

Needs work

Version

11.0 πŸ”₯

Component
OptionsΒ  β†’

Last updated 27 days ago

No maintainer
Created by

πŸ‡ΊπŸ‡ΈUnited States bkosborne New Jersey, USA

Live updates comments and jobs are added and updated live.
  • Needs tests

    The change is currently missing an automated test that fails when run with the original code, and succeeds when the bug has been fixed.

Missing content requested by

πŸ‡¦πŸ‡ΊAustralia dpi
6 months ago
Sign in to follow issues

Merge Requests

Comments & Activities

Production build 0.69.0 2024