options_test.module 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. <?php
  2. /**
  3. * @file
  4. * Helper module for the List module tests.
  5. */
  6. use Drupal\Core\Entity\FieldableEntityInterface;
  7. use Drupal\Core\Field\FieldStorageDefinitionInterface;
  8. /**
  9. * Implements callback_allowed_values_function().
  10. *
  11. * @see options_allowed_values()
  12. */
  13. function options_test_allowed_values_callback(FieldStorageDefinitionInterface $definition, FieldableEntityInterface $entity = NULL) {
  14. $values = [
  15. 'Group 1' => [
  16. 0 => 'Zero',
  17. ],
  18. 1 => 'One',
  19. 'Group 2' => [
  20. 2 => 'Some <script>dangerous</script> & unescaped <strong>markup</strong>',
  21. ],
  22. 'More <script>dangerous</script> markup' => [
  23. 3 => 'Three',
  24. ],
  25. ];
  26. return $values;
  27. }
  28. /**
  29. * Implements callback_allowed_values_function().
  30. *
  31. * @todo This function violates the recommendation in options_allowed_values()
  32. * to return a list of all possible values in any context when $items is
  33. * NULL. Since this is not yet used for testing Views integration, that is
  34. * alright for now. Fix this in https://www.drupal.org/node/2012130.
  35. *
  36. * @see options_allowed_values()
  37. */
  38. function options_test_dynamic_values_callback(FieldStorageDefinitionInterface $definition, FieldableEntityInterface $entity = NULL, &$cacheable = NULL) {
  39. $values = [];
  40. if (isset($entity)) {
  41. $cacheable = FALSE;
  42. $values = [
  43. $entity->label(),
  44. $entity->url(),
  45. $entity->uuid(),
  46. $entity->bundle(),
  47. ];
  48. }
  49. // We need the values of the entity as keys.
  50. return array_combine($values, $values);
  51. }