Change Allowed Values for List Fields in Drupal 8

Beginning in Drupal 8, field settings were moved into configuration files. The idea was to easily import these settings if something happened to your database and to ensure a sort of data integrity for a field’s value. Certainly an improvement to Drupal 7 (without the Features module) way of handling field settings.

What is the client wants to change the field’s setting after data has already been entered? Unless you’re adding a new item (while the existing list items remain the same), you’ll need to override the list values using a function in the global namespace. This option allows for dynamic values but requires future changes to be made in this function.

Steps to alter field values#

Step 1: Create module using Drupal Console to contain global override function

    
BASH
drupal generate:module

Note: Make sure to include a .module file

2: Add the global function in the module’s .module file

    
PHP
<?php use Drupal\field\Entity\FieldStorageConfig; use Drupal\Core\Entity\ContentEntityInterface; /** * Set dynamic allowed values for the background color field. */ function custom_module_list_values(FieldStorageConfig $definition, ContentEntityInterface $entity = NULL, $cacheable) { return [ 'one' => 'One', 'two' => 'Two', 'three' => 'Three', ]; }

Step 3: Find the configuration file and add the global function

In your project’s synced configuration folder, look for the field’s storage configuration. The name would look something like field.storage.paragraph.field_name_of_your_field.yml.

Inside the storage configuration file, look for the allowed_values_function key. Add the global function’s name to replace the existing values with the new list array.

    
YAML
allowed_values_function: custom_module_list_values