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

/**
 * @file
5
 * Contains \Drupal\Component\Plugin\ContextAwarePluginBase.
6 7 8 9
 */

namespace Drupal\Component\Plugin;

10 11
use Drupal\Component\Plugin\Context\ContextInterface;
use Drupal\Component\Plugin\Exception\ContextException;
12
use Drupal\Component\Plugin\Context\Context;
13
use Symfony\Component\Validator\ConstraintViolationList;
14 15 16 17 18 19 20 21 22

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

  /**
   * The data objects representing the context of this plugin.
   *
23
   * @var \Drupal\Component\Plugin\Context\ContextInterface[]
24
   */
25
  protected $context = [];
26

27 28 29 30 31 32 33 34 35 36 37
  /**
   * 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.
38 39 40 41
   * @param string $plugin_id
   *   The plugin_id for the plugin instance.
   * @param mixed $plugin_definition
   *   The plugin implementation definition.
42
   */
43
  public function __construct(array $configuration, $plugin_id, $plugin_definition) {
44 45 46 47 48
    $context = array();
    if (isset($configuration['context'])) {
      $context = $configuration['context'];
      unset($configuration['context']);
    }
49
    parent::__construct($configuration, $plugin_id, $plugin_definition);
50 51 52 53 54 55 56
    foreach ($context as $key => $value) {
      $context_definition = $this->getContextDefinition($key);
      $this->context[$key] = new Context($context_definition);
      $this->context[$key]->setContextValue($value);
    }
  }

57
  /**
58
   * {@inheritdoc}
59 60
   */
  public function getContextDefinitions() {
61
    $definition = $this->getPluginDefinition();
62
    return !empty($definition['context']) ? $definition['context'] : array();
63 64 65
  }

  /**
66
   * {@inheritdoc}
67
   */
68
  public function getContextDefinition($name) {
69
    $definition = $this->getPluginDefinition();
70
    if (empty($definition['context'][$name])) {
71
      throw new ContextException(sprintf("The %s context is not a valid context.", $name));
72
    }
73
    return $definition['context'][$name];
74 75 76
  }

  /**
77
   * {@inheritdoc}
78 79
   */
  public function getContexts() {
80 81 82
    // Make sure all context objects are initialized.
    foreach ($this->getContextDefinitions() as $name => $definition) {
      $this->getContext($name);
83
    }
84
    return $this->context;
85 86 87
  }

  /**
88
   * {@inheritdoc}
89
   */
90
  public function getContext($name) {
91
    // Check for a valid context value.
92
    if (!isset($this->context[$name])) {
93
      $this->context[$name] = new Context($this->getContextDefinition($name));
94
    }
95
    return $this->context[$name];
96 97 98
  }

  /**
99 100 101 102 103 104 105 106
   * {@inheritdoc}
   */
  public function setContext($name, ContextInterface $context) {
    $this->context[$name] = $context;
  }

  /**
   * {@inheritdoc}
107 108
   */
  public function getContextValues() {
109 110 111
    $values = array();
    foreach ($this->getContextDefinitions() as $name => $definition) {
      $values[$name] = isset($this->context[$name]) ? $this->context[$name]->getContextValue() : NULL;
112
    }
113
    return $values;
114 115 116
  }

  /**
117
   * {@inheritdoc}
118
   */
119 120
  public function getContextValue($name) {
    return $this->getContext($name)->getContextValue();
121 122 123
  }

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

131
  /**
132
   * {@inheritdoc}
133 134 135 136 137 138
   */
  public function validateContexts() {
    $violations = new ConstraintViolationList();
    // @todo: Implement symfony validator API to let the validator traverse
    // and set property paths accordingly.

139 140
    foreach ($this->getContexts() as $context) {
      $violations->addAll($context->validate());
141 142 143 144
    }
    return $violations;
  }

145
}