diff --git a/core/lib/Drupal/Core/Block/BlockBase.php b/core/lib/Drupal/Core/Block/BlockBase.php index 1321f3c..745ec49 100644 --- a/core/lib/Drupal/Core/Block/BlockBase.php +++ b/core/lib/Drupal/Core/Block/BlockBase.php @@ -173,7 +173,7 @@ public function buildConfigurationForm(array $form, FormStateInterface $form_sta '#title' => $this->t('Display title'), '#description' => $this->t('How to display the block title.'), '#options' => $this->getLabelDisplayOptions(), - '#default_value' => $this->configuration['label_display'] ? $this->configuration['label_display'] : static::BLOCK_LABEL_VISIBLE, + '#default_value' => $this->configuration['label_display'], ]; // Add context mapping UI form elements. @@ -191,11 +191,11 @@ public function buildConfigurationForm(array $form, FormStateInterface $form_sta * An array of visibility options. */ protected function getLabelDisplayOptions() { - return array( + return [ static::BLOCK_LABEL_VISIBLE => $this->t('Visible'), static::BLOCK_LABEL_HIDDEN => '- ' . $this->t('Hidden') . ' -', static::BLOCK_LABEL_VISUALLY_HIDDEN => '- ' . $this->t('Visually Hidden') . ' -', - ); + ]; } /** diff --git a/core/modules/block/src/Tests/BlockTest.php b/core/modules/block/src/Tests/BlockTest.php index 5910d47..5633032 100644 --- a/core/modules/block/src/Tests/BlockTest.php +++ b/core/modules/block/src/Tests/BlockTest.php @@ -4,6 +4,7 @@ use Drupal\Component\Utility\Html; use Drupal\block\Entity\Block; +use Drupal\Core\Block\BlockPluginInterface; use Drupal\Core\Url; use Drupal\user\Entity\Role; use Drupal\user\RoleInterface; @@ -28,7 +29,7 @@ public function testBlockVisibility() { 'id' => strtolower($this->randomMachineName(8)), 'region' => 'sidebar_first', 'settings[label]' => $title, - 'settings[label_display]' => TRUE, + 'settings[label_display]' => BlockPluginInterface::BLOCK_LABEL_VISIBLE, ]; // Set the block to be hidden on any user path, and to be shown only to // authenticated users. @@ -188,7 +189,7 @@ public function testBlock() { $block = []; $block['id'] = 'system_powered_by_block'; $block['settings[label]'] = $this->randomMachineName(8); - $block['settings[label_display]'] = TRUE; + $block['settings[label_display]'] = BlockPluginInterface::BLOCK_LABEL_VISIBLE; $block['theme'] = $this->config('system.theme')->get('default'); $block['region'] = 'header'; @@ -296,24 +297,39 @@ public function testHideBlockTitle() { 'id' => $id, 'region' => 'sidebar_first', 'settings[label]' => $title, + 'settings[label_display]' => BlockPluginInterface::BLOCK_LABEL_VISIBLE, ]; $this->drupalPostForm('admin/structure/block/add/' . $block_name . '/' . $default_theme, $edit, t('Save block')); $this->assertText('The block configuration has been saved.', 'Block was saved'); $this->drupalGet('user'); - $this->assertNoText($title, 'Block title was not displayed by default.'); + $this->assertText($title, 'Block title was displayed by default.'); + // Hide the title of the block. $edit = [ - 'settings[label_display]' => TRUE, + 'settings[label_display]' => BlockPluginInterface::BLOCK_LABEL_HIDDEN, ]; $this->drupalPostForm('admin/structure/block/manage/' . $id, $edit, t('Save block')); $this->assertText('The block configuration has been saved.', 'Block was saved'); $this->drupalGet('admin/structure/block/manage/' . $id); - $this->assertFieldChecked('edit-settings-label-display', 'The display_block option has the correct default value on the configuration form.'); + $this->assertOptionSelectedWithDrupalSelector('edit-settings-label-display', BlockPluginInterface::BLOCK_LABEL_HIDDEN, 'The label_display option has the correct default value on the configuration form.'); $this->drupalGet('user'); - $this->assertText($title, 'Block title was displayed when enabled.'); + $this->assertNoText($title, 'Block title was not displayed anymore when disabled.'); + + // Visually hide the title of the block. + $edit = [ + 'settings[label_display]' => BlockPluginInterface::BLOCK_LABEL_VISUALLY_HIDDEN, + ]; + $this->drupalPostForm('admin/structure/block/manage/' . $id, $edit, t('Save block')); + $this->assertText('The block configuration has been saved.', 'Block was saved'); + + $this->drupalGet('admin/structure/block/manage/' . $id); + $this->assertOptionSelectedWithDrupalSelector('edit-settings-label-display', BlockPluginInterface::BLOCK_LABEL_VISUALLY_HIDDEN, 'The label_display option has the correct default value on the configuration form.'); + + $this->drupalGet('user'); + $this->assertFieldByXPath('//*[@id="block-' . $id . '"]/h2[contains(@class, "visually-hidden")]', $title, 'Block title was displayed in the source code but with the visually-hidden CSS class when configured be visually hidden.'); } /** diff --git a/core/modules/block/tests/src/Kernel/BlockInterfaceTest.php b/core/modules/block/tests/src/Kernel/BlockInterfaceTest.php index 48d63a3..8cae75d 100644 --- a/core/modules/block/tests/src/Kernel/BlockInterfaceTest.php +++ b/core/modules/block/tests/src/Kernel/BlockInterfaceTest.php @@ -70,10 +70,15 @@ public function testBlockInterface() { '#required' => TRUE, ], 'label_display' => [ - '#type' => 'checkbox', + '#type' => 'select', '#title' => 'Display title', - '#default_value' => TRUE, - '#return_value' => 'visible', + '#description' => 'How to display the block title.', + '#options' => [ + BlockPluginInterface::BLOCK_LABEL_VISIBLE => 'Visible', + BlockPluginInterface::BLOCK_LABEL_HIDDEN => '- Hidden -', + BlockPluginInterface::BLOCK_LABEL_VISUALLY_HIDDEN => '- Visually Hidden -', + ], + '#default_value' => BlockPluginInterface::BLOCK_LABEL_VISIBLE, ], 'context_mapping' => [], 'display_message' => [ diff --git a/core/modules/outside_in/tests/src/FunctionalJavascript/OutsideInBlockFormTest.php b/core/modules/outside_in/tests/src/FunctionalJavascript/OutsideInBlockFormTest.php index 04c6e70..15223db 100644 --- a/core/modules/outside_in/tests/src/FunctionalJavascript/OutsideInBlockFormTest.php +++ b/core/modules/outside_in/tests/src/FunctionalJavascript/OutsideInBlockFormTest.php @@ -184,7 +184,7 @@ protected function assertOffCanvasBlockFormIsValid() { $web_assert = $this->assertSession(); // Check that common block form elements exist. $web_assert->elementExists('css', 'input[data-drupal-selector="edit-settings-label"]'); - $web_assert->elementExists('css', 'input[data-drupal-selector="edit-settings-label-display"]'); + $web_assert->elementExists('css', 'select[data-drupal-selector="edit-settings-label-display"]'); // Check that advanced block form elements do not exist. $web_assert->elementNotExists('css', 'input[data-drupal-selector="edit-visibility-request-path-pages"]'); $web_assert->elementNotExists('css', 'select[data-drupal-selector="edit-region"]');