You are here

function _mongodb_block_rehash in MongoDB 7

Updates the block collection with the blocks currently exported by modules.

Parameters

string $theme: The theme to rehash blocks for. If not provided, defaults to the currently used theme.

Return value

mixed Blocks currently exported by modules.

Throws

\MongoConnectionException

\MongoCursorException

\MongoCursorTimeoutException

3 calls to _mongodb_block_rehash()
mongodb_block_page_build in mongodb_block/mongodb_block.module
Implements hook_page_build().
mongodb_block_rehash in mongodb_block/mongodb_block.module
Run _mongodb_block_rehash and go to admin/structure.
mongodb_block_ui_admin_display in mongodb_block_ui/mongodb_block_ui.admin.inc
Menu callback for admin/structure/mongodb_block_ui.

File

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

Code

function _mongodb_block_rehash($theme) {
  $collection = mongodb_collection('block', $theme);
  $regions = system_region_list($theme);
  $ids = [];
  foreach (module_implements('block_info') as $module) {
    if ($blocks_current = module_invoke($module, 'block_info')) {
      foreach ($blocks_current as $delta => $block) {
        $block = [
          '_id' => [
            'module' => $module,
            'delta' => $delta,
          ],
          'module' => $module,
          'delta' => $delta,
        ] + $block + [
          'weight' => 0,
          'pages' => [
            '%',
          ],
          'pages_exclude' => [],
          'status' => 0,
          'region' => MONGODB_BLOCK_REGION_NONE,
        ];
        if (isset($block['node_type']) && $block['pages'] == [
          '%',
        ]) {
          $block['pages'] = [
            'node/%',
          ];
        }
        else {
          if (!isset($block['node_type'])) {
            $block['node_type'] = [
              '*',
            ];
          }
        }
        $blocks[$module . '_' . $delta] = $block;
      }
    }
  }
  drupal_alter('mongodb_block_info', $blocks, $theme);

  // Only store the block in MongoDB if the status is enabled.
  foreach ($blocks as $key => $block) {
    if ($block['region'] != MONGODB_BLOCK_REGION_NONE && !isset($regions[$block['region']])) {
      $blocks[$key]['status'] = 0;
      $blocks[$key]['region'] = MONGODB_BLOCK_REGION_NONE;
      $block = $blocks[$key];
    }
    if (!empty($block['status'])) {
      $ids[] = $block['_id'];
      mongodb_block_save_block($collection, $block);
    }
    else {
      $blocks[$key]['status'] = 0;
      $blocks[$key]['region'] = MONGODB_BLOCK_REGION_NONE;
    }
  }
  $collection
    ->remove([
    '_id' => [
      '$nin' => $ids,
    ],
  ], mongodb_default_write_options());
  return $blocks;
}