Mercurial > hg > cmmr2012-drupal-site
comparison core/lib/Drupal/Core/Annotation/ContextDefinition.php @ 5:12f9dff5fda9 tip
Update to Drupal core 8.7.1
author | Chris Cannam |
---|---|
date | Thu, 09 May 2019 15:34:47 +0100 |
parents | a9cd425dd02b |
children |
comparison
equal
deleted
inserted
replaced
4:a9cd425dd02b | 5:12f9dff5fda9 |
---|---|
12 * | 12 * |
13 * When providing plugin annotations, contexts can be defined to support UI | 13 * When providing plugin annotations, contexts can be defined to support UI |
14 * interactions through providing limits, and mapping contexts to appropriate | 14 * interactions through providing limits, and mapping contexts to appropriate |
15 * plugins. Context definitions can be provided as such: | 15 * plugins. Context definitions can be provided as such: |
16 * @code | 16 * @code |
17 * context = { | 17 * context_definitions = { |
18 * "node" = @ContextDefinition("entity:node") | 18 * "node" = @ContextDefinition("entity:node") |
19 * } | 19 * } |
20 * @endcode | 20 * @endcode |
21 * | 21 * |
22 * To add a label to a context definition use the "label" key: | 22 * To add a label to a context definition use the "label" key: |
23 * @code | 23 * @code |
24 * context = { | 24 * context_definitions = { |
25 * "node" = @ContextDefinition("entity:node", label = @Translation("Node")) | 25 * "node" = @ContextDefinition("entity:node", label = @Translation("Node")) |
26 * } | 26 * } |
27 * @endcode | 27 * @endcode |
28 * | 28 * |
29 * Contexts are required unless otherwise specified. To make an optional | 29 * Contexts are required unless otherwise specified. To make an optional |
30 * context use the "required" key: | 30 * context use the "required" key: |
31 * @code | 31 * @code |
32 * context = { | 32 * context_definitions = { |
33 * "node" = @ContextDefinition("entity:node", required = FALSE, label = @Translation("Node")) | 33 * "node" = @ContextDefinition("entity:node", required = FALSE, label = @Translation("Node")) |
34 * } | 34 * } |
35 * @endcode | 35 * @endcode |
36 * | 36 * |
37 * To define multiple contexts, simply provide different key names in the | 37 * To define multiple contexts, simply provide different key names in the |
38 * context array: | 38 * context array: |
39 * @code | 39 * @code |
40 * context = { | 40 * context_definitions = { |
41 * "artist" = @ContextDefinition("entity:node", label = @Translation("Artist")), | 41 * "artist" = @ContextDefinition("entity:node", label = @Translation("Artist")), |
42 * "album" = @ContextDefinition("entity:node", label = @Translation("Album")) | 42 * "album" = @ContextDefinition("entity:node", label = @Translation("Album")) |
43 * } | 43 * } |
44 * @endcode | 44 * @endcode |
45 * | 45 * |
46 * Specifying a default value for the context definition: | 46 * Specifying a default value for the context definition: |
47 * @code | 47 * @code |
48 * context = { | 48 * context_definitions = { |
49 * "message" = @ContextDefinition("string", | 49 * "message" = @ContextDefinition("string", |
50 * label = @Translation("Message"), | 50 * label = @Translation("Message"), |
51 * default_value = @Translation("Checkout complete! Thank you for your purchase.") | 51 * default_value = @Translation("Checkout complete! Thank you for your purchase.") |
52 * ) | 52 * ) |
53 * } | 53 * } |
117 throw new \Exception('ContextDefinition class must implement \Drupal\Core\Plugin\Context\ContextDefinitionInterface.'); | 117 throw new \Exception('ContextDefinition class must implement \Drupal\Core\Plugin\Context\ContextDefinitionInterface.'); |
118 } | 118 } |
119 | 119 |
120 $class = $this->getDefinitionClass($values); | 120 $class = $this->getDefinitionClass($values); |
121 $this->definition = new $class($values['value'], $values['label'], $values['required'], $values['multiple'], $values['description'], $values['default_value']); | 121 $this->definition = new $class($values['value'], $values['label'], $values['required'], $values['multiple'], $values['description'], $values['default_value']); |
122 | |
123 if (isset($values['constraints'])) { | |
124 foreach ($values['constraints'] as $constraint_name => $options) { | |
125 $this->definition->addConstraint($constraint_name, $options); | |
126 } | |
127 } | |
122 } | 128 } |
123 | 129 |
124 /** | 130 /** |
125 * Determines the context definition class to use. | 131 * Determines the context definition class to use. |
126 * | 132 * |