You are here

public function BlockService::getAvailableBlocks in Opigno dashboard 8

Same name and namespace in other branches
  1. 3.x src/BlockService.php \Drupal\opigno_dashboard\BlockService::getAvailableBlocks()

Returns available blocks.

2 calls to BlockService::getAvailableBlocks()
BlockService::createBlocksInstances in src/BlockService.php
Creates blocks instances.
BlockService::getDashboardBlocksContents in src/BlockService.php
Returns blocks contents.

File

src/BlockService.php, line 33

Class

BlockService
Class BlockService.

Namespace

Drupal\opigno_dashboard

Code

public function getAvailableBlocks() {
  $blocks = $this
    ->getAllBlocks();
  $availables = \Drupal::config('opigno_dashboard.settings')
    ->get('blocks');
  $account = \Drupal::currentUser();
  $account_roles = $account
    ->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);
}