You are here

function block_rebuild in Drupal 9

Same name and namespace in other branches
  1. 8 core/modules/block/block.module \block_rebuild()

Implements hook_rebuild().

7 calls to block_rebuild()
BlockRebuildTest::testRebuildInvalidBlocks in core/modules/block/tests/src/Kernel/BlockRebuildTest.php
@covers ::block_rebuild
BlockRebuildTest::testRebuildNoBlocks in core/modules/block/tests/src/Kernel/BlockRebuildTest.php
@covers ::block_rebuild
BlockRebuildTest::testRebuildNoInvalidBlocks in core/modules/block/tests/src/Kernel/BlockRebuildTest.php
@covers ::block_rebuild
MigrateBlockContentTranslationTest::setUp in core/modules/block/tests/src/Kernel/Migrate/d6/MigrateBlockContentTranslationTest.php
MigrateBlockContentTranslationTest::setUp in core/modules/block/tests/src/Kernel/Migrate/d7/MigrateBlockContentTranslationTest.php

... See full list

File

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

Code

function block_rebuild() {
  foreach (\Drupal::service('theme_handler')
    ->listInfo() as $theme => $data) {
    if ($data->status) {
      $regions = system_region_list($theme);

      /** @var \Drupal\block\BlockInterface[] $blocks */
      $blocks = \Drupal::entityTypeManager()
        ->getStorage('block')
        ->loadByProperties([
        'theme' => $theme,
      ]);
      foreach ($blocks as $block_id => $block) {

        // Disable blocks in invalid regions.
        if (!isset($regions[$block
          ->getRegion()])) {
          if ($block
            ->status()) {
            \Drupal::messenger()
              ->addWarning(t('The block %info was assigned to the invalid region %region and has been disabled.', [
              '%info' => $block_id,
              '%region' => $block
                ->getRegion(),
            ]));
          }
          $block
            ->setRegion(system_default_region($theme))
            ->disable()
            ->save();
        }
      }
    }
  }
}