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 = \Drupal::entityTypeManager()->getStorage('block')->loadByProperties(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 = \Drupal::entityTypeManager()->getStorage('block')->loadByProperties(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();
    }
  }
}
doc_Drupal
2016-10-29 08:47:55
Comments
Leave a Comment

Please login to continue.