Same name and namespace in other branches
  1. 4.7.x modules/block.module \block_list()
  2. 5.x modules/block/block.module \block_list()
  3. 6.x modules/block/block.module \block_list()
  4. 7.x modules/block/block.module \block_list()

Return all blocks in the specied region for the current user. You may use this function to implement variable block regions. The default regions are 'left', 'right' and 'all', where 'all' means both left and right.

@todo Add a proper primary key (bid) to the blocks table so we don't have to mess around with this <i>module</i>_<i>delta</i> construct. Currently, the blocks table has no primary key defined!

Parameters

$region: This is a string which describes in a human readable form which region you need.

$regions: This is an optional array and contains map(s) from the string $region to the numerical region value(s) in the blocks table. See default value for examples.

Return value

An array of block objects, indexed with <i>module</i>_<i>delta</i>. If you are displaying your blocks in one or two sidebars, you may check whether this array is empty to see how many columns are going to be displayed.

File

modules/block.module, line 449
Controls the boxes that are displayed around the main content.

Code

function block_list($region, $regions = array(
  'left' => 0,
  'right' => 1,
  'all' => '0, 1',
)) {
  global $user;
  static $blocks = array();
  if (!isset($blocks[$region])) {
    $blocks[$region] = array();
    $result = db_query("SELECT * FROM {blocks} WHERE status = 1 AND region IN (%s) ORDER BY weight, module", $regions[$region]);
    while ($block = db_fetch_array($result)) {

      // Use the user's block visibility setting, if necessary
      if ($block['custom'] != 0) {
        if ($user->uid && isset($user->block[$block['module']][$block['delta']])) {
          $enabled = $user->block[$block['module']][$block['delta']];
        }
        else {
          $enabled = $block['custom'] == 1;
        }
      }
      else {
        $enabled = TRUE;
      }

      // Match path if necessary
      if ($block['pages']) {
        $path = drupal_get_path_alias($_GET['q']);
        $regexp = '/^(' . preg_replace(array(
          '/(\\r\\n?|\\n)/',
          '/\\\\\\*/',
          '/(^|\\|)\\\\<front\\\\>($|\\|)/',
        ), array(
          '|',
          '.*',
          '\\1' . preg_quote(variable_get('site_frontpage', 'node'), '/') . '\\2',
        ), preg_quote($block['pages'], '/')) . ')$/';
        $page_match = !($block['visibility'] xor preg_match($regexp, $path));
      }
      else {
        $page_match = TRUE;
      }

      // Match node type if necessary
      $type_match = FALSE;
      if ($block['types'] != '') {
        if (arg(0) == 'node' && is_numeric(arg(1))) {
          $node = node_load(array(
            'nid' => arg(1),
          ));
          $types = explode(',', $block['types']);

          //Match on any one selected type
          foreach ($types as $type) {
            if ($node->type == $type) {
              $type_match = TRUE;
              break;
            }
          }
        }
      }
      else {
        $type_match = TRUE;
      }
      if ($enabled && $page_match && $type_match) {

        // Check the current throttle status and see if block should be displayed
        // based on server load.
        if (!($block['throttle'] && module_invoke('throttle', 'status') > 0)) {
          $array = module_invoke($block['module'], 'block', 'view', $block['delta']);
          if (is_array($array)) {
            $block = array_merge($block, $array);
          }
        }
        if (isset($block['content']) && $block['content']) {
          $blocks[$region]["{$block['module']}_{$block['delta']}"] = (object) $block;
        }
      }
    }
  }
  return $blocks[$region];
}