Upgraded drupal core with security updates
[yaffs-website] / web / core / modules / options / options.api.php
1 <?php
2
3 /**
4  * @file
5  * Hooks provided by the Options module.
6  */
7
8 use Drupal\Core\Entity\FieldableEntityInterface;
9 use Drupal\Core\Field\FieldStorageDefinitionInterface;
10
11 /**
12  * Alters the list of options to be displayed for a field.
13  *
14  * This hook can notably be used to change the label of the empty option.
15  *
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
20  *   properties.
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).
27  *
28  * @ingroup hooks
29  * @see hook_options_list()
30  */
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 ==');
36   }
37 }
38
39 /**
40  * Provide the allowed values for a 'list_*' field.
41  *
42  * Callback for options_allowed_values().
43  *
44  * 'list_*' fields can specify a callback to define the set of their allowed
45  * values using the 'allowed_values_function' storage setting.
46  *
47  * That function will be called:
48  *  - either in the context of a specific entity, which is then provided as the
49  *    $entity parameter,
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.
55  *
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.
58  *
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.
69  *
70  * @return array
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.
76  *
77  * @ingroup callbacks
78  * @see options_allowed_values()
79  * @see options_test_allowed_values_callback()
80  * @see options_test_dynamic_values_callback()
81  */
82 function callback_allowed_values_function(FieldStorageDefinitionInterface $definition, FieldableEntityInterface $entity = NULL, &$cacheable = TRUE) {
83   if (isset($entity) && ($entity->bundle() == 'not_a_programmer')) {
84     $values = [
85       1 => 'One',
86       2 => 'Two',
87     ];
88   }
89   else {
90     $values = [
91       'Group 1' => [
92         0 => 'Zero',
93         1 => 'One',
94       ],
95       'Group 2' => [
96         2 => 'Two',
97       ],
98     ];
99   }
100
101   return $values;
102 }