3 namespace Drupal\Component\Plugin;
5 use Drupal\Component\Plugin\Context\ContextInterface;
8 * Interface for defining context aware plugins.
10 * Context aware plugins can specify an array of context definitions keyed by
11 * context name at the plugin definition under the "context" key.
15 interface ContextAwarePluginInterface extends PluginInspectionInterface {
18 * Gets the context definitions of the plugin.
20 * @return \Drupal\Component\Plugin\Context\ContextDefinitionInterface[]
21 * The array of context definitions, keyed by context name.
23 public function getContextDefinitions();
26 * Gets a specific context definition of the plugin.
29 * The name of the context in the plugin definition.
31 * @return \Drupal\Component\Plugin\Context\ContextDefinitionInterface
32 * The definition against which the context value must validate.
34 * @throws \Drupal\Component\Plugin\Exception\PluginException
35 * If the requested context is not defined.
37 public function getContextDefinition($name);
40 * Gets the defined contexts.
43 * The set context objects.
45 * @throws \Drupal\Component\Plugin\Exception\PluginException
46 * If contexts are defined but not set.
48 public function getContexts();
51 * Gets a defined context.
54 * The name of the context in the plugin definition.
56 * @return \Drupal\Component\Plugin\Context\ContextInterface
59 * @throws \Drupal\Component\Plugin\Exception\PluginException
60 * If the requested context is not set.
62 public function getContext($name);
65 * Gets the values for all defined contexts.
68 * An array of set context values, keyed by context name. If a context is
69 * unset its value is returned as NULL.
71 public function getContextValues();
74 * Gets the value for a defined context.
77 * The name of the context in the plugin configuration.
80 * The currently set context value.
82 * @throws \Drupal\Component\Plugin\Exception\PluginException
83 * If the requested context is not set.
85 public function getContextValue($name);
88 * Set a context on this plugin.
91 * The name of the context in the plugin configuration.
92 * @param \Drupal\Component\Plugin\Context\ContextInterface $context
93 * The context object to set.
95 public function setContext($name, ContextInterface $context);
98 * Sets the value for a defined context.
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.
106 * @return \Drupal\Component\Plugin\ContextAwarePluginInterface
107 * A context aware plugin object for chaining.
109 * @throws \Drupal\Component\Plugin\Exception\PluginException
110 * If the value does not pass validation.
112 public function setContextValue($name, $value);
115 * Validates the set values for the defined contexts.
117 * @return \Symfony\Component\Validator\ConstraintViolationListInterface
118 * A list of constraint violations. If the list is empty, validation
121 public function validateContexts();
124 * Gets a mapping of the expected assignment names to their context names.
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'.
132 public function getContextMapping();
135 * Sets a mapping of the expected assignment names to their context names.
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'.
145 public function setContextMapping(array $context_mapping);