function block_theme_initialize

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.

Parameters

$theme: The name of a theme.

2 calls to block_theme_initialize()
block_themes_enabled in drupal/core/modules/block/block.module
Initializes blocks for enabled themes.
hook_themes_enabled in drupal/core/modules/system/theme.api.php
Respond to themes being enabled.

File

drupal/core/modules/block/block.module, line 414
Controls the visual building blocks a page is constructed with.

Code

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();
    }
  }
}