5 * Hooks provided by the Options module.
8 use Drupal\Core\Entity\FieldableEntityInterface;
9 use Drupal\Core\Field\FieldStorageDefinitionInterface;
12 * Alters the list of options to be displayed for a field.
14 * This hook can notably be used to change the label of the empty option.
16 * @param array $options
17 * The array of options for the field, as returned by
18 * \Drupal\Core\TypedData\OptionsProviderInterface::getSettableOptions(). An
19 * empty option (_none) might have been added, depending on the field
21 * @param array $context
22 * An associative array containing:
23 * - fieldDefinition: The field definition
24 * (\Drupal\Core\Field\FieldDefinitionInterface).
25 * - entity: The entity object the field is attached to
26 * (\Drupal\Core\Entity\EntityInterface).
29 * @see hook_options_list()
31 function hook_options_list_alter(array &$options, array $context) {
32 // Check if this is the field we want to change.
33 if ($context['fieldDefinition']->id() == 'field_option') {
34 // Change the label of the empty option.
35 $options['_none'] = t('== Empty ==');
40 * Provide the allowed values for a 'list_*' field.
42 * Callback for options_allowed_values().
44 * 'list_*' fields can specify a callback to define the set of their allowed
45 * values using the 'allowed_values_function' storage setting.
47 * That function will be called:
48 * - either in the context of a specific entity, which is then provided as the
50 * - or for the field generally without the context of any specific entity or
51 * entity bundle (typically, Views needing a list of values for an exposed
52 * filter), in which case the $entity parameter is NULL.
53 * This lets the callback restrict the set of allowed values or adjust the
54 * labels depending on some conditions on the containing entity.
56 * For consistency, the set of values returned when an $entity is provided
57 * should be a subset of the values returned when no $entity is provided.
59 * @param \Drupal\Core\Field\FieldStorageDefinitionInterface $definition
60 * The field storage definition.
61 * @param \Drupal\Core\Entity\FieldableEntityInterface|null $entity
62 * (optional) The entity context if known, or NULL if the allowed values are
63 * being collected without the context of a specific entity.
64 * @param bool &$cacheable
65 * (optional) If an $entity is provided, the $cacheable parameter should be
66 * modified by reference and set to FALSE if the set of allowed values
67 * returned was specifically adjusted for that entity and cannot not be reused
68 * for other entities. Defaults to TRUE.
71 * The array of allowed values. Keys of the array are the raw stored values
72 * (number or text), values of the array are the display labels. If $entity
73 * is NULL, you should return the list of all the possible allowed values in
74 * any context so that other code (e.g. Views filters) can support the allowed
75 * values for all possible entities and bundles.
78 * @see options_allowed_values()
79 * @see options_test_allowed_values_callback()
80 * @see options_test_dynamic_values_callback()
82 function callback_allowed_values_function(FieldStorageDefinitionInterface $definition, FieldableEntityInterface $entity = NULL, &$cacheable = TRUE) {
83 if (isset($entity) && ($entity->bundle() == 'not_a_programmer')) {