1. 8.2.x core/lib/Drupal/Core/Annotation/ContextDefinition.php
  2. 8.2.x core/lib/Drupal/Core/Plugin/Context/ContextDefinition.php
  3. 8.0.x core/lib/Drupal/Core/Plugin/Context/ContextDefinition.php
  4. 8.0.x core/lib/Drupal/Core/Annotation/ContextDefinition.php
  5. 8.1.x core/lib/Drupal/Core/Plugin/Context/ContextDefinition.php
  6. 8.1.x core/lib/Drupal/Core/Annotation/ContextDefinition.php
  7. 8.3.x core/lib/Drupal/Core/Plugin/Context/ContextDefinition.php
  8. 8.3.x core/lib/Drupal/Core/Annotation/ContextDefinition.php

Namespace

Drupal\Core\Annotation

File

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

Classes

Namesort descending Description
ContextDefinition Defines a context definition annotation object.