diff --git a/modules/dashboard/dashboard.install b/modules/dashboard/dashboard.install index 5021826..6042aef 100644 --- a/modules/dashboard/dashboard.install +++ b/modules/dashboard/dashboard.install @@ -76,3 +76,74 @@ function dashboard_enable() { function dashboard_uninstall() { variable_del('dashboard_stashed_blocks'); } + +/** + * @addtogroup updates-7.x-extra + * @{ + */ + +/** + * Remove blocks from dashboard that do not exist. + */ +function dashboard_update_7000() { + // If we are enabled, get list of blocks from the database + if (module_exists('dashboard')) { + $result = db_select('block', 'b') + ->fields('b', array('module', 'delta')) + ->condition('b.region', dashboard_regions(), 'IN') + ->execute(); + + foreach ($result as $block) { + $stashed_blocks[] = array( + 'module' => $block->module, + 'delta' => $block->delta, + ); + } + } + else { + // Otherwise get list of blocks from variable. + $stashed_blocks = variable_get('dashboard_stashed_blocks', array()); + } + + $all_blocks = array(); + // Gather all 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) { + $all_blocks[$module][$delta] = $block; + } + } + + $delete_blocks = array(); + // Go through the blocks on the dashboard and make a list of those to delete + foreach ($stashed_blocks as $block) { + // If the block does not exist in all blocks, it needs to be removed + if (!isset($all_blocks[$block['module']][$block['delta']])) { + $delete_blocks[] = $block; + } + } + + if (module_exists('dashboard')) { + if ($delete_blocks) { + $or = db_or(); + foreach ($delete_blocks as $block) { + $or->condition(db_and() + ->condition('module', $block['module']) + ->condition('delta', $block['delta'])); + } + db_delete('block') + ->condition($or) + ->execute(); + } + } + else { + foreach ($delete_blocks as $block) { + unset($stashed_blocks[$block['module']][$block['delta']]); + } + variable_set('dashboard_stashed_blocks', $stashed_blocks); + } +} + +/** + * @} End of "addtogroup updates-7.x-extra". + */