function block_theme_initialize

block_theme_initialize($theme)

Assigns an initial, default set of blocks for a theme.

This function is called the first time a new theme is installed. 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.

File

core/modules/block/block.module, line 110
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 = \Drupal::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) {
      if (strpos($default_theme_block_id, $default_theme . '_') === 0) {
        $id = str_replace($default_theme, $theme, $default_theme_block_id);
      }
      else {
        $id = $theme . '_' . $default_theme_block_id;
      }
      $block = $default_theme_block->createDuplicateBlock($id, $theme);
      // If the region isn't supported by the theme, assign the block to the
      // theme's default region.
      if (!isset($regions[$block->getRegion()])) {
        $block->setRegion(system_default_region($theme));
      }
      $block->save();
    }
  }
}

© 2001–2016 by the original authors
Licensed under the GNU General Public License, version 2 and later.
Drupal is a registered trademark of Dries Buytaert.
https://api.drupal.org/api/drupal/core!modules!block!block.module/function/block_theme_initialize/8.1.x