diff --git a/core/modules/block/block.module b/core/modules/block/block.module index ff804b2..acfe1a8 100644 --- a/core/modules/block/block.module +++ b/core/modules/block/block.module @@ -535,6 +535,11 @@ function template_preprocess_block(&$variables) { $block_counter = &drupal_static(__FUNCTION__, array()); $variables['block'] = (object) $variables['elements']['#block_config']; + // If the block title is configured to be hidden, set it to an empty string. + if ($variables['elements']['#block']->title_display == 'hidden') { + $variables['block']->subject_hidden = $variables['block']->subject; + $variables['block']->subject = ''; + } // All blocks get an independent counter for each region. if (!isset($block_counter[$variables['block']->region])) { diff --git a/core/modules/block/lib/Drupal/block/BlockBase.php b/core/modules/block/lib/Drupal/block/BlockBase.php index 9e5bf6c..384fd7f 100644 --- a/core/modules/block/lib/Drupal/block/BlockBase.php +++ b/core/modules/block/lib/Drupal/block/BlockBase.php @@ -239,6 +239,15 @@ public function form($form, &$form_state) { '#title' => t('Title'), '#maxlength' => 255, '#default_value' => !$entity->isNew() ? $entity->label() : $definition['subject'], + '#required' => TRUE, + ); + $form['title_display'] = array( + '#type' => 'checkbox', + '#title' => t('Display title'), + '#default_value' => $entity->title_display == 'hidden' ? FALSE : 'visible', + // Make it possible for contributed modules to make more title visibility + // settings available. + '#return_value' => 'visible', ); $form['machine_name'] = array( '#type' => 'machine_name', @@ -464,6 +473,11 @@ public function blockValidate($form, &$form_state) {} */ public function submit($form, &$form_state) { if (!form_get_errors()) { + // Default title display value to hidden if not checked. + if (empty($form_state['values']['title_display'])) { + $form_state['values']['title_display'] = 'hidden'; + } + $this->blockSubmit($form, $form_state); drupal_set_message(t('The block configuration has been saved.')); diff --git a/core/modules/block/lib/Drupal/block/Plugin/Core/Entity/Block.php b/core/modules/block/lib/Drupal/block/Plugin/Core/Entity/Block.php index d2bf1a7..39d1751 100644 --- a/core/modules/block/lib/Drupal/block/Plugin/Core/Entity/Block.php +++ b/core/modules/block/lib/Drupal/block/Plugin/Core/Entity/Block.php @@ -53,6 +53,16 @@ class Block extends ConfigEntityBase { public $label; /** + * Whether the label should be displayed. + * + * Drupal core hides the title altogether if the value is + * 'hidden'. Otherwise the title is displayed. + * + * @var string + */ + public $title_display = 'visible'; + + /** * The block UUID. * * @var string @@ -169,6 +179,7 @@ public function getExportProperties() { $names = array( 'id', 'label', + 'title_display', 'uuid', 'region', 'weight', diff --git a/core/modules/block/lib/Drupal/block/Tests/BlockStorageUnitTest.php b/core/modules/block/lib/Drupal/block/Tests/BlockStorageUnitTest.php index d0c833a..ade0959 100644 --- a/core/modules/block/lib/Drupal/block/Tests/BlockStorageUnitTest.php +++ b/core/modules/block/lib/Drupal/block/Tests/BlockStorageUnitTest.php @@ -93,6 +93,7 @@ protected function createTests() { $expected_properties = array( 'id' => 'stark.test_block', 'label' => '', + 'title_display' => 'visible', 'region' => '-1', 'weight' => '', 'module' => 'block_test', diff --git a/core/modules/block/lib/Drupal/block/Tests/BlockTest.php b/core/modules/block/lib/Drupal/block/Tests/BlockTest.php index 38a0d29..6ae8648 100644 --- a/core/modules/block/lib/Drupal/block/Tests/BlockTest.php +++ b/core/modules/block/lib/Drupal/block/Tests/BlockTest.php @@ -307,6 +307,37 @@ function testBlock() { } /** + * Test block title display settings. + */ + function testHideBlockTitle() { + $block_name = 'system_powered_by_block'; + // Create a random title for the block. + $title = $this->randomName(8); + $machine_name = strtolower($this->randomName(8)); + // Enable a standard block. + $default_theme = variable_get('theme_default', 'stark'); + $edit = array( + 'machine_name' => $machine_name, + 'region' => 'sidebar_first', + 'label' => $title, + ); + $this->drupalPost('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->assertText($title, 'Block title was displayed by default.'); + + $edit = array( + 'title_display' => FALSE, + ); + $this->drupalPost('admin/structure/block/manage/' . $default_theme . '.' . $machine_name . '/configure', $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 when hidden.'); + } + + /** * Moves a block to a given region via the UI and confirms the result. * * @param array $block diff --git a/core/modules/block/templates/block.tpl.php b/core/modules/block/templates/block.tpl.php index 8c42131..ed5c35d 100644 --- a/core/modules/block/templates/block.tpl.php +++ b/core/modules/block/templates/block.tpl.php @@ -6,6 +6,8 @@ * * Available variables: * - $block->subject: Block title. + * - $block->subject_hidden: The hidden block title value if the block was + * configured to hide the title ($block->subject is empty in this case). * - $content: Block content. * - $block->module: Module that generated the block. * - $block->delta: An ID for the block, unique within each module.