_block_rehash
- Versions
- 4.6
_block_rehash($order_by= array('weight'))- 4.7 – 6
_block_rehash()- 7
_block_rehash($theme = NULL)
Update the 'block' DB table with the blocks currently exported by modules.
Parameters
$theme The theme to rehash blocks for. If not provided, defaults to the currently used theme.
Return value
Blocks currently exported by modules.
Code
modules/block/block.module, line 307
<?php
function _block_rehash($theme = NULL) {
global $theme_key;
drupal_theme_initialize();
if (!isset($theme)) {
// If theme is not specifically set, rehash for the current theme.
$theme = $theme_key;
}
$old_blocks = array();
$result = db_query("SELECT * FROM {block} WHERE theme = :theme", array(':theme' => $theme));
foreach ($result as $old_block) {
$old_block = is_object($old_block) ? get_object_vars($old_block) : $old_block;
$old_blocks[$old_block['module']][$old_block['delta']] = $old_block;
}
$blocks = array();
// Valid region names for the theme.
$regions = system_region_list($theme);
foreach (module_implements('block_info') as $module) {
$module_blocks = module_invoke($module, 'block_info');
if ($module_blocks) {
foreach ($module_blocks as $delta => $block) {
if (empty($old_blocks[$module][$delta])) {
// If it's a new block, add identifiers.
$block['module'] = $module;
$block['delta'] = $delta;
$block['theme'] = $theme;
if (!isset($block['pages'])) {
// {block}.pages is type 'text', so it cannot have a
// default value, and not null, so we need to provide
// value if the module did not.
$block['pages'] = '';
}
// Add defaults and save it into the database.
drupal_write_record('block', $block);
// Set region to none if not enabled.
$block['region'] = $block['status'] ? $block['region'] : BLOCK_REGION_NONE;
// Add to the list of blocks we return.
$blocks[] = $block;
}
else {
// If it's an existing block, database settings should overwrite
// the code. But aside from 'info' everything that's definable in
// code is stored in the database and we do not store 'info', so we
// do not need to update the database here.
// Add 'info' to this block.
$old_blocks[$module][$delta]['info'] = $block['info'];
// If the region name does not exist, disable the block and assign it to none.
if (!empty($old_blocks[$module][$delta]['region']) && !isset($regions[$old_blocks[$module][$delta]['region']])) {
drupal_set_message(t('The block %info was assigned to the invalid region %region and has been disabled.', array('%info' => $old_blocks[$module][$delta]['info'], '%region' => $old_blocks[$module][$delta]['region'])), 'warning');
$old_blocks[$module][$delta]['status'] = 0;
$old_blocks[$module][$delta]['region'] = BLOCK_REGION_NONE;
}
else {
$old_blocks[$module][$delta]['region'] = $old_blocks[$module][$delta]['status'] ? $old_blocks[$module][$delta]['region'] : BLOCK_REGION_NONE;
}
// Add this block to the list of blocks we return.
$blocks[] = $old_blocks[$module][$delta];
// Remove this block from the list of blocks to be deleted.
unset($old_blocks[$module][$delta]);
}
}
}
}
// Remove blocks that are no longer defined by the code from the database.
foreach ($old_blocks as $module => $old_module_blocks) {
foreach ($old_module_blocks as $delta => $block) {
db_delete('block')
->condition('module', $module)
->condition('delta', $delta)
->condition('theme', $theme)
->execute();
}
}
return $blocks;
}
?>Login or register to post comments 