3 namespace Drupal\Component\Plugin;
5 use Drupal\Component\Plugin\Context\ContextInterface;
6 use Drupal\Component\Plugin\Exception\ContextException;
7 use Drupal\Component\Plugin\Context\Context;
8 use Symfony\Component\Validator\ConstraintViolationList;
11 * Base class for plugins that are context aware.
13 abstract class ContextAwarePluginBase extends PluginBase implements ContextAwarePluginInterface {
16 * The data objects representing the context of this plugin.
18 * @var \Drupal\Component\Plugin\Context\ContextInterface[]
20 protected $context = [];
23 * Overrides \Drupal\Component\Plugin\PluginBase::__construct().
25 * Overrides the construction of context aware plugins to allow for
26 * unvalidated constructor based injection of contexts.
28 * @param array $configuration
29 * The plugin configuration, i.e. an array with configuration values keyed
30 * by configuration option name. The special key 'context' may be used to
31 * initialize the defined contexts by setting it to an array of context
32 * values keyed by context names.
33 * @param string $plugin_id
34 * The plugin_id for the plugin instance.
35 * @param mixed $plugin_definition
36 * The plugin implementation definition.
38 public function __construct(array $configuration, $plugin_id, $plugin_definition) {
39 $context_configuration = isset($configuration['context']) ? $configuration['context'] : [];
40 unset($configuration['context']);
42 parent::__construct($configuration, $plugin_id, $plugin_definition);
44 $this->contexts = $this->createContextFromConfiguration($context_configuration);
48 * Creates context objects from any context mappings in configuration.
50 * @param array $context_configuration
51 * An associative array of context names and values.
53 * @return \Drupal\Component\Plugin\Context\ContextInterface[]
54 * An array of context objects.
56 protected function createContextFromConfiguration(array $context_configuration) {
58 foreach ($context_configuration as $key => $value) {
59 $context_definition = $this->getContextDefinition($key);
60 $contexts[$key] = new Context($context_definition, $value);
68 public function getContextDefinitions() {
69 $definition = $this->getPluginDefinition();
70 return !empty($definition['context']) ? $definition['context'] : [];
76 public function getContextDefinition($name) {
77 $definition = $this->getPluginDefinition();
78 if (empty($definition['context'][$name])) {
79 throw new ContextException(sprintf("The %s context is not a valid context.", $name));
81 return $definition['context'][$name];
87 public function getContexts() {
88 // Make sure all context objects are initialized.
89 foreach ($this->getContextDefinitions() as $name => $definition) {
90 $this->getContext($name);
92 return $this->context;
98 public function getContext($name) {
99 // Check for a valid context value.
100 if (!isset($this->context[$name])) {
101 $this->context[$name] = new Context($this->getContextDefinition($name));
103 return $this->context[$name];
109 public function setContext($name, ContextInterface $context) {
110 $this->context[$name] = $context;
116 public function getContextValues() {
118 foreach ($this->getContextDefinitions() as $name => $definition) {
119 $values[$name] = isset($this->context[$name]) ? $this->context[$name]->getContextValue() : NULL;
127 public function getContextValue($name) {
128 return $this->getContext($name)->getContextValue();
134 public function setContextValue($name, $value) {
135 $this->context[$name] = new Context($this->getContextDefinition($name), $value);
142 public function validateContexts() {
143 $violations = new ConstraintViolationList();
144 // @todo: Implement symfony validator API to let the validator traverse
145 // and set property paths accordingly.
147 foreach ($this->getContexts() as $context) {
148 $violations->addAll($context->validate());