You are here

public function ContextAwarePluginTrait::getContext in Drupal 9

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];
}