block_rebuild

block_rebuild()

Implements hook_rebuild().

File

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

Code

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
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.
        $region = $block->getRegion();
        if ($region !== BlockInterface::BLOCK_REGION_NONE) {
          if (!empty($region) && !isset($regions[$region]) && $block->status()) {
            drupal_set_message(t('The block %info was assigned to the invalid region %region and has been disabled.', ['%info' => $block_id, '%region' => $region]), 'warning');
            $block->disable();
          }
          // Set region to none if not enabled.
          if (!$block->status()) {
            $block->setRegion(BlockInterface::BLOCK_REGION_NONE);
            $block->save();
          }
        }
      }
    }
  }
}
doc_Drupal
2025-01-10 15:47:30
Comments
Leave a Comment

Please login to continue.