3 namespace Drupal\Component\Plugin\Definition;
5 use Drupal\Component\Plugin\Context\ContextDefinitionInterface;
6 use Drupal\Component\Plugin\Exception\ContextException;
9 * Provides a trait for context-aware object-based plugin definitions.
11 trait ContextAwarePluginDefinitionTrait {
14 * The context definitions for this plugin definition.
16 * @var \Drupal\Component\Plugin\Context\ContextDefinitionInterface[]
18 protected $contextDefinitions = [];
21 * Implements \Drupal\Component\Plugin\Definition\ContextAwarePluginDefinitionInterface::hasContextDefinition().
23 public function hasContextDefinition($name) {
24 return array_key_exists($name, $this->contextDefinitions);
28 * Implements \Drupal\Component\Plugin\Definition\ContextAwarePluginDefinitionInterface::getContextDefinitions().
30 public function getContextDefinitions() {
31 return $this->contextDefinitions;
35 * Implements \Drupal\Component\Plugin\Definition\ContextAwarePluginDefinitionInterface::getContextDefinition().
37 public function getContextDefinition($name) {
38 if ($this->hasContextDefinition($name)) {
39 return $this->contextDefinitions[$name];
41 throw new ContextException($this->id() . " does not define a '$name' context");
45 * Implements \Drupal\Component\Plugin\Definition\ContextAwarePluginDefinitionInterface::addContextDefinition().
47 public function addContextDefinition($name, ContextDefinitionInterface $definition) {
48 $this->contextDefinitions[$name] = $definition;
53 * Implements \Drupal\Component\Plugin\Definition\ContextAwarePluginDefinitionInterface::removeContextDefinition().
55 public function removeContextDefinition($name) {
56 unset($this->contextDefinitions[$name]);