annotate core/modules/options/options.api.php @ 19:fa3358dc1485 tip

Add ndrum files
author Chris Cannam
date Wed, 28 Aug 2019 13:14:47 +0100
parents 4c8ae668cc8c
children
rev   line source
Chris@0 1 <?php
Chris@0 2
Chris@0 3 /**
Chris@0 4 * @file
Chris@0 5 * Hooks provided by the Options module.
Chris@0 6 */
Chris@0 7
Chris@0 8 use Drupal\Core\Entity\FieldableEntityInterface;
Chris@0 9 use Drupal\Core\Field\FieldStorageDefinitionInterface;
Chris@0 10
Chris@0 11 /**
Chris@0 12 * Alters the list of options to be displayed for a field.
Chris@0 13 *
Chris@0 14 * This hook can notably be used to change the label of the empty option.
Chris@0 15 *
Chris@0 16 * @param array $options
Chris@0 17 * The array of options for the field, as returned by
Chris@0 18 * \Drupal\Core\TypedData\OptionsProviderInterface::getSettableOptions(). An
Chris@0 19 * empty option (_none) might have been added, depending on the field
Chris@0 20 * properties.
Chris@0 21 * @param array $context
Chris@0 22 * An associative array containing:
Chris@0 23 * - fieldDefinition: The field definition
Chris@0 24 * (\Drupal\Core\Field\FieldDefinitionInterface).
Chris@0 25 * - entity: The entity object the field is attached to
Chris@0 26 * (\Drupal\Core\Entity\EntityInterface).
Chris@0 27 *
Chris@0 28 * @ingroup hooks
Chris@0 29 * @see hook_options_list()
Chris@0 30 */
Chris@0 31 function hook_options_list_alter(array &$options, array $context) {
Chris@0 32 // Check if this is the field we want to change.
Chris@0 33 if ($context['fieldDefinition']->id() == 'field_option') {
Chris@0 34 // Change the label of the empty option.
Chris@0 35 $options['_none'] = t('== Empty ==');
Chris@0 36 }
Chris@0 37 }
Chris@0 38
Chris@0 39 /**
Chris@0 40 * Provide the allowed values for a 'list_*' field.
Chris@0 41 *
Chris@0 42 * Callback for options_allowed_values().
Chris@0 43 *
Chris@0 44 * 'list_*' fields can specify a callback to define the set of their allowed
Chris@0 45 * values using the 'allowed_values_function' storage setting.
Chris@0 46 *
Chris@0 47 * That function will be called:
Chris@0 48 * - either in the context of a specific entity, which is then provided as the
Chris@0 49 * $entity parameter,
Chris@0 50 * - or for the field generally without the context of any specific entity or
Chris@0 51 * entity bundle (typically, Views needing a list of values for an exposed
Chris@0 52 * filter), in which case the $entity parameter is NULL.
Chris@0 53 * This lets the callback restrict the set of allowed values or adjust the
Chris@0 54 * labels depending on some conditions on the containing entity.
Chris@0 55 *
Chris@0 56 * For consistency, the set of values returned when an $entity is provided
Chris@0 57 * should be a subset of the values returned when no $entity is provided.
Chris@0 58 *
Chris@0 59 * @param \Drupal\Core\Field\FieldStorageDefinitionInterface $definition
Chris@0 60 * The field storage definition.
Chris@0 61 * @param \Drupal\Core\Entity\FieldableEntityInterface|null $entity
Chris@0 62 * (optional) The entity context if known, or NULL if the allowed values are
Chris@0 63 * being collected without the context of a specific entity.
Chris@0 64 * @param bool &$cacheable
Chris@0 65 * (optional) If an $entity is provided, the $cacheable parameter should be
Chris@0 66 * modified by reference and set to FALSE if the set of allowed values
Chris@0 67 * returned was specifically adjusted for that entity and cannot not be reused
Chris@0 68 * for other entities. Defaults to TRUE.
Chris@0 69 *
Chris@0 70 * @return array
Chris@0 71 * The array of allowed values. Keys of the array are the raw stored values
Chris@0 72 * (number or text), values of the array are the display labels. If $entity
Chris@0 73 * is NULL, you should return the list of all the possible allowed values in
Chris@0 74 * any context so that other code (e.g. Views filters) can support the allowed
Chris@0 75 * values for all possible entities and bundles.
Chris@0 76 *
Chris@0 77 * @ingroup callbacks
Chris@0 78 * @see options_allowed_values()
Chris@0 79 * @see options_test_allowed_values_callback()
Chris@0 80 * @see options_test_dynamic_values_callback()
Chris@0 81 */
Chris@0 82 function callback_allowed_values_function(FieldStorageDefinitionInterface $definition, FieldableEntityInterface $entity = NULL, &$cacheable = TRUE) {
Chris@0 83 if (isset($entity) && ($entity->bundle() == 'not_a_programmer')) {
Chris@0 84 $values = [
Chris@0 85 1 => 'One',
Chris@0 86 2 => 'Two',
Chris@0 87 ];
Chris@0 88 }
Chris@0 89 else {
Chris@0 90 $values = [
Chris@0 91 'Group 1' => [
Chris@0 92 0 => 'Zero',
Chris@0 93 1 => 'One',
Chris@0 94 ],
Chris@0 95 'Group 2' => [
Chris@0 96 2 => 'Two',
Chris@0 97 ],
Chris@0 98 ];
Chris@0 99 }
Chris@0 100
Chris@0 101 return $values;
Chris@0 102 }