function BlockRepository::getVisibleBlocksPerRegion

Same name in other branches
  1. 9 core/modules/block/src/BlockRepository.php \Drupal\block\BlockRepository::getVisibleBlocksPerRegion()
  2. 8.9.x core/modules/block/src/BlockRepository.php \Drupal\block\BlockRepository::getVisibleBlocksPerRegion()
  3. 11.x core/modules/block/src/BlockRepository.php \Drupal\block\BlockRepository::getVisibleBlocksPerRegion()

File

core/modules/block/src/BlockRepository.php, line 55

Class

BlockRepository
Provides a repository for Block config entities.

Namespace

Drupal\block

Code

public function getVisibleBlocksPerRegion(array &$cacheable_metadata = []) {
    $active_theme = $this->themeManager
        ->getActiveTheme();
    // Build an array of the region names in the right order.
    $empty = array_fill_keys($active_theme->getRegions(), []);
    $full = [];
    foreach ($this->blockStorage
        ->loadByProperties([
        'theme' => $active_theme->getName(),
    ]) as $block_id => $block) {
        
        /** @var \Drupal\block\BlockInterface $block */
        $access = $block->access('view', NULL, TRUE);
        $region = $block->getRegion();
        if (!isset($cacheable_metadata[$region])) {
            $cacheable_metadata[$region] = CacheableMetadata::createFromObject($access);
        }
        else {
            $cacheable_metadata[$region] = $cacheable_metadata[$region]->merge(CacheableMetadata::createFromObject($access));
        }
        // Set the contexts on the block before checking access.
        if ($access->isAllowed()) {
            $full[$region][$block_id] = $block;
        }
    }
    // Merge it with the actual values to maintain the region ordering.
    $assignments = array_intersect_key(array_merge($empty, $full), $empty);
    foreach ($assignments as &$assignment) {
        uasort($assignment, 'Drupal\\block\\Entity\\Block::sort');
    }
    return $assignments;
}

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.