function ContextAwarePluginTrait::getContext

Same name and namespace in other branches
  1. 10 core/lib/Drupal/Core/Plugin/ContextAwarePluginTrait.php \Drupal\Core\Plugin\ContextAwarePluginTrait::getContext()
  2. 11.x core/lib/Drupal/Core/Plugin/ContextAwarePluginTrait.php \Drupal\Core\Plugin\ContextAwarePluginTrait::getContext()

Return value

\Drupal\Core\Plugin\Context\ContextInterface The context object.

2 calls to ContextAwarePluginTrait::getContext()
ContextAwarePluginTrait::getContexts in core/lib/Drupal/Core/Plugin/ContextAwarePluginTrait.php
ContextAwarePluginTrait::setContextValue in core/lib/Drupal/Core/Plugin/ContextAwarePluginTrait.php

File

core/lib/Drupal/Core/Plugin/ContextAwarePluginTrait.php, line 60

Class

ContextAwarePluginTrait
Provides a trait to add context-aware functionality to plugins.

Namespace

Drupal\Core\Plugin

Code

public function getContext($name) {
    // @todo Remove this entire block in Drupal 10.0.x.
    //   See https://www.drupal.org/project/drupal/issues/3153956.
    if (!$this->initializedContextConfig) {
        $this->initializedContextConfig = TRUE;
        if ($this instanceof ConfigurableInterface) {
            $configuration = $this->getConfiguration();
        }
        else {
            $reflection = new \ReflectionProperty($this, 'configuration');
            $reflection->setAccessible(TRUE);
            $configuration = $reflection->getValue($this);
        }
        if (isset($configuration['context'])) {
            @trigger_error('Passing context values to plugins via configuration is deprecated in drupal:9.1.0 and will be removed before drupal:10.0.0. Instead, call ::setContextValue() on the plugin itself. See https://www.drupal.org/node/3120980', E_USER_DEPRECATED);
            foreach ($configuration['context'] as $key => $value) {
                $context_definition = $this->getContextDefinition($key);
                $this->context[$key] = new Context($context_definition, $value);
            }
        }
    }
    // Check for a valid context value.
    if (!isset($this->context[$name])) {
        $this->context[$name] = new Context($this->getContextDefinition($name));
    }
    return $this->context[$name];
}

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