Same name and namespace in other branches
  1. 4.6.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 specified region for the current user.

@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: The name of a region.

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 601
Controls the boxes that are displayed around the main content.

Code

function block_list($region) {
  global $user, $theme_key;
  static $blocks = array();
  if (!count($blocks)) {
    $result = db_query("SELECT * FROM {blocks} WHERE theme = '%s' AND status = 1 ORDER BY region, weight, module", $theme_key);
    while ($block = db_fetch_object($result)) {
      if (!isset($blocks[$block->region])) {
        $blocks[$block->region] = array();
      }

      // 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) {
        if ($block->visibility < 2) {
          $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, '/')) . ')$/';

          // Compare with the internal and path alias (if any).
          $page_match = preg_match($regexp, $path);
          if ($path != $_GET['q']) {
            $page_match = $page_match || preg_match($regexp, $_GET['q']);
          }

          // When $block->visibility has a value of 0, the block is displayed on
          // all pages except those listed in $block->pages. When set to 1, it
          // is displayed only on those pages listed in $block->pages.
          $page_match = !($block->visibility xor $page_match);
        }
        else {
          $page_match = drupal_eval($block->pages);
        }
      }
      else {
        $page_match = TRUE;
      }
      if ($enabled && $page_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 (isset($array) && is_array($array)) {
            foreach ($array as $k => $v) {
              $block->{$k} = $v;
            }
          }
        }
        if (isset($block->content) && $block->content) {
          $blocks[$block->region]["{$block->module}_{$block->delta}"] = $block;
        }
      }
    }
  }

  // Create an empty array if there were no entries
  if (!isset($blocks[$region])) {
    $blocks[$region] = array();
  }
  return $blocks[$region];
}