Problem/Motivation

Drupal 10.2.2 now offers an attractive GUI interface for adding new allowed values to a field and removing ones that are not in use. Except that if you try to submit the form on a field with any existing values, you get an error saying that it cannot have its keys changed.

Steps to reproduce

Create a list field with one or more allowed values. Create or edit an entity with this field, entering a value for the field. Edit the field storage and attempt to add or remove an allowed value. You will see an error saying "Oops, something went wrong. Check your browser's developer console for more details." The developer console and the log will both say,

Drupal\Core\Entity\Exception\FieldStorageDefinitionUpdateForbiddenException: A list field 'field_fieldname' with existing data cannot have its keys changed. in options_field_storage_config_update_forbid() (line 116 of core/modules/options/options.module).

Proposed resolution

unknown

Remaining tasks

unknown

CommentFileSizeAuthor
#4 List field update.gif2.98 MBAkhil Babu
Support from Acquia helps fund testing for Drupal Acquia logo

Comments

BenStallings created an issue. See original summary.

BenStallings’s picture

Title: Allowed values interface gives error when in use » Allowed values interface gives error when field in use
Issue summary: View changes
BenStallings’s picture

Issue summary: View changes
Akhil Babu’s picture

FileSize
2.98 MB

I tried to reproduce this issue in Drupal 10.2.2, but was not able to.
Steps followed

  • Created a fresh Drupal 10.2.2 site.
  • Added list field (Cardinality 1 & type 'Text') to aricle content type.
  • Added 2 values - 'value-1' and 'value-2'
  • Created an aricle, selected 'value-1' in list field. Published the article.
  • Updated the field configuration of the list field.
    • 'Value 1' is disabed as it is already used in article.
    • Able to add a new value 'Value 3'
    • Able to remove existing value 'Value 2'.

https://www.drupal.org/files/issues/2024-02-05/List%20field%20update.gif

Akhil Babu’s picture

Adding tags as issue is not reproducible.

Version: 10.2.x-dev » 11.x-dev

Drupal core is moving towards using a “main” branch. As an interim step, a new 11.x branch has been opened, as Drupal.org infrastructure cannot currently fully support a branch named main. New developments and disruptive changes should now be targeted for the 11.x branch. For more information, see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

pixlkat’s picture

Drupal 10.2.4. I have a list field where all the values are present in the database. I am trying to simply update the label on one of the allowed values and receive the error. I am not trying to change the keys, only the display text, which should be possible.

In my case, in the function `options_field_storage_config_update_forbid` the `$field_storage` and `$prior_field_storage` arguments do not report the allowed values in the same format. The current values are in a completely different format. This appears to have additional related to form_state --

$allowed_values = $field_storage->getSetting('allowed_values'):
[
  'table' => [
     0 => [
       'item' => ['label' => 'Text value', key => 4],
       'weight' => "0",
     ],
    1 => [
      'item' => ['label' => 'Another text value', key => 3],
      'weight' => "1",
    ],
  ],
]

However the value returned for the prior values returns something completely different. This appears to be the current config storage, without the extra information.

$prior_allowed_values = $prior_field_storage->getSetting('allowed_values')
[
  4 => 'Text value',
  3 => 'Another text value',
]

Given what the values are, this code will never work as intended.

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("A list field '{$field_storage->getName()}' with existing data cannot have its keys changed.");
    }
  }
}

pixlkat’s picture

After some further investigation, I believe https://www.drupal.org/node/3409364 could be behind what I was seeing. Once I upgraded to the latest version of the Field Permissions module, the behavior I was experiencing above (including the format of allowed values I was seeing in xdebug) is no longer present. I can no longer reproduce this issue.