Skip to main content
Drupal API
User account menu
  • Log in

Breadcrumb

  1. Drupal Core 11.1.x
  2. Context.php

class Context

Same name in this branch
  1. 11.1.x vendor/open-telemetry/api/Instrumentation/AutoInstrumentation/Context.php \OpenTelemetry\API\Instrumentation\AutoInstrumentation\Context
  2. 11.1.x vendor/open-telemetry/context/Context.php \OpenTelemetry\Context\Context
  3. 11.1.x vendor/sebastian/recursion-context/src/Context.php \SebastianBergmann\RecursionContext\Context
  4. 11.1.x vendor/symfony/serializer/Attribute/Context.php \Symfony\Component\Serializer\Attribute\Context
  5. 11.1.x vendor/phpdocumentor/type-resolver/src/Types/Context.php \phpDocumentor\Reflection\Types\Context
  6. 11.1.x core/lib/Drupal/Core/Plugin/Context/Context.php \Drupal\Core\Plugin\Context\Context

A generic context class for wrapping data a plugin needs to operate.

Hierarchy

  • class \Drupal\Component\Plugin\Context\Context implements \Drupal\Component\Plugin\Context\ContextInterface

Expanded class hierarchy of Context

1 file declares its use of Context
Context.php in core/lib/Drupal/Core/Plugin/Context/Context.php
18 string references to 'Context'
CompiledUrlMatcherDumper::compileRoute in vendor/symfony/routing/Matcher/Dumper/CompiledUrlMatcherDumper.php
Compiles a single Route to PHP code used to match it against the path info.
ConditionManagerCreateInstanceContextConfigurationRule::processNode in vendor/mglaman/phpstan-drupal/src/Rules/Deprecations/ConditionManagerCreateInstanceContextConfigurationRule.php
LocaleConfigManager::processTranslatableData in core/modules/locale/src/LocaleConfigManager.php
Process the translatable data array with a given language.
LocaleConfigSubscriber::processTranslatableData in core/modules/locale/src/LocaleConfigSubscriber.php
Process the translatable data array with a given language.
LocaleConfigSubscriber::resetExistingTranslations in core/modules/locale/src/LocaleConfigSubscriber.php
Reset existing locale translations to their source values.

... See full list

File

core/lib/Drupal/Component/Plugin/Context/Context.php, line 12

Namespace

Drupal\Component\Plugin\Context
View source
class Context implements ContextInterface {
    
    /**
     * The value of the context.
     *
     * @var mixed
     */
    protected $contextValue;
    
    /**
     * The definition to which a context must conform.
     *
     * @var \Drupal\Component\Plugin\Context\ContextDefinitionInterface
     */
    protected $contextDefinition;
    
    /**
     * Create a context object.
     *
     * @param \Drupal\Component\Plugin\Context\ContextDefinitionInterface $context_definition
     *   The context definition.
     * @param mixed|null $context_value
     *   The value of the context.
     */
    public function __construct(ContextDefinitionInterface $context_definition, $context_value = NULL) {
        $this->contextDefinition = $context_definition;
        $this->contextValue = $context_value;
    }
    
    /**
     * {@inheritdoc}
     */
    public function getContextValue() {
        // Support optional contexts.
        if (!isset($this->contextValue)) {
            $definition = $this->getContextDefinition();
            $default_value = $definition->getDefaultValue();
            if (!isset($default_value) && $definition->isRequired()) {
                $type = $definition->getDataType();
                throw new ContextException(sprintf("The %s context is required and not present.", $type));
            }
            // Keep the default value here so that subsequent calls don't have to look
            // it up again.
            $this->contextValue = $default_value;
        }
        return $this->contextValue;
    }
    
    /**
     * {@inheritdoc}
     */
    public function hasContextValue() {
        return $this->contextValue !== NULL || $this->getContextDefinition()
            ->getDefaultValue() !== NULL;
    }
    
    /**
     * {@inheritdoc}
     */
    public function getContextDefinition() {
        return $this->contextDefinition;
    }
    
    /**
     * {@inheritdoc}
     */
    public function getConstraints() {
        if (empty($this->contextDefinition['class'])) {
            throw new ContextException("An error was encountered while trying to validate the context.");
        }
        return [
            new Type($this->contextDefinition['class']),
        ];
    }
    
    /**
     * {@inheritdoc}
     */
    public function validate() {
        $validator = Validation::createValidatorBuilder()->getValidator();
        return $validator->validateValue($this->getContextValue(), $this->getConstraints());
    }

}

Members

Title Sort descending Modifiers Object type Summary Overriden Title Overrides
Context::$contextDefinition protected property The definition to which a context must conform. 1
Context::$contextValue protected property The value of the context.
Context::getConstraints public function Gets a list of validation constraints. Overrides ContextInterface::getConstraints 1
Context::getContextDefinition public function Gets the provided definition that the context must conform to. Overrides ContextInterface::getContextDefinition 1
Context::getContextValue public function Gets the context value. Overrides ContextInterface::getContextValue 1
Context::hasContextValue public function Returns whether the context has a value. Overrides ContextInterface::hasContextValue 1
Context::validate public function Validates the set context value. Overrides ContextInterface::validate 1
Context::__construct public function Create a context object. 1
RSS feed
Powered by Drupal