You are here

public function BlockRepository::getVisibleBlocksPerRegion in Drupal 10

Same name and namespace in other branches
  1. 8 core/modules/block/src/BlockRepository.php \Drupal\block\BlockRepository::getVisibleBlocksPerRegion()
  2. 9 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) {

    // Suppress errors because PHPUnit will indirectly modify the contents,
    // triggering https://bugs.php.net/bug.php?id=50688.
    @uasort($assignment, 'Drupal\\block\\Entity\\Block::sort');
  }
  return $assignments;
}