diff --git a/core/modules/block/block.install b/core/modules/block/block.install index 305f438..106815c 100644 --- a/core/modules/block/block.install +++ b/core/modules/block/block.install @@ -124,29 +124,5 @@ function block_update_8003() { } /** - * Disable blocks that are placed into the "disabled" region. - */ -function block_update_8004() { - // Find all blocks in the disabled region. - /** @var \Drupal\block\BlockInterface[] $blocks */ - $block_ids = \Drupal::entityManager() - ->getStorage('block') - ->getQuery() - ->condition('region', -1) - ->execute(); - - $config_factory = \Drupal::configFactory(); - - // Disable each block and assign them to the default region. - foreach ($block_ids as $block_id) { - $block_config = $config_factory->getEditable('block.block.' . $block_id); - $block_config - ->set('region', system_default_region($block_config->get('theme'))) - ->set('status', FALSE) - ->save(); - } -} - -/** * @} End of "addtogroup updates-8.0.0-beta". */ diff --git a/core/modules/block/block.module b/core/modules/block/block.module index 5b30b4a..e415524 100644 --- a/core/modules/block/block.module +++ b/core/modules/block/block.module @@ -144,8 +144,10 @@ function block_rebuild() { $blocks = \Drupal::entityTypeManager()->getStorage('block')->loadByProperties(['theme' => $theme]); foreach ($blocks as $block_id => $block) { // Disable blocks in invalid regions. - if (!isset($regions[$block->getRegion()]) && $block->status()) { - drupal_set_message(t('The block %info was assigned to the invalid region %region and has been disabled.', ['%info' => $block_id, '%region' => $block->getRegion()]), 'warning'); + if (!isset($regions[$block->getRegion()])) { + if ($block->status()) { + drupal_set_message(t('The block %info was assigned to the invalid region %region and has been disabled.', ['%info' => $block_id, '%region' => $block->getRegion()]), 'warning'); + } $block ->setRegion(system_default_region($theme)) ->disable() diff --git a/core/modules/block/block.post_update.php b/core/modules/block/block.post_update.php index f208f65..7bab413 100644 --- a/core/modules/block/block.post_update.php +++ b/core/modules/block/block.post_update.php @@ -75,5 +75,12 @@ function block_post_update_disable_blocks_with_missing_contexts() { } /** + * Disable blocks that are placed into the "disabled" region. + */ +function block_post_update_disabled_region_update() { + // An empty update will flush caches, forcing block_rebuild() to run. +} + +/** * @} End of "addtogroup updates-8.0.0-beta". */ diff --git a/core/modules/block/src/Entity/Block.php b/core/modules/block/src/Entity/Block.php index 1a0c82c..63b5205 100644 --- a/core/modules/block/src/Entity/Block.php +++ b/core/modules/block/src/Entity/Block.php @@ -330,4 +330,21 @@ public function createDuplicateBlock($new_id = NULL, $new_theme = NULL) { return $duplicate; } + /** + * {@inheritdoc} + */ + public function preSave(EntityStorageInterface $storage) { + parent::preSave($storage); + + // Ensure the region is valid to mirror the behavior of block_rebuild(). + // This is done primarily for backwards compatibility support of + // \Drupal\block\BlockInterface::BLOCK_REGION_NONE. + $regions = system_region_list($this->theme); + if (!isset($regions[$this->region]) && $this->status()) { + $this + ->setRegion(system_default_region($this->theme)) + ->disable(); + } + } + } diff --git a/core/modules/block/src/Tests/Update/BlockRemoveDisabledRegionUpdateTest.php b/core/modules/block/src/Tests/Update/BlockRemoveDisabledRegionUpdateTest.php index c359b3b..0d9826f 100644 --- a/core/modules/block/src/Tests/Update/BlockRemoveDisabledRegionUpdateTest.php +++ b/core/modules/block/src/Tests/Update/BlockRemoveDisabledRegionUpdateTest.php @@ -53,10 +53,10 @@ public function testUpdateHookN() { [':block' => 'edit-blocks-pagetitle-1', ':status' => 'block-disabled', ':region' => 'header']); $this->assertTrue(!empty($element)); - // Verify that an enabled block is in the default region. + // Verify that an enabled block is now disabled and in the default region. $this->drupalGet('admin/structure/block'); $element = $this->xpath("//tr[contains(@data-drupal-selector, :block) and contains(@class, :status)]//select/option[@selected and @value=:region]", - [':block' => 'edit-blocks-pagetitle-2', ':status' => 'block-enabled', ':region' => 'header']); + [':block' => 'edit-blocks-pagetitle-2', ':status' => 'block-disabled', ':region' => 'header']); $this->assertTrue(!empty($element)); } diff --git a/core/modules/block/tests/src/Kernel/BlockRebuildTest.php b/core/modules/block/tests/src/Kernel/BlockRebuildTest.php index e2d555d..39ed44e 100644 --- a/core/modules/block/tests/src/Kernel/BlockRebuildTest.php +++ b/core/modules/block/tests/src/Kernel/BlockRebuildTest.php @@ -29,6 +29,7 @@ protected function setUp() { parent::setUp(); $this->container->get('theme_installer')->install(['stable', 'classy']); + $this->container->get('config.factory')->getEditable('system.theme')->set('default', 'classy')->save(); } /** @@ -55,7 +56,7 @@ public function testRebuildNoBlocks() { * @covers ::block_rebuild */ public function testRebuildNoInvalidBlocks() { - $this->placeBlock('system_powered_by_block', ['region' => 'content', 'theme' => 'classy']); + $this->placeBlock('system_powered_by_block', ['region' => 'content']); block_rebuild(); $messages = drupal_get_messages(); @@ -66,20 +67,38 @@ public function testRebuildNoInvalidBlocks() { * @covers ::block_rebuild */ public function testRebuildInvalidBlocks() { - $this->placeBlock('system_powered_by_block', ['region' => 'content', 'theme' => 'classy']); - $block = $this->placeBlock('system_powered_by_block', ['region' => 'INVALID', 'theme' => 'classy']); - $this->assertSame('INVALID', $block->getRegion()); - $this->assertTrue($block->status()); + $this->placeBlock('system_powered_by_block', ['region' => 'content']); + $block1 = $this->placeBlock('system_powered_by_block'); + $block2 = $this->placeBlock('system_powered_by_block'); + $block2->disable()->save(); + // Use the config API directly to bypass Block::preSave(). + \Drupal::configFactory()->getEditable('block.block.' . $block1->id())->set('region', 'INVALID')->save(); + \Drupal::configFactory()->getEditable('block.block.' . $block2->id())->set('region', 'INVALID')->save(); + + // Reload block entities. + $block1 = Block::load($block1->id()); + $block2 = Block::load($block2->id()); + + $this->assertSame('INVALID', $block1->getRegion()); + $this->assertTrue($block1->status()); + $this->assertSame('INVALID', $block2->getRegion()); + $this->assertFalse($block2->status()); block_rebuild(); - // Reload block entity. - $block = Block::load($block->id()); + + // Reload block entities. + $block1 = Block::load($block1->id()); + $block2 = Block::load($block2->id()); $messages = drupal_get_messages(); - $expected = ['warning' => [new TranslatableMarkup('The block %info was assigned to the invalid region %region and has been disabled.', ['%info' => $block->id(), '%region' => 'INVALID'])]]; + $expected = ['warning' => [new TranslatableMarkup('The block %info was assigned to the invalid region %region and has been disabled.', ['%info' => $block1->id(), '%region' => 'INVALID'])]]; $this->assertEquals($expected, $messages); - $this->assertSame(system_default_region('classy'), $block->getRegion()); - $this->assertFalse($block->status()); + + $default_region = system_default_region('classy'); + $this->assertSame($default_region, $block1->getRegion()); + $this->assertFalse($block1->status()); + $this->assertSame($default_region, $block2->getRegion()); + $this->assertFalse($block2->status()); } } diff --git a/core/modules/system/tests/fixtures/update/block.block.secondtestfor2513534.yml b/core/modules/system/tests/fixtures/update/block.block.secondtestfor2513534.yml index b1dbbae..2999cab 100644 --- a/core/modules/system/tests/fixtures/update/block.block.secondtestfor2513534.yml +++ b/core/modules/system/tests/fixtures/update/block.block.secondtestfor2513534.yml @@ -6,7 +6,7 @@ dependencies: - bartik id: pagetitle_2 theme: bartik -region: header +region: '-1' weight: null provider: null plugin: page_title_block