comparison core/lib/Drupal/Component/Plugin/Definition/ContextAwarePluginDefinitionTrait.php @ 17:129ea1e6d783

Update, including to Drupal core 8.6.10
author Chris Cannam
date Thu, 28 Feb 2019 13:21:36 +0000
parents
children
comparison
equal deleted inserted replaced
16:c2387f117808 17:129ea1e6d783
1 <?php
2
3 namespace Drupal\Component\Plugin\Definition;
4
5 use Drupal\Component\Plugin\Context\ContextDefinitionInterface;
6 use Drupal\Component\Plugin\Exception\ContextException;
7
8 /**
9 * Provides a trait for context-aware object-based plugin definitions.
10 */
11 trait ContextAwarePluginDefinitionTrait {
12
13 /**
14 * The context definitions for this plugin definition.
15 *
16 * @var \Drupal\Component\Plugin\Context\ContextDefinitionInterface[]
17 */
18 protected $contextDefinitions = [];
19
20 /**
21 * Implements \Drupal\Component\Plugin\Definition\ContextAwarePluginDefinitionInterface::hasContextDefinition().
22 */
23 public function hasContextDefinition($name) {
24 return array_key_exists($name, $this->contextDefinitions);
25 }
26
27 /**
28 * Implements \Drupal\Component\Plugin\Definition\ContextAwarePluginDefinitionInterface::getContextDefinitions().
29 */
30 public function getContextDefinitions() {
31 return $this->contextDefinitions;
32 }
33
34 /**
35 * Implements \Drupal\Component\Plugin\Definition\ContextAwarePluginDefinitionInterface::getContextDefinition().
36 */
37 public function getContextDefinition($name) {
38 if ($this->hasContextDefinition($name)) {
39 return $this->contextDefinitions[$name];
40 }
41 throw new ContextException($this->id() . " does not define a '$name' context");
42 }
43
44 /**
45 * Implements \Drupal\Component\Plugin\Definition\ContextAwarePluginDefinitionInterface::addContextDefinition().
46 */
47 public function addContextDefinition($name, ContextDefinitionInterface $definition) {
48 $this->contextDefinitions[$name] = $definition;
49 return $this;
50 }
51
52 /**
53 * Implements \Drupal\Component\Plugin\Definition\ContextAwarePluginDefinitionInterface::removeContextDefinition().
54 */
55 public function removeContextDefinition($name) {
56 unset($this->contextDefinitions[$name]);
57 return $this;
58 }
59
60 }