Assigns an initial, default set of blocks for a theme.
This function is called the first time a new theme is enabled. The new theme gets a copy of the default theme's blocks, with the difference that if a particular region isn't available in the new theme, the block is assigned to the new theme's default region.
$theme: The name of a theme.
function block_theme_initialize($theme) {
// Initialize theme's blocks if none already registered.
$has_blocks = entity_load_multiple_by_properties('block', array(
'theme' => $theme,
));
if (!$has_blocks) {
$default_theme = config('system.theme')
->get('default');
// Apply only to new theme's visible regions.
$regions = system_region_list($theme, REGIONS_VISIBLE);
$default_theme_blocks = entity_load_multiple_by_properties('block', array(
'theme' => $default_theme,
));
foreach ($default_theme_blocks as $default_theme_block_id => $default_theme_block) {
list(, $machine_name) = explode('.', $default_theme_block_id);
$block = $default_theme_block
->createDuplicate();
$block
->set('id', $theme . '.' . $machine_name);
// If the region isn't supported by the theme, assign the block to the
// theme's default region.
if (!isset($regions[$block
->get('region')])) {
$block
->set('region', system_default_region($theme));
}
$block
->save();
}
}
}