ContextProviderTrait.php in Rules 8.3
File
src/Context/ContextProviderTrait.php
View source
<?php
namespace Drupal\rules\Context;
use Drupal\Component\Plugin\Exception\ContextException;
use Drupal\Core\Plugin\Context\Context;
trait ContextProviderTrait {
protected $providedContext;
public function setProvidedValue($name, $value) {
$context = $this
->getProvidedContext($name);
$new_context = Context::createFromContext($context, $value);
$this->providedContext[$name] = $new_context;
return $this;
}
public function getProvidedContext($name) {
if (!isset($this->providedContext[$name])) {
$this->providedContext[$name] = new Context($this
->getProvidedContextDefinition($name));
}
return $this->providedContext[$name];
}
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];
}
public function getProvidedContextDefinitions() {
$definition = $this
->getPluginDefinition();
return !empty($definition['provides']) ? $definition['provides'] : [];
}
}