Chris@0: blockStorage = $entity_type_manager->getStorage('block'); Chris@0: $this->themeManager = $theme_manager; Chris@0: $this->contextHandler = $context_handler; Chris@0: } Chris@0: Chris@0: /** Chris@0: * {@inheritdoc} Chris@0: */ Chris@0: public function getVisibleBlocksPerRegion(array &$cacheable_metadata = []) { Chris@0: $active_theme = $this->themeManager->getActiveTheme(); Chris@0: // Build an array of the region names in the right order. Chris@0: $empty = array_fill_keys($active_theme->getRegions(), []); Chris@0: Chris@0: $full = []; Chris@0: foreach ($this->blockStorage->loadByProperties(['theme' => $active_theme->getName()]) as $block_id => $block) { Chris@0: /** @var \Drupal\block\BlockInterface $block */ Chris@0: $access = $block->access('view', NULL, TRUE); Chris@0: $region = $block->getRegion(); Chris@0: if (!isset($cacheable_metadata[$region])) { Chris@0: $cacheable_metadata[$region] = CacheableMetadata::createFromObject($access); Chris@0: } Chris@0: else { Chris@0: $cacheable_metadata[$region] = $cacheable_metadata[$region]->merge(CacheableMetadata::createFromObject($access)); Chris@0: } Chris@0: Chris@0: // Set the contexts on the block before checking access. Chris@0: if ($access->isAllowed()) { Chris@0: $full[$region][$block_id] = $block; Chris@0: } Chris@0: } Chris@0: Chris@0: // Merge it with the actual values to maintain the region ordering. Chris@0: $assignments = array_intersect_key(array_merge($empty, $full), $empty); Chris@0: foreach ($assignments as &$assignment) { Chris@0: // Suppress errors because PHPUnit will indirectly modify the contents, Chris@0: // triggering https://bugs.php.net/bug.php?id=50688. Chris@0: @uasort($assignment, 'Drupal\block\Entity\Block::sort'); Chris@0: } Chris@0: return $assignments; Chris@0: } Chris@0: Chris@0: }