You are here

function context_reaction_block::get_blocks in Context 7.3

Same name and namespace in other branches
  1. 6.3 plugins/context_reaction_block.inc \context_reaction_block::get_blocks()
  2. 6 plugins/context_reaction_block.inc \context_reaction_block::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.

6 calls to context_reaction_block::get_blocks()
context_reaction_block::block_list in plugins/context_reaction_block.inc
An alternative version of block_list() that provides any context enabled blocks.
context_reaction_block::editor_form in plugins/context_reaction_block.inc
Context editor form for blocks.
context_reaction_block::max_block_weight in plugins/context_reaction_block.inc
Generate the safe weight range for a block being added to a region such that there are enough potential unique weights to support all blocks.
context_reaction_block::options_form in plugins/context_reaction_block.inc
Options form.
context_reaction_block::options_form_submit in plugins/context_reaction_block.inc
Options form submit handler.

... See full list

File

plugins/context_reaction_block.inc, line 485

Class

context_reaction_block
Expose blocks as context reactions.

Code

function get_blocks($region = NULL, $context = NULL, $reset = FALSE) {
  static $block_info;
  $theme_key = variable_get('theme_default', 'garland');
  if (!isset($block_info) || $reset) {
    $block_info = array();
    if (!$reset) {
      $block_info = context_cache_get('block_info');
    }
    if (empty($block_info)) {
      if (module_exists('block')) {
        $block_blocks = _block_rehash($theme_key);
        $block_info = array();

        // Change from numeric keys to module-delta.
        foreach ($block_blocks as $block) {
          $block = (object) $block;
          unset($block->theme, $block->status, $block->weight, $block->region, $block->custom, $block->visibility, $block->pages);
          $block->bid = "{$block->module}-{$block->delta}";
          $block_info[$block->bid] = $block;
        }
      }
      else {
        $block_info = array();
        foreach (module_implements('block_info') as $module) {
          $module_blocks = module_invoke($module, 'block_info');
          if (!empty($module_blocks)) {
            foreach ($module_blocks as $delta => $block) {
              $block = (object) $block;
              $block->module = $module;
              $block->delta = $delta;
              $block->bid = "{$block->module}-{$block->delta}";
              $block_info[$block->bid] = $block;
            }
          }
        }
      }
      context_cache_set('block_info', $block_info);
    }

    // Allow other modules that may declare blocks dynamically to alter
    // this list.
    drupal_alter('context_block_info', $block_info);

    // Gather only region info from the database.
    if (module_exists('block')) {
      $result = db_select('block')
        ->fields('block')
        ->condition('theme', $theme_key)
        ->execute()
        ->fetchAllAssoc('bid');
      drupal_alter('block_list', $result);
      drupal_alter('context_block_list', $result);
      foreach ($result as $row) {
        if (isset($block_info["{$row->module}-{$row->delta}"])) {
          $block_info["{$row->module}-{$row->delta}"] = (object) array_merge((array) $row, (array) $block_info["{$row->module}-{$row->delta}"]);
          unset($block_info["{$row->module}-{$row->delta}"]->status);
          unset($block_info["{$row->module}-{$row->delta}"]->visibility);
        }
      }
    }
  }
  $blocks = array();

  // No region specified, provide all blocks.
  if (!isset($region)) {
    $blocks = $block_info;
  }
  else {
    foreach ($block_info as $bid => $block) {
      if (isset($block->region) && $block->region == $region) {
        $blocks[$bid] = $block;
      }
    }
  }

  // Add context blocks if provided.
  if (is_object($context) && ($options = $this
    ->fetch_from_context($context))) {
    if (!empty($options['blocks'])) {
      foreach ($options['blocks'] as $block) {
        if (isset($block_info["{$block['module']}-{$block['delta']}"]) && (!isset($region) || !empty($region) && $block['region'] == $region)) {
          $context_block = $block_info["{$block['module']}-{$block['delta']}"];
          $context_block->weight = $block['weight'];
          $context_block->region = $block['region'];
          $context_block->context = !empty($context->name) ? $context->name : 'tempname';
          $blocks[$context_block->bid] = $context_block;
        }
      }
    }
    uasort($blocks, array(
      'context_reaction_block',
      'block_sort',
    ));
  }
  return $blocks;
}