public function BlockService::getAvailableBlocks in Opigno dashboard 3.x
Same name and namespace in other branches
- 8 src/BlockService.php \Drupal\opigno_dashboard\BlockService::getAvailableBlocks()
Returns available blocks.
Return value
array The list of available blocks.
Overrides BlockServiceInterface::getAvailableBlocks
3 calls to BlockService::getAvailableBlocks()
- BlockService::createBlocksInstances in src/
BlockService.php - Creates blocks instances.
- BlockService::getDashboardBlocksContents in src/
BlockService.php - Returns blocks contents.
- BlockService::getPositioning in src/
BlockService.php - Returns positioning.
File
- src/
BlockService.php, line 109
Class
- BlockService
- The dashboard block manager service definition.
Namespace
Drupal\opigno_dashboardCode
public function getAvailableBlocks() : array {
$blocks = $this
->getAllBlocks();
$availables = $this->configFactory
->get('opigno_dashboard.settings')
->get('blocks');
$account_roles = $this->currentUser
->getRoles();
foreach ($blocks as $key1 => &$block) {
if (!isset($availables[$key1]) || isset($availables[$key1]) && !$availables[$key1]['available']) {
unset($blocks[$key1]);
}
else {
// Check access first.
$block_real = Block::load($this
->sanitizeId($key1));
if (!$block_real) {
// Try to load old version of block.
$block_real = Block::load($this
->sanitizeIdOld($key1));
}
$role_access = TRUE;
if (!empty($block_real)) {
$block_visibility = $block_real
->getVisibility();
if (isset($block_visibility['user_role']) && !empty($block_visibility['user_role'])) {
$role_access = FALSE;
foreach ($block_visibility['user_role']['roles'] as $block_role) {
if (in_array($block_role, $account_roles)) {
$role_access = TRUE;
}
}
}
}
if (!$role_access) {
unset($blocks[$key1]);
continue;
}
foreach ($block as &$value) {
if (is_object($value)) {
$value = $value
->render();
}
}
$blocks[$key1]['id'] = $key1;
unset($blocks[$key1]['config_dependencies'], $blocks[$key1]['class'], $blocks[$key1]['provider'], $blocks[$key1]['category'], $blocks[$key1]['deriver'], $blocks[$key1]['context']);
}
}
return array_values($blocks);
}