diff --git a/core/modules/block/block.module b/core/modules/block/block.module index 1b01d45..a7dd7dc 100644 --- a/core/modules/block/block.module +++ b/core/modules/block/block.module @@ -145,20 +145,11 @@ function block_rebuild() { $blocks = entity_load_multiple_by_properties('block', ['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(); - } + 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'); + $block->disable()->save(); } } - return $blocks; } } } diff --git a/core/modules/block/block.routing.yml b/core/modules/block/block.routing.yml index 2f26a38..6c7ffef 100644 --- a/core/modules/block/block.routing.yml +++ b/core/modules/block/block.routing.yml @@ -25,6 +25,22 @@ entity.block.edit_form: requirements: _entity_access: 'block.update' +entity.block.enable: + path: '/admin/structure/block/manage/{block}/enable' + defaults: + _controller: '\Drupal\block\Controller\BlockController::performOperation' + op: enable + requirements: + _entity_access: 'block.enable' + +entity.block.disable: + path: '/admin/structure/block/manage/{block}/disable' + defaults: + _controller: '\Drupal\block\Controller\BlockController::performOperation' + op: disable + requirements: + _entity_access: 'block.disable' + block.admin_display: path: '/admin/structure/block' defaults: diff --git a/core/modules/block/css/block.admin.css b/core/modules/block/css/block.admin.css index d9f32a2..71bf82c 100644 --- a/core/modules/block/css/block.admin.css +++ b/core/modules/block/css/block.admin.css @@ -50,6 +50,11 @@ a.block-demo-backlink:hover { content: ':'; } +.block-disabled { + color: #999; + background-color: #eee; +} + /* Wide screens */ @media screen and (min-width: 780px), diff --git a/core/modules/block/src/BlockForm.php b/core/modules/block/src/BlockForm.php index ac9406a..9f28c43 100644 --- a/core/modules/block/src/BlockForm.php +++ b/core/modules/block/src/BlockForm.php @@ -170,7 +170,7 @@ public function form(array $form, FormStateInterface $form_state) { '#title' => $this->t('Region'), '#description' => $this->t('Select the region where this block should be displayed.'), '#default_value' => $entity->getRegion(), - '#empty_value' => BlockInterface::BLOCK_REGION_NONE, + '#required' => TRUE, '#options' => system_region_list($theme, REGIONS_VISIBLE), '#prefix' => '
', '#suffix' => '
', diff --git a/core/modules/block/src/BlockInterface.php b/core/modules/block/src/BlockInterface.php index 39ad9a9..48ce6e4 100644 --- a/core/modules/block/src/BlockInterface.php +++ b/core/modules/block/src/BlockInterface.php @@ -20,11 +20,6 @@ const BLOCK_LABEL_VISIBLE = 'visible'; /** - * Denotes that a block is not enabled in any region and should not be shown. - */ - const BLOCK_REGION_NONE = -1; - - /** * Returns the plugin instance. * * @return \Drupal\Core\Block\BlockPluginInterface diff --git a/core/modules/block/src/BlockListBuilder.php b/core/modules/block/src/BlockListBuilder.php index 32d5581..83ada1a 100644 --- a/core/modules/block/src/BlockListBuilder.php +++ b/core/modules/block/src/BlockListBuilder.php @@ -176,6 +176,7 @@ protected function buildBlocksForm() { 'weight' => $entity->getWeight(), 'entity' => $entity, 'category' => $definition['category'], + 'status' => $entity->status(), ); } @@ -206,8 +207,7 @@ protected function buildBlocksForm() { // Loop over each region and build blocks. $regions = $this->systemRegionList($this->getThemeName(), REGIONS_VISIBLE); - $block_regions_with_disabled = $regions + array(BlockInterface::BLOCK_REGION_NONE => BlockInterface::BLOCK_REGION_NONE); - foreach ($block_regions_with_disabled as $region => $title) { + foreach ($regions as $region => $title) { $form['#tabledrag'][] = array( 'action' => 'match', 'relationship' => 'sibling', @@ -229,7 +229,7 @@ protected function buildBlocksForm() { ), ); $form['region-' . $region]['title'] = array( - '#markup' => $region != BlockInterface::BLOCK_REGION_NONE ? $title : $this->t('Disabled', array(), array('context' => 'Plural')), + '#markup' => $title, '#wrapper_attributes' => array( 'colspan' => 5, ), @@ -260,12 +260,13 @@ protected function buildBlocksForm() { 'class' => array('draggable'), ), ); + $form[$entity_id]['#attributes']['class'][] = $info['status'] ? 'block-enabled' : 'block-disabled'; if ($placement && $placement == Html::getClass($entity_id)) { $form[$entity_id]['#attributes']['class'][] = 'color-warning'; $form[$entity_id]['#attributes']['class'][] = 'js-block-placed'; } $form[$entity_id]['info'] = array( - '#markup' => SafeMarkup::checkPlain($info['label']), + '#markup' => $info['status'] ? SafeMarkup::checkPlain($info['label']) : SafeMarkup::format('@label (disabled)', ['@label' => $info['label']]), '#wrapper_attributes' => array( 'class' => array('block'), ), @@ -276,7 +277,7 @@ protected function buildBlocksForm() { $form[$entity_id]['region-theme']['region'] = array( '#type' => 'select', '#default_value' => $region, - '#empty_value' => BlockInterface::BLOCK_REGION_NONE, + '#required' => TRUE, '#title' => $this->t('Region for @block block', array('@block' => $info['label'])), '#title_display' => 'invisible', '#options' => $regions, @@ -438,12 +439,6 @@ public function submitForm(array &$form, FormStateInterface $form_state) { $entity_values = $form_state->getValue(array('blocks', $entity_id)); $entity->setWeight($entity_values['weight']); $entity->setRegion($entity_values['region']); - if ($entity->getRegion() == BlockInterface::BLOCK_REGION_NONE) { - $entity->disable(); - } - else { - $entity->enable(); - } $entity->save(); } drupal_set_message(t('The block settings have been updated.')); diff --git a/core/modules/block/src/Controller/BlockController.php b/core/modules/block/src/Controller/BlockController.php index 66ecbbd..2265e2f 100644 --- a/core/modules/block/src/Controller/BlockController.php +++ b/core/modules/block/src/Controller/BlockController.php @@ -7,6 +7,7 @@ namespace Drupal\block\Controller; +use Drupal\block\BlockInterface; use Drupal\Core\Controller\ControllerBase; use Drupal\Core\Extension\ThemeHandler; use Drupal\Core\Extension\ThemeHandlerInterface; @@ -45,6 +46,23 @@ public static function create(ContainerInterface $container) { } /** + * Calls a method on a block and reloads the listing page. + * + * @param \Drupal\block\BlockInterface $block + * The block being acted upon. + * @param string $op + * The operation to perform, e.g., 'enable' or 'disable'. + * + * @return \Symfony\Component\HttpFoundation\RedirectResponse + * A redirect back to the listing page. + */ + public function performOperation(BlockInterface $block, $op) { + $block->$op()->save(); + drupal_set_message($this->t('The block settings have been updated.')); + return $this->redirect('block.admin_display'); + } + + /** * Returns a block theme demo page. * * @param string $theme diff --git a/core/modules/block/src/Entity/Block.php b/core/modules/block/src/Entity/Block.php index 948d293..f503779 100644 --- a/core/modules/block/src/Entity/Block.php +++ b/core/modules/block/src/Entity/Block.php @@ -33,11 +33,14 @@ * }, * admin_permission = "administer blocks", * entity_keys = { - * "id" = "id" + * "id" = "id", + * "status" = "status" * }, * links = { * "delete-form" = "/admin/structure/block/manage/{block}/delete", - * "edit-form" = "/admin/structure/block/manage/{block}" + * "edit-form" = "/admin/structure/block/manage/{block}", + * "enable" = "/admin/structure/block/manage/{block}/enable", + * "disable" = "/admin/structure/block/manage/{block}/disable", * }, * config_export = { * "id", @@ -75,7 +78,7 @@ class Block extends ConfigEntityBase implements BlockInterface, EntityWithPlugin * * @var string */ - protected $region = self::BLOCK_REGION_NONE; + protected $region; /** * The block weight. @@ -214,13 +217,13 @@ public static function sort(ConfigEntityInterface $a, ConfigEntityInterface $b) if ($status !== 0) { return $status; } - // Sort by weight, unless disabled. - if ($a->getRegion() != static::BLOCK_REGION_NONE) { - $weight = $a->getWeight() - $b->getWeight(); - if ($weight) { - return $weight; - } + + // Sort by weight. + $weight = $a->getWeight() - $b->getWeight(); + if ($weight) { + return $weight; } + // Sort by label. return strcmp($a->label(), $b->label()); } diff --git a/core/modules/block/src/Tests/BlockStorageUnitTest.php b/core/modules/block/src/Tests/BlockStorageUnitTest.php index f1d435a..1c57c34 100644 --- a/core/modules/block/src/Tests/BlockStorageUnitTest.php +++ b/core/modules/block/src/Tests/BlockStorageUnitTest.php @@ -90,7 +90,7 @@ protected function createTests() { 'dependencies' => array('module' => array('block_test'), 'theme' => array('stark')), 'id' => 'test_block', 'theme' => 'stark', - 'region' => '-1', + 'region' => NULL, 'weight' => NULL, 'provider' => NULL, 'plugin' => 'test_html', @@ -120,7 +120,7 @@ protected function loadTests() { $this->assertTrue($entity instanceof Block, 'The loaded entity is a Block.'); // Verify several properties of the block. - $this->assertEqual($entity->getRegion(), '-1'); + $this->assertEqual($entity->getRegion(), NULL); $this->assertTrue($entity->status()); $this->assertEqual($entity->getTheme(), 'stark'); $this->assertTrue($entity->uuid()); diff --git a/core/modules/block/src/Tests/BlockTest.php b/core/modules/block/src/Tests/BlockTest.php index 8c65bc3..8e4e5e4 100644 --- a/core/modules/block/src/Tests/BlockTest.php +++ b/core/modules/block/src/Tests/BlockTest.php @@ -155,10 +155,9 @@ function testBlock() { $this->moveBlockToRegion($block, $region); } - // Set the block to the disabled region. - $edit = array(); - $edit['blocks[' . $block['id'] . '][region]'] = -1; - $this->drupalPostForm('admin/structure/block', $edit, t('Save blocks')); + // Disable the block. + $this->drupalGet('admin/structure/block'); + $this->clickLink('Disable'); // Confirm that the block is now listed as disabled. $this->assertText(t('The block settings have been updated.'), 'Block successfully move to disabled region.'); diff --git a/core/modules/block/src/Tests/BlockUiTest.php b/core/modules/block/src/Tests/BlockUiTest.php index aba4ba1..8dfcc79 100644 --- a/core/modules/block/src/Tests/BlockUiTest.php +++ b/core/modules/block/src/Tests/BlockUiTest.php @@ -177,12 +177,15 @@ public function testMachineNameSuggestion() { $url = 'admin/structure/block/add/test_block_instantiation/classy'; $this->drupalGet($url); $this->assertFieldByName('id', 'displaymessage', 'Block form uses raw machine name suggestion when no instance already exists.'); - $this->drupalPostForm($url, array(), 'Save block'); + $edit = ['region' => 'content']; + $this->drupalPostForm($url, $edit, 'Save block'); + $this->assertText('The block configuration has been saved.'); // Now, check to make sure the form starts by autoincrementing correctly. $this->drupalGet($url); $this->assertFieldByName('id', 'displaymessage_2', 'Block form appends _2 to plugin-suggested machine name when an instance already exists.'); - $this->drupalPostForm($url, array(), 'Save block'); + $this->drupalPostForm($url, $edit, 'Save block'); + $this->assertText('The block configuration has been saved.'); // And verify that it continues working beyond just the first two. $this->drupalGet($url); diff --git a/core/modules/block/src/Tests/Views/DisplayBlockTest.php b/core/modules/block/src/Tests/Views/DisplayBlockTest.php index 3f96ab1..297c6ae 100644 --- a/core/modules/block/src/Tests/Views/DisplayBlockTest.php +++ b/core/modules/block/src/Tests/Views/DisplayBlockTest.php @@ -191,8 +191,10 @@ public function testViewsBlockForm() { // Test that that machine name field is hidden from display and has been // saved as expected from the default value. $this->assertNoFieldById('edit-machine-name', 'views_block__test_view_block_1', 'The machine name is hidden on the views block form.'); + // Save the block. - $this->drupalPostForm(NULL, array(), t('Save block')); + $edit = ['region' => 'content']; + $this->drupalPostForm(NULL, $edit, t('Save block')); $storage = $this->container->get('entity.manager')->getStorage('block'); $block = $storage->load('views_block__test_view_block_block_1'); // This will only return a result if our new block has been created with the @@ -201,7 +203,7 @@ public function testViewsBlockForm() { for ($i = 2; $i <= 3; $i++) { // Place the same block again and make sure we have a new ID. - $this->drupalPostForm('admin/structure/block/add/views_block:test_view_block-block_1/' . $default_theme, array(), t('Save block')); + $this->drupalPostForm('admin/structure/block/add/views_block:test_view_block-block_1/' . $default_theme, $edit, t('Save block')); $block = $storage->load('views_block__test_view_block_block_1_' . $i); // This will only return a result if our new block has been created with the // expected machine name. @@ -210,7 +212,7 @@ public function testViewsBlockForm() { // Tests the override capability of items per page. $this->drupalGet('admin/structure/block/add/views_block:test_view_block-block_1/' . $default_theme); - $edit = array(); + $edit = ['region' => 'content']; $edit['settings[override][items_per_page]'] = 10; $this->drupalPostForm('admin/structure/block/add/views_block:test_view_block-block_1/' . $default_theme, $edit, t('Save block')); @@ -228,7 +230,7 @@ public function testViewsBlockForm() { $this->assertEqual(5, $config['items_per_page'], "'Items per page' is properly saved."); // Tests the override of the label capability. - $edit = array(); + $edit = ['region' => 'content']; $edit['settings[views_label_checkbox]'] = 1; $edit['settings[views_label]'] = 'Custom title'; $this->drupalPostForm('admin/structure/block/add/views_block:test_view_block-block_1/' . $default_theme, $edit, t('Save block')); diff --git a/core/modules/block_content/src/Tests/BlockContentCreationTest.php b/core/modules/block_content/src/Tests/BlockContentCreationTest.php index 6bf250c..bda0bea 100644 --- a/core/modules/block_content/src/Tests/BlockContentCreationTest.php +++ b/core/modules/block_content/src/Tests/BlockContentCreationTest.php @@ -104,6 +104,7 @@ public function testBlockContentCreationMultipleViewModes() { $this->assertFieldByXPath('//select[@name="settings[view_mode]"]', NULL, 'View mode setting shown because multiple exist'); // Change the view mode. + $view_mode['region'] = 'content'; $view_mode['settings[view_mode]'] = 'test_view_mode'; $this->drupalPostForm(NULL, $view_mode, t('Save block')); diff --git a/core/modules/block_content/src/Tests/BlockContentTypeTest.php b/core/modules/block_content/src/Tests/BlockContentTypeTest.php index 4270538..0312798 100644 --- a/core/modules/block_content/src/Tests/BlockContentTypeTest.php +++ b/core/modules/block_content/src/Tests/BlockContentTypeTest.php @@ -206,7 +206,7 @@ public function testsBlockContentAddTypes() { if (!empty($blocks)) { $block = reset($blocks); $this->assertUrl(\Drupal::url('block.admin_add', array('plugin_id' => 'block_content:' . $block->uuid(), 'theme' => $theme), array('absolute' => TRUE))); - $this->drupalPostForm(NULL, array(), t('Save block')); + $this->drupalPostForm(NULL, ['region' => 'content'], t('Save block')); $this->assertUrl(\Drupal::url('block.admin_display_theme', array('theme' => $theme), array('absolute' => TRUE, 'query' => array('block-placement' => Html::getClass($edit['info[0][value]']))))); } else { diff --git a/core/modules/system/src/Tests/System/AccessDeniedTest.php b/core/modules/system/src/Tests/System/AccessDeniedTest.php index 0f37320..810cd87 100644 --- a/core/modules/system/src/Tests/System/AccessDeniedTest.php +++ b/core/modules/system/src/Tests/System/AccessDeniedTest.php @@ -58,7 +58,7 @@ function testAccessDenied() { $this->drupalPostForm('admin/config/system/site-information', $edit, t('Save configuration')); // Enable the user login block. - $this->drupalPlaceBlock('user_login_block', array('id' => 'login')); + $block = $this->drupalPlaceBlock('user_login_block', array('id' => 'login')); // Log out and check that the user login block is shown on custom 403 pages. $this->drupalLogout(); @@ -83,10 +83,7 @@ function testAccessDenied() { // Log back in, set the custom 403 page to /user/login and remove the block $this->drupalLogin($this->adminUser); $this->config('system.site')->set('page.403', '/user/login')->save(); - $edit = [ - 'region' => -1, - ]; - $this->drupalPostForm('admin/structure/block/manage/login', $edit, t('Save block')); + $block->disable()->save(); // Check that we can log in from the 403 page. $this->drupalLogout();