allowed_values_functions
No security coverage
This module simplifies the process of using a method for a field's allowed values.
Normally, you have to set the method directly in the field’s StorageConfig — either in the configuration file or via hook_entity_field_storage_info_alter().
After installing this module, you can simply add the AllowedValuesFunction attribute to the corresponding method and don’t have to worry about anything else.
Example:
#[AllowedValuesFunction('node', 'field_headline_alignment')]
#[AllowedValuesFunction('paragraph', 'field_subheadline_alignment')]
public static function subheadlineAlignmentAllowedValues(FieldStorageDefinitionInterface $definition, ?FieldableEntityInterface $entity = NULL): array {
$translation_args = [
'context' => 'subheadline alignment',
];
return [
'left' => t('Left', $translation_args),
'center' => t('Center', $translation_args),
'right' => t('Right', $translation_args),
];
}
Post-Installation
After installation create a class in one of your custom modules and add the method that provides the allowed values for a field. Add the attribute AllowedValuesFunction to the method.