trait ContextAwarePluginDefinitionTrait
Provides a trait for context-aware object-based plugin definitions.
Hierarchy
- trait \Drupal\Component\Plugin\Definition\ContextAwarePluginDefinitionTrait
3 files declare their use of ContextAwarePluginDefinitionTrait
- ContextAwarePluginBaseTest.php in core/tests/ Drupal/ KernelTests/ Core/ Plugin/ Context/ ContextAwarePluginBaseTest.php 
- ContextHandlerTest.php in core/tests/ Drupal/ Tests/ Core/ Plugin/ ContextHandlerTest.php 
- Contains \Drupal\Tests\Core\Plugin\ContextHandlerTest.
- SectionStorageDefinition.php in core/modules/ layout_builder/ src/ SectionStorage/ SectionStorageDefinition.php 
File
- 
              core/lib/ Drupal/ Component/ Plugin/ Definition/ ContextAwarePluginDefinitionTrait.php, line 11 
Namespace
Drupal\Component\Plugin\DefinitionView source
trait ContextAwarePluginDefinitionTrait {
  
  /**
   * The context definitions for this plugin definition.
   *
   * @var \Drupal\Component\Plugin\Context\ContextDefinitionInterface[]
   */
  protected $contextDefinitions = [];
  
  /**
   * Implements \Drupal\Component\Plugin\Definition\ContextAwarePluginDefinitionInterface::hasContextDefinition().
   */
  public function hasContextDefinition($name) {
    return array_key_exists($name, $this->contextDefinitions);
  }
  
  /**
   * Implements \Drupal\Component\Plugin\Definition\ContextAwarePluginDefinitionInterface::getContextDefinitions().
   */
  public function getContextDefinitions() {
    return $this->contextDefinitions;
  }
  
  /**
   * Implements \Drupal\Component\Plugin\Definition\ContextAwarePluginDefinitionInterface::getContextDefinition().
   */
  public function getContextDefinition($name) {
    if ($this->hasContextDefinition($name)) {
      return $this->contextDefinitions[$name];
    }
    throw new ContextException($this->id() . " does not define a '{$name}' context");
  }
  
  /**
   * Implements \Drupal\Component\Plugin\Definition\ContextAwarePluginDefinitionInterface::addContextDefinition().
   */
  public function addContextDefinition($name, ContextDefinitionInterface $definition) {
    $this->contextDefinitions[$name] = $definition;
    return $this;
  }
  
  /**
   * Implements \Drupal\Component\Plugin\Definition\ContextAwarePluginDefinitionInterface::removeContextDefinition().
   */
  public function removeContextDefinition($name) {
    unset($this->contextDefinitions[$name]);
    return $this;
  }
}Members
| Title Sort descending | Modifiers | Object type | Summary | 
|---|---|---|---|
| ContextAwarePluginDefinitionTrait::$contextDefinitions | protected | property | The context definitions for this plugin definition. | 
| ContextAwarePluginDefinitionTrait::addContextDefinition | public | function | Implements \Drupal\Component\Plugin\Definition\ContextAwarePluginDefinitionInterface::addContextDefinition(). | 
| ContextAwarePluginDefinitionTrait::getContextDefinition | public | function | Implements \Drupal\Component\Plugin\Definition\ContextAwarePluginDefinitionInterface::getContextDefinition(). | 
| ContextAwarePluginDefinitionTrait::getContextDefinitions | public | function | Implements \Drupal\Component\Plugin\Definition\ContextAwarePluginDefinitionInterface::getContextDefinitions(). | 
| ContextAwarePluginDefinitionTrait::hasContextDefinition | public | function | Implements \Drupal\Component\Plugin\Definition\ContextAwarePluginDefinitionInterface::hasContextDefinition(). | 
| ContextAwarePluginDefinitionTrait::removeContextDefinition | public | function | Implements \Drupal\Component\Plugin\Definition\ContextAwarePluginDefinitionInterface::removeContextDefinition(). | 
Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.
