function BlockHooks::rebuild

Same name and namespace in other branches
  1. 11.x core/modules/block/src/Hook/BlockHooks.php \Drupal\block\Hook\BlockHooks::rebuild()

Implements hook_rebuild().

Attributes

#[Hook('rebuild')]

File

core/modules/block/src/Hook/BlockHooks.php, line 185

Class

BlockHooks
Hook implementations for block.

Namespace

Drupal\block\Hook

Code

public function rebuild() : void {
  foreach ($this->themeHandler
    ->listInfo() as $theme => $theme_extension) {
    if ($theme_extension->status) {
      $regions = $theme_extension->listAllRegions();
      /** @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($this->t('The block %info was assigned to the invalid region %region and has been disabled.', [
              '%info' => $block_id,
              '%region' => $block->getRegion(),
            ]));
          }
          $block->setRegion($theme_extension->getDefaultRegion())
            ->disable()
            ->save();
        }
      }
    }
  }
}

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