ContextAwarePluginBase.php 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  1. <?php
  2. namespace Drupal\Component\Plugin;
  3. use Drupal\Component\Plugin\Context\ContextInterface;
  4. use Drupal\Component\Plugin\Definition\ContextAwarePluginDefinitionInterface;
  5. use Drupal\Component\Plugin\Exception\ContextException;
  6. use Drupal\Component\Plugin\Context\Context;
  7. use Symfony\Component\Validator\ConstraintViolationList;
  8. /**
  9. * Base class for plugins that are context aware.
  10. */
  11. abstract class ContextAwarePluginBase extends PluginBase implements ContextAwarePluginInterface {
  12. /**
  13. * The data objects representing the context of this plugin.
  14. *
  15. * @var \Drupal\Component\Plugin\Context\ContextInterface[]
  16. */
  17. protected $context = [];
  18. /**
  19. * Data objects representing the contexts passed in the plugin configuration.
  20. *
  21. * @var \Drupal\Component\Plugin\Context\ContextInterface[]
  22. *
  23. * @deprecated
  24. * in drupal:8.8.0 and is removed from drupal:9.0.0. Use
  25. * \Drupal\Component\Plugin\ContextAwarePluginInterface instead.
  26. *
  27. * @see https://www.drupal.org/project/drupal/issues/3080631
  28. */
  29. private $contexts = [];
  30. /**
  31. * Overrides \Drupal\Component\Plugin\PluginBase::__construct().
  32. *
  33. * Overrides the construction of context aware plugins to allow for
  34. * unvalidated constructor based injection of contexts.
  35. *
  36. * @param array $configuration
  37. * The plugin configuration, i.e. an array with configuration values keyed
  38. * by configuration option name. The special key 'context' may be used to
  39. * initialize the defined contexts by setting it to an array of context
  40. * values keyed by context names.
  41. * @param string $plugin_id
  42. * The plugin_id for the plugin instance.
  43. * @param mixed $plugin_definition
  44. * The plugin implementation definition.
  45. */
  46. public function __construct(array $configuration, $plugin_id, $plugin_definition) {
  47. $context_configuration = isset($configuration['context']) ? $configuration['context'] : [];
  48. unset($configuration['context']);
  49. parent::__construct($configuration, $plugin_id, $plugin_definition);
  50. $this->context = $this->createContextFromConfiguration($context_configuration);
  51. // @todo Remove $this->contexts in Drupal 9; see
  52. // https://www.drupal.org/project/drupal/issues/3081145
  53. $this->contexts = $this->context;
  54. }
  55. /**
  56. * Implements magic __get() method.
  57. */
  58. public function __get($name) {
  59. if ($name === 'contexts') {
  60. @trigger_error('The $contexts property is deprecated in Drupal 8.8.0 and will be removed before Drupal 9.0.0. Use methods of \Drupal\Component\Plugin\ContextAwarePluginInterface instead. See https://www.drupal.org/project/drupal/issues/3080631 for more information.', E_USER_DEPRECATED);
  61. return $this->contexts;
  62. }
  63. }
  64. /**
  65. * Creates context objects from any context mappings in configuration.
  66. *
  67. * @param array $context_configuration
  68. * An associative array of context names and values.
  69. *
  70. * @return \Drupal\Component\Plugin\Context\ContextInterface[]
  71. * An array of context objects.
  72. */
  73. protected function createContextFromConfiguration(array $context_configuration) {
  74. $contexts = [];
  75. foreach ($context_configuration as $key => $value) {
  76. $context_definition = $this->getContextDefinition($key);
  77. $contexts[$key] = new Context($context_definition, $value);
  78. }
  79. return $contexts;
  80. }
  81. /**
  82. * {@inheritdoc}
  83. */
  84. public function getContextDefinitions() {
  85. $definition = $this->getPluginDefinition();
  86. if ($definition instanceof ContextAwarePluginDefinitionInterface) {
  87. return $definition->getContextDefinitions();
  88. }
  89. else {
  90. return !empty($definition['context_definitions']) ? $definition['context_definitions'] : [];
  91. }
  92. }
  93. /**
  94. * {@inheritdoc}
  95. */
  96. public function getContextDefinition($name) {
  97. $definition = $this->getPluginDefinition();
  98. if ($definition instanceof ContextAwarePluginDefinitionInterface) {
  99. if ($definition->hasContextDefinition($name)) {
  100. return $definition->getContextDefinition($name);
  101. }
  102. }
  103. elseif (!empty($definition['context_definitions'][$name])) {
  104. return $definition['context_definitions'][$name];
  105. }
  106. throw new ContextException(sprintf("The %s context is not a valid context.", $name));
  107. }
  108. /**
  109. * {@inheritdoc}
  110. */
  111. public function getContexts() {
  112. // Make sure all context objects are initialized.
  113. foreach ($this->getContextDefinitions() as $name => $definition) {
  114. $this->getContext($name);
  115. }
  116. return $this->context;
  117. }
  118. /**
  119. * {@inheritdoc}
  120. */
  121. public function getContext($name) {
  122. // Check for a valid context value.
  123. if (!isset($this->context[$name])) {
  124. $this->context[$name] = new Context($this->getContextDefinition($name));
  125. }
  126. return $this->context[$name];
  127. }
  128. /**
  129. * {@inheritdoc}
  130. */
  131. public function setContext($name, ContextInterface $context) {
  132. $this->context[$name] = $context;
  133. }
  134. /**
  135. * {@inheritdoc}
  136. */
  137. public function getContextValues() {
  138. $values = [];
  139. foreach ($this->getContextDefinitions() as $name => $definition) {
  140. $values[$name] = isset($this->context[$name]) ? $this->context[$name]->getContextValue() : NULL;
  141. }
  142. return $values;
  143. }
  144. /**
  145. * {@inheritdoc}
  146. */
  147. public function getContextValue($name) {
  148. return $this->getContext($name)->getContextValue();
  149. }
  150. /**
  151. * {@inheritdoc}
  152. */
  153. public function setContextValue($name, $value) {
  154. $this->context[$name] = new Context($this->getContextDefinition($name), $value);
  155. return $this;
  156. }
  157. /**
  158. * {@inheritdoc}
  159. */
  160. public function validateContexts() {
  161. $violations = new ConstraintViolationList();
  162. // @todo: Implement symfony validator API to let the validator traverse
  163. // and set property paths accordingly.
  164. foreach ($this->getContexts() as $context) {
  165. $violations->addAll($context->validate());
  166. }
  167. return $violations;
  168. }
  169. }