Newer
Older
Dries Buytaert
committed
<?php
/**
* @file
* Contains \Drupal\Component\Plugin\ContextAwarePluginBase.
Dries Buytaert
committed
*/
namespace Drupal\Component\Plugin;
use Drupal\Component\Plugin\Context\ContextInterface;
use Drupal\Component\Plugin\Exception\ContextException;
Dries Buytaert
committed
use Drupal\Component\Plugin\Context\Context;
Angie Byron
committed
use Symfony\Component\Validator\ConstraintViolationList;
Dries Buytaert
committed
/**
* Base class for plugins that are context aware.
*/
abstract class ContextAwarePluginBase extends PluginBase implements ContextAwarePluginInterface {
/**
* The data objects representing the context of this plugin.
*
* @var \Drupal\Component\Plugin\Context\ContextInterface[]
Dries Buytaert
committed
*/
protected $context;
Angie Byron
committed
/**
* 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.
* @param string $plugin_id
* The plugin_id for the plugin instance.
* @param mixed $plugin_definition
* The plugin implementation definition.
Angie Byron
committed
*/
public function __construct(array $configuration, $plugin_id, $plugin_definition) {
Angie Byron
committed
$context = array();
if (isset($configuration['context'])) {
$context = $configuration['context'];
unset($configuration['context']);
}
Angie Byron
committed
parent::__construct($configuration, $plugin_id, $plugin_definition);
Angie Byron
committed
foreach ($context as $key => $value) {
$context_definition = $this->getContextDefinition($key);
$this->context[$key] = new Context($context_definition);
$this->context[$key]->setContextValue($value);
}
}
Dries Buytaert
committed
/**
* {@inheritdoc}
Dries Buytaert
committed
*/
public function getContextDefinitions() {
Alex Pott
committed
$definition = $this->getPluginDefinition();
Angie Byron
committed
return !empty($definition['context']) ? $definition['context'] : array();
Dries Buytaert
committed
}
/**
* {@inheritdoc}
Dries Buytaert
committed
*/
Angie Byron
committed
public function getContextDefinition($name) {
Alex Pott
committed
$definition = $this->getPluginDefinition();
Angie Byron
committed
if (empty($definition['context'][$name])) {
throw new ContextException(sprintf("The %s context is not a valid context.", $name));
Dries Buytaert
committed
}
Angie Byron
committed
return $definition['context'][$name];
Dries Buytaert
committed
}
/**
* {@inheritdoc}
Dries Buytaert
committed
*/
public function getContexts() {
// Make sure all context objects are initialized.
foreach ($this->getContextDefinitions() as $name => $definition) {
$this->getContext($name);
Dries Buytaert
committed
}
return $this->context;
Dries Buytaert
committed
}
/**
* {@inheritdoc}
Dries Buytaert
committed
*/
Angie Byron
committed
public function getContext($name) {
Dries Buytaert
committed
// Check for a valid context value.
Angie Byron
committed
if (!isset($this->context[$name])) {
$this->context[$name] = new Context($this->getContextDefinition($name));
Dries Buytaert
committed
}
Angie Byron
committed
return $this->context[$name];
Dries Buytaert
committed
}
/**
* {@inheritdoc}
*/
public function setContext($name, ContextInterface $context) {
$this->context[$name] = $context;
}
/**
* {@inheritdoc}
Dries Buytaert
committed
*/
public function getContextValues() {
Angie Byron
committed
$values = array();
foreach ($this->getContextDefinitions() as $name => $definition) {
$values[$name] = isset($this->context[$name]) ? $this->context[$name]->getContextValue() : NULL;
Dries Buytaert
committed
}
Angie Byron
committed
return $values;
Dries Buytaert
committed
}
/**
* {@inheritdoc}
Dries Buytaert
committed
*/
Angie Byron
committed
public function getContextValue($name) {
return $this->getContext($name)->getContextValue();
Dries Buytaert
committed
}
/**
* {@inheritdoc}
Dries Buytaert
committed
*/
Angie Byron
committed
public function setContextValue($name, $value) {
$this->getContext($name)->setContextValue($value);
Dries Buytaert
committed
return $this;
}
Angie Byron
committed
/**
* {@inheritdoc}
Angie Byron
committed
*/
public function validateContexts() {
$violations = new ConstraintViolationList();
// @todo: Implement symfony validator API to let the validator traverse
// and set property paths accordingly.
foreach ($this->getContexts() as $context) {
$violations->addAll($context->validate());
Angie Byron
committed
}
return $violations;
}