BlockRepository.php 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. <?php
  2. namespace Drupal\block;
  3. use Drupal\Core\Cache\CacheableMetadata;
  4. use Drupal\Core\Entity\EntityManagerInterface;
  5. use Drupal\Core\Plugin\Context\ContextHandlerInterface;
  6. use Drupal\Core\Theme\ThemeManagerInterface;
  7. /**
  8. * Provides a repository for Block config entities.
  9. */
  10. class BlockRepository implements BlockRepositoryInterface {
  11. /**
  12. * The block storage.
  13. *
  14. * @var \Drupal\Core\Entity\EntityStorageInterface
  15. */
  16. protected $blockStorage;
  17. /**
  18. * The theme manager.
  19. *
  20. * @var \Drupal\Core\Theme\ThemeManagerInterface
  21. */
  22. protected $themeManager;
  23. /**
  24. * Constructs a new BlockRepository.
  25. *
  26. * @param \Drupal\Core\Entity\EntityManagerInterface $entity_manager
  27. * The entity manager.
  28. * @param \Drupal\Core\Theme\ThemeManagerInterface $theme_manager
  29. * The theme manager.
  30. * @param \Drupal\Core\Plugin\Context\ContextHandlerInterface $context_handler
  31. * The plugin context handler.
  32. */
  33. public function __construct(EntityManagerInterface $entity_manager, ThemeManagerInterface $theme_manager, ContextHandlerInterface $context_handler) {
  34. $this->blockStorage = $entity_manager->getStorage('block');
  35. $this->themeManager = $theme_manager;
  36. $this->contextHandler = $context_handler;
  37. }
  38. /**
  39. * {@inheritdoc}
  40. */
  41. public function getVisibleBlocksPerRegion(array &$cacheable_metadata = []) {
  42. $active_theme = $this->themeManager->getActiveTheme();
  43. // Build an array of the region names in the right order.
  44. $empty = array_fill_keys($active_theme->getRegions(), []);
  45. $full = [];
  46. foreach ($this->blockStorage->loadByProperties(['theme' => $active_theme->getName()]) as $block_id => $block) {
  47. /** @var \Drupal\block\BlockInterface $block */
  48. $access = $block->access('view', NULL, TRUE);
  49. $region = $block->getRegion();
  50. if (!isset($cacheable_metadata[$region])) {
  51. $cacheable_metadata[$region] = CacheableMetadata::createFromObject($access);
  52. }
  53. else {
  54. $cacheable_metadata[$region] = $cacheable_metadata[$region]->merge(CacheableMetadata::createFromObject($access));
  55. }
  56. // Set the contexts on the block before checking access.
  57. if ($access->isAllowed()) {
  58. $full[$region][$block_id] = $block;
  59. }
  60. }
  61. // Merge it with the actual values to maintain the region ordering.
  62. $assignments = array_intersect_key(array_merge($empty, $full), $empty);
  63. foreach ($assignments as &$assignment) {
  64. // Suppress errors because PHPUnit will indirectly modify the contents,
  65. // triggering https://bugs.php.net/bug.php?id=50688.
  66. @uasort($assignment, 'Drupal\block\Entity\Block::sort');
  67. }
  68. return $assignments;
  69. }
  70. }