function block_rebuild

Same name and namespace in other branches
  1. 9 core/modules/block/block.module \block_rebuild()
  2. 8.9.x core/modules/block/block.module \block_rebuild()
  3. 10 core/modules/block/block.module \block_rebuild()

Implements hook_rebuild().

9 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 170

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

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.