You are here

trait ContextProviderTrait in Rules 8.3

A trait implementing the ContextProviderInterface.

This trait is intended for context aware plugins that want to provide context.

The trait requires the plugin to use configuration as defined by the ContextConfig class.

Hierarchy

See also

\Drupal\rules\Context\ContextProviderInterface

2 files declare their use of ContextProviderTrait
RulesActionBase.php in src/Core/RulesActionBase.php
RulesConditionBase.php in src/Core/RulesConditionBase.php

File

src/Context/ContextProviderTrait.php, line 19

Namespace

Drupal\rules\Context
View source
trait ContextProviderTrait {

  /**
   * The data objects that are provided by this plugin.
   *
   * @var \Drupal\Component\Plugin\Context\ContextInterface[]
   */
  protected $providedContext;

  /**
   * @see \Drupal\rules\Context\ContextProviderInterface
   */
  public function setProvidedValue($name, $value) {
    $context = $this
      ->getProvidedContext($name);
    $new_context = Context::createFromContext($context, $value);
    $this->providedContext[$name] = $new_context;
    return $this;
  }

  /**
   * @see \Drupal\rules\Context\ContextProviderInterface
   */
  public function getProvidedContext($name) {

    // Check for a valid context value.
    if (!isset($this->providedContext[$name])) {
      $this->providedContext[$name] = new Context($this
        ->getProvidedContextDefinition($name));
    }
    return $this->providedContext[$name];
  }

  /**
   * @see \Drupal\rules\Context\ContextProviderInterface
   */
  public function getProvidedContextDefinition($name) {
    $definition = $this
      ->getPluginDefinition();
    if (empty($definition['provides'][$name])) {
      throw new ContextException(sprintf("The provided context '%s' is not valid.", $name));
    }
    return $definition['provides'][$name];
  }

  /**
   * @see \Drupal\rules\Context\ContextProviderInterface
   */
  public function getProvidedContextDefinitions() {
    $definition = $this
      ->getPluginDefinition();
    return !empty($definition['provides']) ? $definition['provides'] : [];
  }

}

Members