class ContextAwarePluginBase

Same name in this branch
  1. 8.9.x core/lib/Drupal/Core/Plugin/ContextAwarePluginBase.php \Drupal\Core\Plugin\ContextAwarePluginBase
Same name in other branches
  1. 9 core/lib/Drupal/Core/Plugin/ContextAwarePluginBase.php \Drupal\Core\Plugin\ContextAwarePluginBase
  2. 9 core/lib/Drupal/Component/Plugin/ContextAwarePluginBase.php \Drupal\Component\Plugin\ContextAwarePluginBase

Base class for plugins that are context aware.

Hierarchy

Expanded class hierarchy of ContextAwarePluginBase

1 file declares its use of ContextAwarePluginBase
ContextAwarePluginBase.php in core/lib/Drupal/Core/Plugin/ContextAwarePluginBase.php

File

core/lib/Drupal/Component/Plugin/ContextAwarePluginBase.php, line 14

Namespace

Drupal\Component\Plugin
View source
abstract class ContextAwarePluginBase extends PluginBase implements ContextAwarePluginInterface {
    
    /**
     * The data objects representing the context of this plugin.
     *
     * @var \Drupal\Component\Plugin\Context\ContextInterface[]
     */
    protected $context = [];
    
    /**
     * Data objects representing the contexts passed in the plugin configuration.
     *
     * @var \Drupal\Component\Plugin\Context\ContextInterface[]
     *
     * @deprecated
     *   in drupal:8.8.0 and is removed from drupal:9.0.0. Use
     *   \Drupal\Component\Plugin\ContextAwarePluginInterface instead.
     *
     * @see https://www.drupal.org/project/drupal/issues/3080631
     */
    private $contexts = [];
    
    /**
     * 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.
     */
    public function __construct(array $configuration, $plugin_id, $plugin_definition) {
        $context_configuration = isset($configuration['context']) ? $configuration['context'] : [];
        unset($configuration['context']);
        parent::__construct($configuration, $plugin_id, $plugin_definition);
        $this->context = $this->createContextFromConfiguration($context_configuration);
        // @todo Remove $this->contexts in Drupal 9; see
        // https://www.drupal.org/project/drupal/issues/3081145
        $this->contexts = $this->context;
    }
    
    /**
     * Implements magic __get() method.
     */
    public function __get($name) {
        if ($name === 'contexts') {
            @trigger_error('The $contexts property is deprecated in Drupal 8.8.0 and will be removed before Drupal 9.0.0. Use methods of \\Drupal\\Component\\Plugin\\ContextAwarePluginInterface instead. See https://www.drupal.org/project/drupal/issues/3080631 for more information.', E_USER_DEPRECATED);
            return $this->contexts;
        }
    }
    
    /**
     * 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) {
            $context_definition = $this->getContextDefinition($key);
            $contexts[$key] = new Context($context_definition, $value);
        }
        return $contexts;
    }
    
    /**
     * {@inheritdoc}
     */
    public function getContextDefinitions() {
        $definition = $this->getPluginDefinition();
        if ($definition instanceof ContextAwarePluginDefinitionInterface) {
            return $definition->getContextDefinitions();
        }
        else {
            return !empty($definition['context_definitions']) ? $definition['context_definitions'] : [];
        }
    }
    
    /**
     * {@inheritdoc}
     */
    public function getContextDefinition($name) {
        $definition = $this->getPluginDefinition();
        if ($definition instanceof ContextAwarePluginDefinitionInterface) {
            if ($definition->hasContextDefinition($name)) {
                return $definition->getContextDefinition($name);
            }
        }
        elseif (!empty($definition['context_definitions'][$name])) {
            return $definition['context_definitions'][$name];
        }
        throw new ContextException(sprintf("The %s context is not a valid context.", $name));
    }
    
    /**
     * {@inheritdoc}
     */
    public function getContexts() {
        // Make sure all context objects are initialized.
        foreach ($this->getContextDefinitions() as $name => $definition) {
            $this->getContext($name);
        }
        return $this->context;
    }
    
    /**
     * {@inheritdoc}
     */
    public function getContext($name) {
        // Check for a valid context value.
        if (!isset($this->context[$name])) {
            $this->context[$name] = new Context($this->getContextDefinition($name));
        }
        return $this->context[$name];
    }
    
    /**
     * {@inheritdoc}
     */
    public function setContext($name, ContextInterface $context) {
        $this->context[$name] = $context;
    }
    
    /**
     * {@inheritdoc}
     */
    public function getContextValues() {
        $values = [];
        foreach ($this->getContextDefinitions() as $name => $definition) {
            $values[$name] = isset($this->context[$name]) ? $this->context[$name]
                ->getContextValue() : NULL;
        }
        return $values;
    }
    
    /**
     * {@inheritdoc}
     */
    public function getContextValue($name) {
        return $this->getContext($name)
            ->getContextValue();
    }
    
    /**
     * {@inheritdoc}
     */
    public function setContextValue($name, $value) {
        $this->context[$name] = new Context($this->getContextDefinition($name), $value);
        return $this;
    }
    
    /**
     * {@inheritdoc}
     */
    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());
        }
        return $violations;
    }

}

Members

Title Sort descending Deprecated Modifiers Object type Summary Overriden Title Overrides
ContextAwarePluginBase::$context protected property The data objects representing the context of this plugin.
ContextAwarePluginBase::$contexts Deprecated private property Data objects representing the contexts passed in the plugin configuration.
ContextAwarePluginBase::createContextFromConfiguration protected function Creates context objects from any context mappings in configuration. 1
ContextAwarePluginBase::getContext public function Gets a defined context. Overrides ContextAwarePluginInterface::getContext 1
ContextAwarePluginBase::getContextDefinition public function Gets a specific context definition of the plugin. Overrides ContextAwarePluginInterface::getContextDefinition 1
ContextAwarePluginBase::getContextDefinitions public function Gets the context definitions of the plugin. Overrides ContextAwarePluginInterface::getContextDefinitions 1
ContextAwarePluginBase::getContexts public function Gets the defined contexts. Overrides ContextAwarePluginInterface::getContexts
ContextAwarePluginBase::getContextValue public function Gets the value for a defined context. Overrides ContextAwarePluginInterface::getContextValue
ContextAwarePluginBase::getContextValues public function Gets the values for all defined contexts. Overrides ContextAwarePluginInterface::getContextValues
ContextAwarePluginBase::setContext public function Set a context on this plugin. Overrides ContextAwarePluginInterface::setContext 1
ContextAwarePluginBase::setContextValue public function Sets the value for a defined context. Overrides ContextAwarePluginInterface::setContextValue 1
ContextAwarePluginBase::validateContexts public function Validates the set values for the defined contexts. Overrides ContextAwarePluginInterface::validateContexts
ContextAwarePluginBase::__construct public function Overrides \Drupal\Component\Plugin\PluginBase::__construct(). Overrides PluginBase::__construct 4
ContextAwarePluginBase::__get public function Implements magic __get() method.
ContextAwarePluginInterface::getContextMapping public function Gets a mapping of the expected assignment names to their context names. 1
ContextAwarePluginInterface::setContextMapping public function Sets a mapping of the expected assignment names to their context names. 1
PluginBase::$configuration protected property Configuration information passed into the plugin.
PluginBase::$pluginDefinition protected property The plugin implementation definition.
PluginBase::$pluginId protected property The plugin_id.
PluginBase::DERIVATIVE_SEPARATOR constant A string which is used to separate base plugin IDs from the derivative ID.
PluginBase::getBaseId public function Gets the base_plugin_id of the plugin instance. Overrides DerivativeInspectionInterface::getBaseId
PluginBase::getDerivativeId public function Gets the derivative_id of the plugin instance. Overrides DerivativeInspectionInterface::getDerivativeId
PluginBase::getPluginDefinition public function Gets the definition of the plugin implementation. Overrides PluginInspectionInterface::getPluginDefinition
PluginBase::getPluginId public function Gets the plugin_id of the plugin instance. Overrides PluginInspectionInterface::getPluginId
PluginBase::isConfigurable public function Determines if the plugin is configurable.

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.