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