3 namespace Drupal\Component\Plugin;
5 use Drupal\Component\Plugin\Context\ContextInterface;
6 use Drupal\Component\Plugin\Definition\ContextAwarePluginDefinitionInterface;
7 use Drupal\Component\Plugin\Exception\ContextException;
8 use Drupal\Component\Plugin\Context\Context;
9 use Symfony\Component\Validator\ConstraintViolationList;
12 * Base class for plugins that are context aware.
14 abstract class ContextAwarePluginBase extends PluginBase implements ContextAwarePluginInterface {
17 * The data objects representing the context of this plugin.
19 * @var \Drupal\Component\Plugin\Context\ContextInterface[]
21 protected $context = [];
24 * Overrides \Drupal\Component\Plugin\PluginBase::__construct().
26 * Overrides the construction of context aware plugins to allow for
27 * unvalidated constructor based injection of contexts.
29 * @param array $configuration
30 * The plugin configuration, i.e. an array with configuration values keyed
31 * by configuration option name. The special key 'context' may be used to
32 * initialize the defined contexts by setting it to an array of context
33 * values keyed by context names.
34 * @param string $plugin_id
35 * The plugin_id for the plugin instance.
36 * @param mixed $plugin_definition
37 * The plugin implementation definition.
39 public function __construct(array $configuration, $plugin_id, $plugin_definition) {
40 $context_configuration = isset($configuration['context']) ? $configuration['context'] : [];
41 unset($configuration['context']);
43 parent::__construct($configuration, $plugin_id, $plugin_definition);
45 $this->contexts = $this->createContextFromConfiguration($context_configuration);
49 * Creates context objects from any context mappings in configuration.
51 * @param array $context_configuration
52 * An associative array of context names and values.
54 * @return \Drupal\Component\Plugin\Context\ContextInterface[]
55 * An array of context objects.
57 protected function createContextFromConfiguration(array $context_configuration) {
59 foreach ($context_configuration as $key => $value) {
60 $context_definition = $this->getContextDefinition($key);
61 $contexts[$key] = new Context($context_definition, $value);
69 public function getContextDefinitions() {
70 $definition = $this->getPluginDefinition();
71 if ($definition instanceof ContextAwarePluginDefinitionInterface) {
72 return $definition->getContextDefinitions();
75 return !empty($definition['context']) ? $definition['context'] : [];
82 public function getContextDefinition($name) {
83 $definition = $this->getPluginDefinition();
84 if ($definition instanceof ContextAwarePluginDefinitionInterface) {
85 if ($definition->hasContextDefinition($name)) {
86 return $definition->getContextDefinition($name);
89 elseif (!empty($definition['context'][$name])) {
90 return $definition['context'][$name];
92 throw new ContextException(sprintf("The %s context is not a valid context.", $name));
98 public function getContexts() {
99 // Make sure all context objects are initialized.
100 foreach ($this->getContextDefinitions() as $name => $definition) {
101 $this->getContext($name);
103 return $this->context;
109 public function getContext($name) {
110 // Check for a valid context value.
111 if (!isset($this->context[$name])) {
112 $this->context[$name] = new Context($this->getContextDefinition($name));
114 return $this->context[$name];
120 public function setContext($name, ContextInterface $context) {
121 $this->context[$name] = $context;
127 public function getContextValues() {
129 foreach ($this->getContextDefinitions() as $name => $definition) {
130 $values[$name] = isset($this->context[$name]) ? $this->context[$name]->getContextValue() : NULL;
138 public function getContextValue($name) {
139 return $this->getContext($name)->getContextValue();
145 public function setContextValue($name, $value) {
146 $this->context[$name] = new Context($this->getContextDefinition($name), $value);
153 public function validateContexts() {
154 $violations = new ConstraintViolationList();
155 // @todo: Implement symfony validator API to let the validator traverse
156 // and set property paths accordingly.
158 foreach ($this->getContexts() as $context) {
159 $violations->addAll($context->validate());