How change the allowed values of a select list or radio field dynamically in drupal 8
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
settings: | |
allowed_values: { } | |
# Add the line below to your file as it will reference the function you'll create in your .module file | |
allowed_values_function: example_allowed_values_function |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
use Drupal\Core\Entity\ContentEntityInterface; | |
use Drupal\field\Entity\FieldStorageConfig; | |
/** | |
* Set dynamic allowed values for the content author field(s). | |
* | |
* @param \Drupal\field\Entity\FieldStorageConfig $definition | |
* The field definition. | |
* @param \Drupal\Core\Entity\ContentEntityInterface|null $entity | |
* The entity being created if applicable. | |
* @param bool $cacheable | |
* Boolean indicating if the results are cacheable. | |
* | |
* @return array | |
* An array of possible key and value options. | |
* | |
* @see options_allowed_values() | |
*/ | |
function example_allowed_values_function(FieldStorageConfig $definition, ContentEntityInterface $entity = NULL, $cacheable) { | |
$options = [ | |
'current_user' => 'Current user', | |
'content_creator' => 'Content user' | |
]; | |
return $options; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Some notes:
drush cim
for the changes to come into effect