class BlockRepository
Same name in other branches
- 9 core/modules/block/src/BlockRepository.php \Drupal\block\BlockRepository
- 10 core/modules/block/src/BlockRepository.php \Drupal\block\BlockRepository
- 11.x core/modules/block/src/BlockRepository.php \Drupal\block\BlockRepository
Provides a repository for Block config entities.
Hierarchy
- class \Drupal\block\BlockRepository implements \Drupal\block\BlockRepositoryInterface
Expanded class hierarchy of BlockRepository
1 file declares its use of BlockRepository
- BlockRepositoryTest.php in core/
modules/ block/ tests/ src/ Unit/ BlockRepositoryTest.php - Contains \Drupal\Tests\block\Unit\BlockRepositoryTest.
1 string reference to 'BlockRepository'
- block.services.yml in core/
modules/ block/ block.services.yml - core/modules/block/block.services.yml
1 service uses BlockRepository
- block.repository in core/
modules/ block/ block.services.yml - Drupal\block\BlockRepository
File
-
core/
modules/ block/ src/ BlockRepository.php, line 13
Namespace
Drupal\blockView source
class BlockRepository implements BlockRepositoryInterface {
/**
* The block storage.
*
* @var \Drupal\Core\Entity\EntityStorageInterface
*/
protected $blockStorage;
/**
* The theme manager.
*
* @var \Drupal\Core\Theme\ThemeManagerInterface
*/
protected $themeManager;
/**
* Constructs a new BlockRepository.
*
* @param \Drupal\Core\Entity\EntityTypeManagerInterface $entity_type_manager
* The entity type manager service.
* @param \Drupal\Core\Theme\ThemeManagerInterface $theme_manager
* The theme manager.
* @param \Drupal\Core\Plugin\Context\ContextHandlerInterface $context_handler
* The plugin context handler.
*/
public function __construct(EntityTypeManagerInterface $entity_type_manager, ThemeManagerInterface $theme_manager, ContextHandlerInterface $context_handler) {
$this->blockStorage = $entity_type_manager->getStorage('block');
$this->themeManager = $theme_manager;
$this->contextHandler = $context_handler;
}
/**
* {@inheritdoc}
*/
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;
}
}
Members
Title Sort descending | Modifiers | Object type | Summary | Overriden Title |
---|---|---|---|---|
BlockRepository::$blockStorage | protected | property | The block storage. | |
BlockRepository::$themeManager | protected | property | The theme manager. | |
BlockRepository::getVisibleBlocksPerRegion | public | function | Returns an array of regions and their block entities. | Overrides BlockRepositoryInterface::getVisibleBlocksPerRegion |
BlockRepository::__construct | public | function | Constructs a new BlockRepository. | |
BlockRepositoryInterface::REGIONS_ALL | constant | Return all regions. | ||
BlockRepositoryInterface::REGIONS_VISIBLE | constant | Return only visible regions. |
Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.