You are here

function _block_skinr_load_blocks in Skinr 7.2

Same name and namespace in other branches
  1. 8.2 modules/block.skinr.inc \_block_skinr_load_blocks()

Returns a list of enabled blocks for a given theme.

Based on _block_rehash(), but without the blocks table rebuild part.

Return value

An array of blocks.

See also

_block_rehash()

2 calls to _block_skinr_load_blocks()
block_skinr_ui_element_options in modules/block.skinr.inc
Implements hook_skinr_ui_element_options().
block_skinr_ui_element_title in modules/block.skinr.inc
Implements hook_skinr_ui_element_title().

File

modules/block.skinr.inc, line 75
Implements Skinr hooks for block.module.

Code

function _block_skinr_load_blocks($theme) {
  $cache =& drupal_static(__FUNCTION__, array());
  if (!isset($cache['code_blocks'])) {
    $cache['code_blocks'] = array();

    // Gather the blocks defined by modules.
    foreach (module_implements('block_info') as $module) {
      $module_blocks = module_invoke($module, 'block_info');
      foreach ($module_blocks as $delta => $block) {

        // Add identifiers.
        $block['module'] = $module;
        $block['delta'] = $delta;
        $block['theme'] = $theme;
        $cache['code_blocks'][$module][$delta] = $block;
      }
    }
  }
  if (!isset($cache['blocks'][$theme])) {
    $regions = system_region_list($theme);
    $current_blocks = $cache['code_blocks'];

    // These are {block}.bid values to be kept.
    $bids = array();
    $or = db_or();
    foreach ($cache['code_blocks'] as $module => $module_blocks) {
      foreach ($module_blocks as $delta => $block) {

        // Compile a condition to retrieve this block from the database.
        $condition = db_and()
          ->condition('module', $module)
          ->condition('delta', $delta);
        $or
          ->condition($condition);
      }
    }
    $database_blocks = db_select('block', 'b')
      ->fields('b')
      ->condition($or)
      ->condition('theme', $theme)
      ->execute();
    foreach ($database_blocks as $block) {

      // Preserve info which is not in the database.
      $block->info = $current_blocks[$block->module][$block->delta]['info'];

      // The cache mode can only by set from hook_block_info(), so that has
      // precedence over the database's value.
      if (isset($current_blocks[$block->module][$block->delta]['cache'])) {
        $block->cache = $current_blocks[$block->module][$block->delta]['cache'];
      }

      // Blocks stored in the database override the blocks defined in code.
      $current_blocks[$block->module][$block->delta] = get_object_vars($block);

      // Preserve this block.
      $bids[$block->bid] = $block->bid;
    }
    drupal_alter('block_info', $current_blocks, $theme, $cache['code_blocks']);
    foreach ($current_blocks as $module => $module_blocks) {
      foreach ($module_blocks as $delta => $block) {
        if (!empty($block['region']) && $block['region'] != BLOCK_REGION_NONE && !isset($regions[$block['region']])) {

          // Disabled modules are moved into the BLOCK_REGION_NONE later so no
          // need to move the block to another region.
          $current_blocks[$module][$delta]['status'] = 0;
        }

        // Set region to none if not enabled and make sure status is set.
        if (empty($block['status'])) {
          $current_blocks[$module][$delta]['status'] = 0;
          $current_blocks[$module][$delta]['region'] = BLOCK_REGION_NONE;
        }
      }
    }
    $cache['blocks'][$theme] = array();
    foreach ($current_blocks as $module => $module_blocks) {
      foreach ($module_blocks as $delta => $block) {
        if (!$block['status']) {
          continue;
        }
        $cache['blocks'][$theme][$block['module'] . '__' . $block['delta']] = (object) $block;
      }
    }
    asort($cache['blocks'][$theme]);
  }
  return $cache['blocks'][$theme];
}