You are here

function mongodb_block_page_build in MongoDB 7

Implements hook_page_build().

Renders blocks into their regions.

See also

\block_page_build()

File

mongodb_block/mongodb_block.module, line 83
Controls the visual building blocks a page is constructed with.

Code

function mongodb_block_page_build(&$page) {
  global $theme;

  // The theme system might not yet be initialized. We need $theme.
  drupal_theme_initialize();

  // Fetch a list of regions for the current theme.
  $all_regions = system_region_list($theme);
  $collection = mongodb_collection('block', $theme);

  // Fastest way to check if collection is not empty, per #2072135.
  if (!$collection
    ->find()
    ->limit(1)
    ->hasNext()) {
    _mongodb_block_rehash($theme);
  }
  $query = [];
  if ($node = menu_get_object()) {
    $query['node_type']['$in'] = [
      '*',
      $node->type,
    ];
  }
  $parts = array_slice(arg(), 0, MENU_MAX_PARTS);
  $ancestors = menu_get_ancestors($parts);
  if ($_GET['q'] != request_path()) {
    $request_parts = array_slice(explode('/', request_path()), 0, MENU_MAX_PARTS);
    $ancestors = array_merge($ancestors, menu_get_ancestors($request_parts));
  }
  $ancestors[] = '%';
  if (drupal_is_front_page()) {
    $ancestors[] = '<front>';
  }
  $query['pages']['$in'] = $ancestors;
  $query['pages_exclude']['$nin'] = $ancestors;
  $query['region']['$in'] = array_keys($all_regions);
  $blocks_result = $collection
    ->find($query)
    ->sort(array(
    'weight' => 1,
  ));
  $regions_blocks = array();
  foreach ($blocks_result as $block) {
    if (!isset($regions_blocks[$block['region']])) {
      $regions_blocks[$block['region']] = array();
    }
    $regions_blocks[$block['region']][] = $block;
  }
  foreach ($regions_blocks as $region => $region_blocks) {
    $region_blocks = mongodb_block_render_blocks($region_blocks);
    if (isset($page[$region])) {
      $page[$region] = array_merge($page[$region], mongodb_block_get_renderable_array($region_blocks));
    }
    else {
      $page[$region] = mongodb_block_get_renderable_array($region_blocks);
    }
  }
}