ContextDefinition.php 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. <?php
  2. namespace Drupal\Core\Annotation;
  3. use Drupal\Component\Annotation\Plugin;
  4. use Drupal\Core\StringTranslation\TranslatableMarkup;
  5. /**
  6. * @defgroup plugin_context Annotation for context definition
  7. * @{
  8. * Describes how to use ContextDefinition annotation.
  9. *
  10. * When providing plugin annotations, contexts can be defined to support UI
  11. * interactions through providing limits, and mapping contexts to appropriate
  12. * plugins. Context definitions can be provided as such:
  13. * @code
  14. * context = {
  15. * "node" = @ContextDefinition("entity:node")
  16. * }
  17. * @endcode
  18. *
  19. * To add a label to a context definition use the "label" key:
  20. * @code
  21. * context = {
  22. * "node" = @ContextDefinition("entity:node", label = @Translation("Node"))
  23. * }
  24. * @endcode
  25. *
  26. * Contexts are required unless otherwise specified. To make an optional
  27. * context use the "required" key:
  28. * @code
  29. * context = {
  30. * "node" = @ContextDefinition("entity:node", required = FALSE, label = @Translation("Node"))
  31. * }
  32. * @endcode
  33. *
  34. * To define multiple contexts, simply provide different key names in the
  35. * context array:
  36. * @code
  37. * context = {
  38. * "artist" = @ContextDefinition("entity:node", label = @Translation("Artist")),
  39. * "album" = @ContextDefinition("entity:node", label = @Translation("Album"))
  40. * }
  41. * @endcode
  42. *
  43. * Specifying a default value for the context definition:
  44. * @code
  45. * context = {
  46. * "message" = @ContextDefinition("string",
  47. * label = @Translation("Message"),
  48. * default_value = @Translation("Checkout complete! Thank you for your purchase.")
  49. * )
  50. * }
  51. * @endcode
  52. *
  53. * @see annotation
  54. *
  55. * @}
  56. */
  57. /**
  58. * Defines a context definition annotation object.
  59. *
  60. * Some plugins require various data contexts in order to function. This class
  61. * supports that need by allowing the contexts to be easily defined within an
  62. * annotation and return a ContextDefinitionInterface implementing class.
  63. *
  64. * @Annotation
  65. *
  66. * @ingroup plugin_context
  67. */
  68. class ContextDefinition extends Plugin {
  69. /**
  70. * The ContextDefinitionInterface object.
  71. *
  72. * @var \Drupal\Core\Plugin\Context\ContextDefinitionInterface
  73. */
  74. protected $definition;
  75. /**
  76. * Constructs a new context definition object.
  77. *
  78. * @param array $values
  79. * An associative array with the following keys:
  80. * - value: The required data type.
  81. * - label: (optional) The UI label of this context definition.
  82. * - required: (optional) Whether the context definition is required.
  83. * - multiple: (optional) Whether the context definition is multivalue.
  84. * - description: (optional) The UI description of this context definition.
  85. * - default_value: (optional) The default value in case the underlying
  86. * value is not set.
  87. * - class: (optional) A custom ContextDefinitionInterface class.
  88. *
  89. * @throws \Exception
  90. * Thrown when the class key is specified with a non
  91. * ContextDefinitionInterface implementing class.
  92. */
  93. public function __construct(array $values) {
  94. $values += [
  95. 'required' => TRUE,
  96. 'multiple' => FALSE,
  97. 'default_value' => NULL,
  98. ];
  99. // Annotation classes extract data from passed annotation classes directly
  100. // used in the classes they pass to.
  101. foreach (['label', 'description'] as $key) {
  102. // @todo Remove this workaround in https://www.drupal.org/node/2362727.
  103. if (isset($values[$key]) && $values[$key] instanceof TranslatableMarkup) {
  104. $values[$key] = (string) $values[$key]->get();
  105. }
  106. else {
  107. $values[$key] = NULL;
  108. }
  109. }
  110. if (isset($values['class']) && !in_array('Drupal\Core\Plugin\Context\ContextDefinitionInterface', class_implements($values['class']))) {
  111. throw new \Exception('ContextDefinition class must implement \Drupal\Core\Plugin\Context\ContextDefinitionInterface.');
  112. }
  113. $class = isset($values['class']) ? $values['class'] : 'Drupal\Core\Plugin\Context\ContextDefinition';
  114. $this->definition = new $class($values['value'], $values['label'], $values['required'], $values['multiple'], $values['description'], $values['default_value']);
  115. }
  116. /**
  117. * Returns the value of an annotation.
  118. *
  119. * @return \Drupal\Core\Plugin\Context\ContextDefinitionInterface
  120. */
  121. public function get() {
  122. return $this->definition;
  123. }
  124. }