options.api.php 3.6 KB

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