function BlockHooks::rebuild

Implements hook_rebuild().

Attributes

#[Hook('rebuild')]

File

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

Class

BlockHooks
Hook implementations for block.

Namespace

Drupal\block\Hook

Code

public function rebuild() : void {
  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($this->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.