You are here

function context_block_list in Context 6.2

Same name and namespace in other branches
  1. 6 context.core.inc \context_block_list()

An alternative version of block_list() that provides any context enabled blocks.

1 call to context_block_list()
context_blocks in ./context.core.inc
This override of theme_blocks() is called because of an alter of the theme registry. See context_theme_registry_alter().

File

./context.core.inc, line 466

Code

function context_block_list($region) {
  static $blocks;
  static $context_blocks;
  if (!isset($context_blocks)) {
    $blocks = array();
    $context_blocks = array();

    // Store all active context blocks when first called
    $context_blocks = array();
    foreach (context_active_values('block') as $block) {
      $block = (object) $block;
      $context_blocks["{$block->module}-{$block->delta}"] = $block;
    }
    global $user, $theme_key;
    $rids = array_keys($user->roles);

    // This query is identical to the one in block_list(), but status = 1 is excluded to
    // retain blocks that may be enabled via context.
    $result = db_query(db_rewrite_sql("SELECT DISTINCT b.* FROM {blocks} b LEFT JOIN {blocks_roles} r ON b.module = r.module AND b.delta = r.delta WHERE b.theme = '%s' AND (r.rid IN (" . db_placeholders($rids) . ") OR r.rid IS NULL) ORDER BY b.region, b.weight, b.module", 'b', 'bid'), array_merge(array(
      $theme_key,
    ), $rids));
    while ($block = db_fetch_object($result)) {
      $bid = "{$block->module}-{$block->delta}";

      // If block is not enabled & not enabled via context, skip it
      if (!empty($context_blocks[$bid])) {
        $block->region = $context_blocks[$bid]->region;
        $block->weight = $context_blocks[$bid]->weight;
        unset($context_blocks[$bid]);
        $enabled = TRUE;
      }
      else {
        if (!$block->status) {
          continue;
        }
      }

      // Initialize region key
      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']);

          // Compare with the internal and path alias (if any).
          $page_match = drupal_match_path($path, $block->pages);
          if ($path != $_GET['q']) {
            $page_match = $page_match || drupal_match_path($_GET['q'], $block->pages);
          }

          // 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;
      }
      $block->enabled = $enabled;
      $block->page_match = $page_match;
      $blocks[$block->region]["{$block->module}_{$block->delta}"] = $block;
    }

    // It's possible that there are still some leftover blocks in the enabled contexts.
    // Add these in as well.
    if (!empty($context_blocks)) {
      foreach ($context_blocks as $block) {
        $block = (object) $block;
        $block->status = 1;
        $block->enabled = TRUE;
        $block->page_match = TRUE;
        $block->throttle = FALSE;
        $block->title = '';
        $blocks[$block->region]["{$block->module}_{$block->delta}"] = $block;
      }
    }

    // Sort blocks -- we must do this here since blocks provided via
    // context may have overridden or altered weights.
    foreach ($blocks as $r => $dummy) {
      uasort($blocks[$r], '_context_block_sort');
    }
  }

  // ==================================================================
  // The block rendering code below is identical to block_list().
  // ==================================================================
  // Create an empty array if there were no entries
  if (!isset($blocks[$region])) {
    $blocks[$region] = array();
  }
  foreach ($blocks[$region] as $key => $block) {

    // Render the block content if it has not been created already.
    if (!isset($block->content)) {

      // Erase the block from the static array - we'll put it back if it has content.
      unset($blocks[$region][$key]);
      if ($block->enabled && $block->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)) {

          // Try fetching the block from cache. Block caching is not compatible with
          // node_access modules. We also preserve the submission of forms in blocks,
          // by fetching from cache only if the request method is 'GET'.
          if (!count(module_implements('node_grants')) && $_SERVER['REQUEST_METHOD'] == 'GET' && ($cid = _block_get_cache_id($block)) && ($cache = cache_get($cid, 'cache_block'))) {
            $array = $cache->data;
          }
          else {
            $array = module_invoke($block->module, 'block', 'view', $block->delta);
            if (isset($cid)) {
              cache_set($cid, $array, 'cache_block', CACHE_TEMPORARY);
            }
          }
          if (isset($array) && is_array($array)) {
            foreach ($array as $k => $v) {
              $block->{$k} = $v;
            }
          }
        }
        if (isset($block->content) && $block->content) {

          // Override default block title if a custom display title is present.
          if ($block->title) {

            // Check plain here to allow module generated titles to keep any markup.
            $block->subject = $block->title == '<none>' ? '' : check_plain($block->title);
          }
          if (!isset($block->subject)) {
            $block->subject = '';
          }
          $blocks[$block->region]["{$block->module}_{$block->delta}"] = $block;
        }
      }
    }
  }
  return $blocks[$region];
}