Same name and namespace in other branches
  1. 4.6.x modules/block.module \_block_rehash()
  2. 5.x modules/block/block.module \_block_rehash()
  3. 6.x modules/block/block.module \_block_rehash()
  4. 7.x modules/block/block.module \_block_rehash()

Update the 'blocks' DB table with the blocks currently exported by modules.

Return value

Blocks currently exported by modules.

1 call to _block_rehash()
block_admin_display in modules/block.module
Generate main block administration form.

File

modules/block.module, line 142
Controls the boxes that are displayed around the main content.

Code

function _block_rehash() {
  global $theme_key;
  init_theme();
  $result = db_query("SELECT * FROM {blocks} WHERE theme = '%s'", $theme_key);
  while ($old_block = db_fetch_object($result)) {
    $old_blocks[$old_block->module][$old_block->delta] = $old_block;
  }
  $blocks = array();
  foreach (module_list() as $module) {
    $module_blocks = module_invoke($module, 'block', 'list');
    if ($module_blocks) {
      foreach ($module_blocks as $delta => $block) {
        $block['module'] = $module;
        $block['delta'] = $delta;

        // If previously written to database, load values.
        if ($old_blocks[$module][$delta]) {
          $block['status'] = $old_blocks[$module][$delta]->status;
          $block['weight'] = $old_blocks[$module][$delta]->weight;
          $block['region'] = $old_blocks[$module][$delta]->region;
          $block['visibility'] = $old_blocks[$module][$delta]->visibility;
          $block['pages'] = $old_blocks[$module][$delta]->pages;
          $block['custom'] = $old_blocks[$module][$delta]->custom;
          $block['throttle'] = $old_blocks[$module][$delta]->throttle;
        }
        else {
          $properties = array(
            'status' => 0,
            'weight' => 0,
            'region' => 'left',
            'pages' => '',
            'custom' => 0,
          );
          foreach ($properties as $property => $default) {
            if (!isset($block[$property])) {
              $block[$property] = $default;
            }
          }
        }
        $blocks[] = $block;
      }
    }
  }
  db_lock_table('blocks');

  // Remove all blocks from table.
  db_query("DELETE FROM {blocks} WHERE theme = '%s'", $theme_key);

  // Reinsert new set of blocks into table.
  foreach ($blocks as $block) {
    db_query("INSERT INTO {blocks} (module, delta, theme, status, weight, region, visibility, pages, custom, throttle) VALUES ('%s', '%s', '%s', %d, %d, '%s', %d, '%s', %d, %d)", $block['module'], $block['delta'], $theme_key, $block['status'], $block['weight'], $block['region'], $block['visibility'], $block['pages'], $block['custom'], $block['throttle']);
  }
  db_unlock_tables();
  return $blocks;
}