class Context in Service Container 7
Same name and namespace in other branches
- 7.2 lib/Drupal/Component/Plugin/Context/Context.php \Drupal\Component\Plugin\Context\Context
A generic context class for wrapping data a plugin needs to operate.
Hierarchy
- class \Drupal\Component\Plugin\Context\Context implements ContextInterface
Expanded class hierarchy of Context
1 file declares its use of Context
- ContextAwarePluginBase.php in lib/Drupal/ Component/ Plugin/ ContextAwarePluginBase.php 
- Contains \Drupal\Component\Plugin\ContextAwarePluginBase.
File
- lib/Drupal/ Component/ Plugin/ Context/ Context.php, line 17 
- Contains \Drupal\Component\Plugin\Context\Context.
Namespace
Drupal\Component\Plugin\ContextView 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 (bool) $this->contextValue || (bool) $this
      ->getContextDefinition()
      ->getDefaultValue();
  }
  /**
   * {@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 array(
      new Type($this->contextDefinition['class']),
    );
  }
  /**
   * {@inheritdoc}
   */
  public function validate() {
    $validator = Validation::createValidatorBuilder()
      ->getValidator();
    return $validator
      ->validateValue($this
      ->getContextValue(), $this
      ->getConstraints());
  }
}Members
| Name   | Modifiers | Type | Description | Overrides | 
|---|---|---|---|---|
| Context:: | protected | property | The definition to which a context must conform. | |
| Context:: | protected | property | The value of the context. | |
| Context:: | public | function | Gets a list of validation constraints. Overrides ContextInterface:: | |
| Context:: | public | function | Gets the provided definition that the context must conform to. Overrides ContextInterface:: | |
| Context:: | public | function | Gets the context value. Overrides ContextInterface:: | |
| Context:: | public | function | Returns whether the context has a value. Overrides ContextInterface:: | |
| Context:: | public | function | Validates the set context value. Overrides ContextInterface:: | |
| Context:: | public | function | Create a context object. | 
