3 namespace Drupal\Core\Annotation;
5 use Drupal\Component\Annotation\Plugin;
6 use Drupal\Core\StringTranslation\TranslatableMarkup;
9 * @defgroup plugin_context Annotation for context definition
11 * Describes how to use ContextDefinition annotation.
13 * When providing plugin annotations, contexts can be defined to support UI
14 * interactions through providing limits, and mapping contexts to appropriate
15 * plugins. Context definitions can be provided as such:
18 * "node" = @ContextDefinition("entity:node")
22 * To add a label to a context definition use the "label" key:
25 * "node" = @ContextDefinition("entity:node", label = @Translation("Node"))
29 * Contexts are required unless otherwise specified. To make an optional
30 * context use the "required" key:
33 * "node" = @ContextDefinition("entity:node", required = FALSE, label = @Translation("Node"))
37 * To define multiple contexts, simply provide different key names in the
41 * "artist" = @ContextDefinition("entity:node", label = @Translation("Artist")),
42 * "album" = @ContextDefinition("entity:node", label = @Translation("Album"))
46 * Specifying a default value for the context definition:
49 * "message" = @ContextDefinition("string",
50 * label = @Translation("Message"),
51 * default_value = @Translation("Checkout complete! Thank you for your purchase.")
62 * Defines a context definition annotation object.
64 * Some plugins require various data contexts in order to function. This class
65 * supports that need by allowing the contexts to be easily defined within an
66 * annotation and return a ContextDefinitionInterface implementing class.
70 * @ingroup plugin_context
72 class ContextDefinition extends Plugin {
75 * The ContextDefinitionInterface object.
77 * @var \Drupal\Core\Plugin\Context\ContextDefinitionInterface
79 protected $definition;
82 * Constructs a new context definition object.
84 * @param array $values
85 * An associative array with the following keys:
86 * - value: The required data type.
87 * - label: (optional) The UI label of this context definition.
88 * - required: (optional) Whether the context definition is required.
89 * - multiple: (optional) Whether the context definition is multivalue.
90 * - description: (optional) The UI description of this context definition.
91 * - default_value: (optional) The default value in case the underlying
93 * - class: (optional) A custom ContextDefinitionInterface class.
96 * Thrown when the class key is specified with a non
97 * ContextDefinitionInterface implementing class.
99 public function __construct(array $values) {
103 'default_value' => NULL,
105 // Annotation classes extract data from passed annotation classes directly
106 // used in the classes they pass to.
107 foreach (['label', 'description'] as $key) {
108 // @todo Remove this workaround in https://www.drupal.org/node/2362727.
109 if (isset($values[$key]) && $values[$key] instanceof TranslatableMarkup) {
110 $values[$key] = (string) $values[$key]->get();
113 $values[$key] = NULL;
116 if (isset($values['class']) && !in_array('Drupal\Core\Plugin\Context\ContextDefinitionInterface', class_implements($values['class']))) {
117 throw new \Exception('ContextDefinition class must implement \Drupal\Core\Plugin\Context\ContextDefinitionInterface.');
119 $class = isset($values['class']) ? $values['class'] : 'Drupal\Core\Plugin\Context\ContextDefinition';
120 $this->definition = new $class($values['value'], $values['label'], $values['required'], $values['multiple'], $values['description'], $values['default_value']);
124 * Returns the value of an annotation.
126 * @return \Drupal\Core\Plugin\Context\ContextDefinitionInterface
128 public function get() {
129 return $this->definition;