ContextAwarePluginBase.php 4.36 KB
Newer Older
1 2 3 4
<?php

namespace Drupal\Component\Plugin;

5 6
use Drupal\Component\Plugin\Context\ContextInterface;
use Drupal\Component\Plugin\Exception\ContextException;
7
use Drupal\Component\Plugin\Context\Context;
8
use Symfony\Component\Validator\ConstraintViolationList;
9 10 11 12 13 14 15 16 17

/**
 * Base class for plugins that are context aware.
 */
abstract class ContextAwarePluginBase extends PluginBase implements ContextAwarePluginInterface {

  /**
   * The data objects representing the context of this plugin.
   *
18
   * @var \Drupal\Component\Plugin\Context\ContextInterface[]
19
   */
20
  protected $context = [];
21

22 23 24 25 26 27 28 29 30 31 32
  /**
   * Overrides \Drupal\Component\Plugin\PluginBase::__construct().
   *
   * Overrides the construction of context aware plugins to allow for
   * unvalidated constructor based injection of contexts.
   *
   * @param array $configuration
   *   The plugin configuration, i.e. an array with configuration values keyed
   *   by configuration option name. The special key 'context' may be used to
   *   initialize the defined contexts by setting it to an array of context
   *   values keyed by context names.
33 34 35 36
   * @param string $plugin_id
   *   The plugin_id for the plugin instance.
   * @param mixed $plugin_definition
   *   The plugin implementation definition.
37
   */
38
  public function __construct(array $configuration, $plugin_id, $plugin_definition) {
39 40 41
    $context_configuration = isset($configuration['context']) ? $configuration['context'] : [];
    unset($configuration['context']);

42
    parent::__construct($configuration, $plugin_id, $plugin_definition);
43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58

    $this->contexts = $this->createContextFromConfiguration($context_configuration);
  }

  /**
   * Creates context objects from any context mappings in configuration.
   *
   * @param array $context_configuration
   *   An associative array of context names and values.
   *
   * @return \Drupal\Component\Plugin\Context\ContextInterface[]
   *   An array of context objects.
   */
  protected function createContextFromConfiguration(array $context_configuration) {
    $contexts = [];
    foreach ($context_configuration as $key => $value) {
59
      $context_definition = $this->getContextDefinition($key);
60
      $contexts[$key] = new Context($context_definition, $value);
61
    }
62
    return $contexts;
63 64
  }

65
  /**
66
   * {@inheritdoc}
67 68
   */
  public function getContextDefinitions() {
69
    $definition = $this->getPluginDefinition();
70
    return !empty($definition['context']) ? $definition['context'] : array();
71 72 73
  }

  /**
74
   * {@inheritdoc}
75
   */
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));
80
    }
81
    return $definition['context'][$name];
82 83 84
  }

  /**
85
   * {@inheritdoc}
86 87
   */
  public function getContexts() {
88 89 90
    // Make sure all context objects are initialized.
    foreach ($this->getContextDefinitions() as $name => $definition) {
      $this->getContext($name);
91
    }
92
    return $this->context;
93 94 95
  }

  /**
96
   * {@inheritdoc}
97
   */
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));
102
    }
103
    return $this->context[$name];
104 105 106
  }

  /**
107 108 109 110 111 112 113 114
   * {@inheritdoc}
   */
  public function setContext($name, ContextInterface $context) {
    $this->context[$name] = $context;
  }

  /**
   * {@inheritdoc}
115 116
   */
  public function getContextValues() {
117 118 119
    $values = array();
    foreach ($this->getContextDefinitions() as $name => $definition) {
      $values[$name] = isset($this->context[$name]) ? $this->context[$name]->getContextValue() : NULL;
120
    }
121
    return $values;
122 123 124
  }

  /**
125
   * {@inheritdoc}
126
   */
127 128
  public function getContextValue($name) {
    return $this->getContext($name)->getContextValue();
129 130 131
  }

  /**
132
   * {@inheritdoc}
133
   */
134
  public function setContextValue($name, $value) {
135
    $this->context[$name] = new Context($this->getContextDefinition($name), $value);
136 137 138
    return $this;
  }

139
  /**
140
   * {@inheritdoc}
141 142 143 144 145 146
   */
  public function validateContexts() {
    $violations = new ConstraintViolationList();
    // @todo: Implement symfony validator API to let the validator traverse
    // and set property paths accordingly.

147 148
    foreach ($this->getContexts() as $context) {
      $violations->addAll($context->validate());
149 150 151 152
    }
    return $violations;
  }

153
}