You are here

function _context_ui_get_blocks in Context 6.2

Same name and namespace in other branches
  1. 5 context_ui/context_ui_admin.inc \_context_ui_get_blocks()
  2. 6 context_ui/context_ui.admin.inc \_context_ui_get_blocks()

Helper function to generate a list of blocks from a specified region. If provided a context object, will generate a full list of blocks for that region distinguishing between system blocks and context-provided blocks.

Parameters

$region: The string identifier for a theme region. e.g. "left"

$context: A context object.

Return value

A keyed (by "module_delta" convention) array of blocks.

2 calls to _context_ui_get_blocks()
context_ui_form in context_ui/context_ui.admin.inc
Generates the omnibus context definition editing form. Note: submission and validation handlers are in context_ui.admin.inc
context_ui_form_process in context_ui/context_ui.admin.inc
Produces a context object from submitted form values.

File

context_ui/context_ui.admin.inc, line 972

Code

function _context_ui_get_blocks($region = NULL, $context = NULL) {
  static $block_info, $valid, $system_blocks;

  // we don't static cache context blocks
  $context_blocks = $blocks = array();
  if (!isset($system_blocks)) {
    $system_blocks = array();

    // Compute the active themes:
    $active_themes = array();
    foreach (list_themes() as $theme => $theme_ob) {
      if ($theme_ob->status) {
        $active_themes[] = $theme;
      }
    }

    // There's a small chance that Drupal doesn't actually have any themes active:
    if (empty($active_themes)) {
      $active_themes[] = variable_get('theme_default', 'garland');
    }

    // Rebuild block 'cache':
    _context_ui_block_rehash($active_themes);

    // Load blocks from database
    $result = db_query("SELECT module, delta, weight, region, status FROM {blocks} WHERE theme IN (" . db_placeholders($active_themes, 'varchar') . ") ORDER BY module, delta, weight, region, status", $active_themes);
    while ($block = db_fetch_object($result)) {

      // load block info
      $block_info[$block->module] = isset($block_info[$block->module]) ? $block_info[$block->module] : module_invoke($block->module, 'block', 'list');

      // ensure DB entry wasn't stale before making block usable
      if (!empty($block_info[$block->module][$block->delta]['info'])) {
        $block->label = $block_info[$block->module][$block->delta]['info'];
        $block->type = 'system';
        $block->bid = $block->module . '_' . $block->delta;

        // add block to region
        if ($block->region && $block->status) {
          $system_blocks[$block->region][$block->bid] = $block;
        }
        else {
          $system_blocks[0][$block->bid] = $block;
        }

        // mark block as available in DB
        $valid[$block->module . "_" . $block->delta] = TRUE;
      }
    }
  }

  // load system blocks into main block array
  $blocks = $system_blocks;

  // load context blocks if provided
  if (is_object($context) && !empty($context->block)) {

    // iterate over context-associated blocks
    foreach ($context->block as $block) {
      $block = (object) $block;

      // check that this is a valid block
      if ($valid[$block->module . "_" . $block->delta]) {

        // if region has been specified, ensure that block belongs to it
        if (!$region || isset($region) && $block->region == $region) {

          // load block info
          $block_info[$block->module] = $block_info[$block->module] ? $block_info[$block->module] : module_invoke($block->module, 'block', 'list');
          $block->label = $block_info[$block->module][$block->delta]['info'];
          $block->type = 'context_ui';
          $block->bid = $block->module . '_' . $block->delta;

          // add block to region
          if ($block->region) {
            $blocks[$block->region][$block->bid] = $block;
          }
          else {
            $blocks[0][$block->bid] = $block;
          }
        }
      }
    }
  }
  foreach ($blocks as $r => $sort_region) {
    if ($r !== 0) {
      uasort($sort_region, create_function('$a, $b', 'return ($a->weight - $b->weight);'));
      $blocks[$r] = $sort_region;
    }
  }
  if (!empty($region)) {
    return isset($blocks[$region]) ? $blocks[$region] : array();
  }
  return $blocks;
}