PageDisplayVariantSelectionEvent.php 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. <?php
  2. namespace Drupal\Core\Render;
  3. use Drupal\Core\Cache\RefinableCacheableDependencyInterface;
  4. use Drupal\Core\Cache\RefinableCacheableDependencyTrait;
  5. use Drupal\Core\Routing\RouteMatchInterface;
  6. use Symfony\Component\EventDispatcher\Event;
  7. /**
  8. * Event fired when rendering main content, to select a page display variant.
  9. *
  10. * Subscribers of this event can call the following setters to pass additional
  11. * information along to the selected variant:
  12. * - self::setPluginConfiguration()
  13. * - self::setContexts()
  14. * - self::addCacheableDependency()
  15. *
  16. * @see \Drupal\Core\Render\RenderEvents::SELECT_PAGE_DISPLAY_VARIANT
  17. * @see \Drupal\Core\Render\MainContent\HtmlRenderer
  18. */
  19. class PageDisplayVariantSelectionEvent extends Event implements RefinableCacheableDependencyInterface {
  20. use RefinableCacheableDependencyTrait;
  21. /**
  22. * The selected page display variant plugin ID.
  23. *
  24. * @var string
  25. */
  26. protected $pluginId;
  27. /**
  28. * The configuration for the selected page display variant.
  29. *
  30. * @var array
  31. */
  32. protected $pluginConfiguration = [];
  33. /**
  34. * The current route match.
  35. *
  36. * @var \Drupal\Core\Routing\RouteMatchInterface
  37. */
  38. protected $routeMatch;
  39. /**
  40. * An array of collected contexts to pass to the page display variant.
  41. *
  42. * @var \Drupal\Component\Plugin\Context\ContextInterface[]
  43. */
  44. protected $contexts = [];
  45. /**
  46. * Constructs the page display variant plugin selection event.
  47. *
  48. * @param string $plugin_id
  49. * The ID of the page display variant plugin to use by default.
  50. * @param \Drupal\Core\Routing\RouteMatchInterface $route_match
  51. * The current route match, for context.
  52. */
  53. public function __construct($plugin_id, RouteMatchInterface $route_match) {
  54. $this->pluginId = $plugin_id;
  55. $this->routeMatch = $route_match;
  56. }
  57. /**
  58. * The selected page display variant plugin ID.
  59. *
  60. * @param string $plugin_id
  61. * The ID of the page display variant plugin to use.
  62. *
  63. * @return $this
  64. */
  65. public function setPluginId($plugin_id) {
  66. $this->pluginId = $plugin_id;
  67. return $this;
  68. }
  69. /**
  70. * The selected page display variant plugin ID.
  71. *
  72. * @return string
  73. */
  74. public function getPluginId() {
  75. return $this->pluginId;
  76. }
  77. /**
  78. * Set the configuration for the selected page display variant.
  79. *
  80. * @param array $configuration
  81. * The configuration for the selected page display variant.
  82. *
  83. * @return $this
  84. */
  85. public function setPluginConfiguration(array $configuration) {
  86. $this->pluginConfiguration = $configuration;
  87. return $this;
  88. }
  89. /**
  90. * Get the configuration for the selected page display variant.
  91. *
  92. * @return array
  93. */
  94. public function getPluginConfiguration() {
  95. return $this->pluginConfiguration;
  96. }
  97. /**
  98. * Gets the current route match.
  99. *
  100. * @return \Drupal\Core\Routing\RouteMatchInterface
  101. * The current route match, for context.
  102. */
  103. public function getRouteMatch() {
  104. return $this->routeMatch;
  105. }
  106. /**
  107. * Gets the contexts that were set during event dispatch.
  108. *
  109. * @return \Drupal\Component\Plugin\Context\ContextInterface[]
  110. * An array of set contexts, keyed by context name.
  111. */
  112. public function getContexts() {
  113. return $this->contexts;
  114. }
  115. /**
  116. * Sets the contexts to be passed to the page display variant.
  117. *
  118. * @param \Drupal\Component\Plugin\Context\ContextInterface[] $contexts
  119. * An array of contexts, keyed by context name.
  120. *
  121. * @return $this
  122. */
  123. public function setContexts(array $contexts) {
  124. $this->contexts = $contexts;
  125. return $this;
  126. }
  127. }