You are here

function block_views_block_list_alter in Block Views Visibility 7

Implements hook_block_list_alter().

Check the view specific visibilty settings. Remove the block if the visibility conditions are not met.

File

./block_views.module, line 129
Demonstrate basic module block_views.

Code

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

  // Build an array of node types for each block.
  $block_views = array();
  $result = db_query('SELECT * FROM {block_views}');
  foreach ($result as $record) {
    $block_views[$record->module][$record->delta][$record->view][$record->display] = TRUE;
  }

  //dsm($block_views);
  $menu = menu_get_item();

  //dsm($menu);
  $view = '';
  if ($menu['page_callback'] == 'views_page') {
    $view = $menu['page_arguments'][0];
    $display = $menu['page_arguments'][1];
  }

  //dsm($view);
  foreach ($blocks as $key => $block) {
    if (!isset($block->theme) || !isset($block->status) || $block->theme != $theme_key || $block->status != 1) {

      // This block was added by a contrib module, leave it in the list.
      continue;
    }

    // If a block has no node types associated, it is displayed for every type.
    // For blocks with node types associated, if the node type does not match
    // the settings from this block, remove it from the block list.
    if (isset($block_views[$block->module][$block->delta])) {
      if (!empty($view)) {

        // This is a node or node edit page.
        if (!isset($block_views[$block->module][$block->delta][$view][$display])) {

          // This block should not be displayed for this node type.
          unset($blocks[$key]);
          continue;
        }
      }
      else {

        // This is not a views page, remove the block.
        unset($blocks[$key]);
        continue;
      }
    }
  }
}