Last active
January 10, 2020 18:57
-
-
Save lincoln-chawora/4ed1e407ba83d2e6f1bb5acbc75a63d5 to your computer and use it in GitHub Desktop.
How change the allowed values of a select list or radio field dynamically in drupal 8
This file contains hidden or 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 hidden or 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