90ee661201b0f0cba09ef96d9f7639b59cb8b28b
[yaffs-website] / web / core / lib / Drupal / Component / Plugin / ContextAwarePluginInterface.php
1 <?php
2
3 namespace Drupal\Component\Plugin;
4
5 use Drupal\Component\Plugin\Context\ContextInterface;
6
7 /**
8  * Interface for defining context aware plugins.
9  *
10  * Context aware plugins can specify an array of context definitions keyed by
11  * context name at the plugin definition under the "context" key.
12  *
13  * @ingroup plugin_api
14  */
15 interface ContextAwarePluginInterface extends PluginInspectionInterface {
16
17   /**
18    * Gets the context definitions of the plugin.
19    *
20    * @return \Drupal\Component\Plugin\Context\ContextDefinitionInterface[]
21    *   The array of context definitions, keyed by context name.
22    */
23   public function getContextDefinitions();
24
25   /**
26    * Gets a specific context definition of the plugin.
27    *
28    * @param string $name
29    *   The name of the context in the plugin definition.
30    *
31    * @return \Drupal\Component\Plugin\Context\ContextDefinitionInterface
32    *   The definition against which the context value must validate.
33    *
34    * @throws \Drupal\Component\Plugin\Exception\PluginException
35    *   If the requested context is not defined.
36    */
37   public function getContextDefinition($name);
38
39   /**
40    * Gets the defined contexts.
41    *
42    * @return array
43    *   The set context objects.
44    *
45    * @throws \Drupal\Component\Plugin\Exception\PluginException
46    *   If contexts are defined but not set.
47    */
48   public function getContexts();
49
50   /**
51    * Gets a defined context.
52    *
53    * @param string $name
54    *   The name of the context in the plugin definition.
55    *
56    * @return \Drupal\Component\Plugin\Context\ContextInterface
57    *   The context object.
58    *
59    * @throws \Drupal\Component\Plugin\Exception\PluginException
60    *   If the requested context is not set.
61    */
62   public function getContext($name);
63
64   /**
65    * Gets the values for all defined contexts.
66    *
67    * @return array
68    *   An array of set context values, keyed by context name. If a context is
69    *   unset its value is returned as NULL.
70    */
71   public function getContextValues();
72
73   /**
74    * Gets the value for a defined context.
75    *
76    * @param string $name
77    *   The name of the context in the plugin configuration.
78    *
79    * @return mixed
80    *   The currently set context value.
81    *
82    * @throws \Drupal\Component\Plugin\Exception\PluginException
83    *   If the requested context is not set.
84    */
85   public function getContextValue($name);
86
87   /**
88    * Set a context on this plugin.
89    *
90    * @param string $name
91    *   The name of the context in the plugin configuration.
92    * @param \Drupal\Component\Plugin\Context\ContextInterface $context
93    *   The context object to set.
94    */
95   public function setContext($name, ContextInterface $context);
96
97   /**
98    * Sets the value for a defined context.
99    *
100    * @param string $name
101    *   The name of the context in the plugin definition.
102    * @param mixed $value
103    *   The value to set the context to. The value has to validate against the
104    *   provided context definition.
105    *
106    * @return \Drupal\Component\Plugin\ContextAwarePluginInterface
107    *   A context aware plugin object for chaining.
108    *
109    * @throws \Drupal\Component\Plugin\Exception\PluginException
110    *   If the value does not pass validation.
111    */
112   public function setContextValue($name, $value);
113
114   /**
115    * Validates the set values for the defined contexts.
116    *
117    * @return \Symfony\Component\Validator\ConstraintViolationListInterface
118    *   A list of constraint violations. If the list is empty, validation
119    *   succeeded.
120    */
121   public function validateContexts();
122
123   /**
124    * Gets a mapping of the expected assignment names to their context names.
125    *
126    * @return array
127    *   A mapping of the expected assignment names to their context names. For
128    *   example, if one of the $contexts is named 'user.current_user', but the
129    *   plugin expects a context named 'user', then this map would contain
130    *   'user' => 'user.current_user'.
131    */
132   public function getContextMapping();
133
134   /**
135    * Sets a mapping of the expected assignment names to their context names.
136    *
137    * @param array $context_mapping
138    *   A mapping of the expected assignment names to their context names. For
139    *   example, if one of the $contexts is named 'user.current_user', but the
140    *   plugin expects a context named 'user', then this map would contain
141    *   'user' => 'user.current_user'.
142    *
143    * @return $this
144    */
145   public function setContextMapping(array $context_mapping);
146
147 }