Verified Commit ac357941 authored by Dave Long's avatar Dave Long
Browse files

refactor: #3566782 Deprecate block_theme_initialize() and move the logic to a service

By: claudiu.cristea
By: nicxvan
By: berdir
By: phenaproxima
parent efe74934
Loading
Loading
Loading
Loading
Loading
+1 −1
Changes for core/lib/Drupal/Core/Render/theme.api.php: 1 added line, 1 removed line.
Original line number Diff line number Diff line
@@ -789,7 +789,7 @@ function hook_theme_suggestions_HOOK_alter(array &$suggestions, array &$variable
 */
function hook_themes_installed($theme_list): void {
  foreach ($theme_list as $theme) {
    block_theme_initialize($theme);
    // ...perform some action based on the $theme.
  }
}

+7 −0
Changes for core/modules/block/block.module: 7 added lines, 0 removed lines.
Original line number Diff line number Diff line
@@ -14,8 +14,15 @@
 *
 * @param string $theme
 *   The name of a theme.
 *
 * @deprecated in drupal:11.4.0 and is removed from drupal:13.0.0. No
 *   replacement is provided.
 *
 * @see https://www.drupal.org/node/3566783
 */
function block_theme_initialize($theme): void {
  @trigger_error(__FUNCTION__ . '() is deprecated in drupal:11.4.0 and is removed from drupal:13.0.0. No replacement is provided. See https://www.drupal.org/node/3566783', E_USER_DEPRECATED);

  // Initialize theme's blocks if none already registered.
  $has_blocks = \Drupal::entityTypeManager()->getStorage('block')->loadByProperties(['theme' => $theme]);
  if (!$has_blocks) {
+43 −2
Changes for core/modules/block/src/Hook/BlockHooks.php: 43 added lines, 2 removed lines.
Original line number Diff line number Diff line
@@ -166,7 +166,7 @@ public function modulesInstalled($modules, bool $is_syncing): void {
    $profile = \Drupal::installProfile();
    if (in_array($profile, $modules, TRUE)) {
      foreach (\Drupal::service('theme_handler')->listInfo() as $theme => $data) {
        block_theme_initialize($theme);
        $this->themeInitialize($theme);
      }
    }
  }
@@ -198,7 +198,7 @@ public function themesInstalled($theme_list): void {
    foreach ($theme_list as $theme) {
      // Don't initialize themes that are not displayed in the UI.
      if (\Drupal::service('theme_handler')->hasUi($theme)) {
        block_theme_initialize($theme);
        $this->themeInitialize($theme);
      }
    }
  }
@@ -303,4 +303,45 @@ public function blockBuildLocalActionsBlockAlter(array &$build, BlockPluginInter
    ];
  }

  /**
   * 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.
   *
   * @param string $theme
   *   The name of a theme.
   */
  protected function themeInitialize(string $theme): void {
    $storage = \Drupal::entityTypeManager()->getStorage('block');

    // Initialize theme's blocks if none already registered.
    $has_blocks = $storage->loadByProperties(['theme' => $theme]);
    if (!$has_blocks) {
      $default_theme = \Drupal::configFactory()->get('system.theme')->get('default');
      // Apply only to new theme's visible regions.
      $regions = system_region_list($theme, REGIONS_VISIBLE);
      $default_theme_blocks = $storage->loadByProperties(['theme' => $default_theme]);
      $block_repository = \Drupal::service('block.repository');
      foreach ($default_theme_blocks as $default_theme_block_id => $default_theme_block) {
        if (str_starts_with($default_theme_block_id, $default_theme . '_')) {
          $id = str_replace($default_theme . '_', '', $default_theme_block_id);
        }
        else {
          $id = $default_theme_block_id;
        }
        $id = $block_repository->getUniqueMachineName($id, $theme);
        $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();
      }
    }
  }

}