You are here

function blockgroup_block_list_alter in Block Group 7.2

Same name and namespace in other branches
  1. 7 blockgroup.module \blockgroup_block_list_alter()

Implements hook_block_list_alter().

Remove all blocks contained in invisible block groups.

File

./blockgroup.module, line 247
Add block groups to block configuration page

Code

function blockgroup_block_list_alter(&$blocks) {
  global $theme_key;

  // Assumption 1: Regions which are not injected by blockgroup are considered
  // system regions. Each system region is assigned a negative fake block id.
  $all_regions = system_region_list($theme_key);
  $system_regions = array_diff_key($all_regions, blockgroup_region_list());
  $system_regions = array_combine(array_keys($system_regions), range(-1, -count($system_regions)));

  // Assumption 2: recursion only occures through block groups. There are no
  // other modules doing the same thing than we do.
  $group_regions = array();
  foreach ($blocks as $bid => $block) {
    if ($block->module === 'blockgroup') {
      $group_regions[blockgroup_get_region($blocks[$bid]->delta)] = $bid;
    }
  }

  // Construct tree (mapping bid -> parent bid).
  $tree = array();
  foreach ($blocks as $bid => $block) {
    if (isset($system_regions[$block->region])) {
      $tree[$bid] = $system_regions[$block->region];
    }
    elseif (isset($group_regions[$block->region])) {
      $tree[$bid] = $group_regions[$block->region];
    }
  }

  // Remove all entries from parent list where top > 0 (i.e. not attached to a
  // system region).
  foreach ($tree as $bid => $pbid) {
    $top = _blockgroup_tree_get_top($bid, $tree);
    if (_blockgroup_tree_get_top($bid, $tree) > 0) {
      unset($tree[$bid]);
    }
  }

  // Construct branch list (mapping base -> children).
  $branches = array();
  foreach ($tree as $bid => $pbid) {
    $branches[$pbid][] = $bid;
  }

  // Purge all branches containing only block groups and no content blocks.
  foreach (array_keys($branches) as $bid) {
    $bids = _blockgroup_branch_flatten($bid, $branches);
    $content_blocks = array_diff($bids, $group_regions);
    $content_blocks = array_filter($content_blocks, function ($bid) {
      return $bid > 0;
    });
    if (empty($content_blocks)) {
      $tree = array_diff_key($tree, drupal_map_assoc($bids));
    }
  }

  // Unset all blocks which are not in the tree.
  $blocks = array_intersect_key($blocks, $tree);
}