ContextAwarePluginBase.php 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  1. <?php
  2. namespace Drupal\Core\Plugin;
  3. use Drupal\Component\Plugin\ConfigurablePluginInterface;
  4. use Drupal\Component\Plugin\ContextAwarePluginBase as ComponentContextAwarePluginBase;
  5. use Drupal\Component\Plugin\Exception\ContextException;
  6. use Drupal\Core\Cache\Cache;
  7. use Drupal\Core\Cache\CacheableDependencyInterface;
  8. use Drupal\Core\DependencyInjection\DependencySerializationTrait;
  9. use Drupal\Core\Plugin\Context\Context;
  10. use Drupal\Core\StringTranslation\StringTranslationTrait;
  11. use Drupal\Core\TypedData\TypedDataTrait;
  12. use Drupal\Component\Plugin\Context\ContextInterface as ComponentContextInterface;
  13. use Drupal\Core\Plugin\Context\ContextInterface;
  14. /**
  15. * Base class for plugins that are context aware.
  16. */
  17. abstract class ContextAwarePluginBase extends ComponentContextAwarePluginBase implements ContextAwarePluginInterface, CacheableDependencyInterface {
  18. use TypedDataTrait;
  19. use StringTranslationTrait;
  20. use DependencySerializationTrait;
  21. /**
  22. * {@inheritdoc}
  23. *
  24. * @return \Drupal\Core\Plugin\Context\ContextInterface[]
  25. */
  26. protected function createContextFromConfiguration(array $context_configuration) {
  27. // This method is overridden so that it will use
  28. // \Drupal\Core\Plugin\Context\Context instead.
  29. $contexts = [];
  30. foreach ($context_configuration as $key => $value) {
  31. $context_definition = $this->getContextDefinition($key);
  32. $contexts[$key] = new Context($context_definition, $value);
  33. }
  34. return $contexts;
  35. }
  36. /**
  37. * {@inheritdoc}
  38. *
  39. * This code is identical to the Component in order to pick up a different
  40. * Context class.
  41. *
  42. * @return \Drupal\Core\Plugin\Context\ContextInterface
  43. * The context object.
  44. */
  45. public function getContext($name) {
  46. // Check for a valid context value.
  47. if (!isset($this->context[$name])) {
  48. $this->context[$name] = new Context($this->getContextDefinition($name));
  49. }
  50. return $this->context[$name];
  51. }
  52. /**
  53. * {@inheritdoc}
  54. */
  55. public function setContext($name, ComponentContextInterface $context) {
  56. // Check that the context passed is an instance of our extended interface.
  57. if (!$context instanceof ContextInterface) {
  58. throw new ContextException("Passed $name context must be an instance of \\Drupal\\Core\\Plugin\\Context\\ContextInterface");
  59. }
  60. parent::setContext($name, $context);
  61. }
  62. /**
  63. * {@inheritdoc}
  64. */
  65. public function setContextValue($name, $value) {
  66. $this->context[$name] = Context::createFromContext($this->getContext($name), $value);
  67. return $this;
  68. }
  69. /**
  70. * {@inheritdoc}
  71. */
  72. public function getContextMapping() {
  73. $configuration = $this instanceof ConfigurablePluginInterface ? $this->getConfiguration() : $this->configuration;
  74. return isset($configuration['context_mapping']) ? $configuration['context_mapping'] : [];
  75. }
  76. /**
  77. * {@inheritdoc}
  78. */
  79. public function setContextMapping(array $context_mapping) {
  80. if ($this instanceof ConfigurablePluginInterface) {
  81. $configuration = $this->getConfiguration();
  82. $configuration['context_mapping'] = array_filter($context_mapping);
  83. $this->setConfiguration($configuration);
  84. }
  85. else {
  86. $this->configuration['context_mapping'] = $context_mapping;
  87. }
  88. return $this;
  89. }
  90. /**
  91. * {@inheritdoc}
  92. *
  93. * @return \Drupal\Core\Plugin\Context\ContextDefinitionInterface[]
  94. */
  95. public function getContextDefinitions() {
  96. return parent::getContextDefinitions();
  97. }
  98. /**
  99. * {@inheritdoc}
  100. *
  101. * @return \Drupal\Core\Plugin\Context\ContextDefinitionInterface
  102. */
  103. public function getContextDefinition($name) {
  104. return parent::getContextDefinition($name);
  105. }
  106. /**
  107. * Wraps the context handler.
  108. *
  109. * @return \Drupal\Core\Plugin\Context\ContextHandlerInterface
  110. */
  111. protected function contextHandler() {
  112. return \Drupal::service('context.handler');
  113. }
  114. /**
  115. * {@inheritdoc}
  116. */
  117. public function getCacheContexts() {
  118. $cache_contexts = [];
  119. // Applied contexts can affect the cache contexts when this plugin is
  120. // involved in caching, collect and return them.
  121. foreach ($this->getContexts() as $context) {
  122. /** @var $context \Drupal\Core\Cache\CacheableDependencyInterface */
  123. if ($context instanceof CacheableDependencyInterface) {
  124. $cache_contexts = Cache::mergeContexts($cache_contexts, $context->getCacheContexts());
  125. }
  126. }
  127. return $cache_contexts;
  128. }
  129. /**
  130. * {@inheritdoc}
  131. */
  132. public function getCacheTags() {
  133. $tags = [];
  134. // Applied contexts can affect the cache tags when this plugin is
  135. // involved in caching, collect and return them.
  136. foreach ($this->getContexts() as $context) {
  137. /** @var $context \Drupal\Core\Cache\CacheableDependencyInterface */
  138. if ($context instanceof CacheableDependencyInterface) {
  139. $tags = Cache::mergeTags($tags, $context->getCacheTags());
  140. }
  141. }
  142. return $tags;
  143. }
  144. /**
  145. * {@inheritdoc}
  146. */
  147. public function getCacheMaxAge() {
  148. $max_age = Cache::PERMANENT;
  149. // Applied contexts can affect the cache max age when this plugin is
  150. // involved in caching, collect and return them.
  151. foreach ($this->getContexts() as $context) {
  152. /** @var $context \Drupal\Core\Cache\CacheableDependencyInterface */
  153. if ($context instanceof CacheableDependencyInterface) {
  154. $max_age = Cache::mergeMaxAges($max_age, $context->getCacheMaxAge());
  155. }
  156. }
  157. return $max_age;
  158. }
  159. }