diff --git a/core/modules/block/lib/Drupal/block/Annotation/Block.php b/core/modules/block/lib/Drupal/block/Annotation/Block.php index 62541ab..64987e6 100644 --- a/core/modules/block/lib/Drupal/block/Annotation/Block.php +++ b/core/modules/block/lib/Drupal/block/Annotation/Block.php @@ -30,6 +30,6 @@ class Block extends Plugin { * * @ingroup plugin_translatable */ - public $admin_label; + public $admin_label = ''; } diff --git a/core/modules/block/lib/Drupal/block/Entity/Block.php b/core/modules/block/lib/Drupal/block/Entity/Block.php index 44fa169..0298f44 100644 --- a/core/modules/block/lib/Drupal/block/Entity/Block.php +++ b/core/modules/block/lib/Drupal/block/Entity/Block.php @@ -121,7 +121,8 @@ public function getPlugin() { */ public function label($langcode = NULL) { $settings = $this->get('settings'); - return $settings['label']; + $definition = $this->getPlugin()->getPluginDefinition(); + return $settings['label'] ?: $definition['admin_label']; } /** diff --git a/core/modules/block/lib/Drupal/block/Tests/Views/DisplayBlockTest.php b/core/modules/block/lib/Drupal/block/Tests/Views/DisplayBlockTest.php index eda9285..97d0c54 100644 --- a/core/modules/block/lib/Drupal/block/Tests/Views/DisplayBlockTest.php +++ b/core/modules/block/lib/Drupal/block/Tests/Views/DisplayBlockTest.php @@ -221,6 +221,16 @@ public function testViewsBlockForm() { $config = $block->getPlugin()->getConfiguration(); $this->assertEqual(5, $config['items_per_page'], "'Items per page' is properly saved."); + + // Tests the override of the label capability. + $edit = array(); + $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')); + + $block = $storage->load('views_block__test_view_block_block_1_5'); + $config = $block->getPlugin()->getConfiguration(); + $this->assertEqual('Custom title', $config['views_label'], "'Label' is properly saved."); } /** diff --git a/core/modules/views/lib/Drupal/views/Plugin/Block/ViewsBlock.php b/core/modules/views/lib/Drupal/views/Plugin/Block/ViewsBlock.php index 3f3fe30..1df1768 100644 --- a/core/modules/views/lib/Drupal/views/Plugin/Block/ViewsBlock.php +++ b/core/modules/views/lib/Drupal/views/Plugin/Block/ViewsBlock.php @@ -46,7 +46,7 @@ public function build() { * {@inheritdoc} */ public function defaultConfiguration() { - $settings = array(); + $settings = parent::defaultConfiguration(); if ($this->displaySet) { return $this->view->display_handler->blockSettings($settings); @@ -79,6 +79,7 @@ public function blockValidate($form, &$form_state) { * {@inheritdoc} */ public function blockSubmit($form, &$form_state) { + parent::blockSubmit($form, $form_state); if ($this->displaySet) { $this->view->display_handler->blockSubmit($this, $form, $form_state); } diff --git a/core/modules/views/lib/Drupal/views/Plugin/Block/ViewsBlockBase.php b/core/modules/views/lib/Drupal/views/Plugin/Block/ViewsBlockBase.php index e20f146..106da28 100644 --- a/core/modules/views/lib/Drupal/views/Plugin/Block/ViewsBlockBase.php +++ b/core/modules/views/lib/Drupal/views/Plugin/Block/ViewsBlockBase.php @@ -87,6 +87,16 @@ public function access(AccountInterface $account) { /** * {@inheritdoc} */ + public function defaultConfiguration() { + $settings = array(); + $settings['views_label'] = ''; + + return $settings; + } + + /** + * {@inheritdoc} + */ public function buildConfigurationForm(array $form, array &$form_state) { $form = parent::buildConfigurationForm($form, $form_state); @@ -94,10 +104,75 @@ public function buildConfigurationForm(array $form, array &$form_state) { $form['label']['#default_value'] = ''; $form['label']['#access'] = FALSE; + // Unset the machine_name provided by BlockFormController. + unset($form['id']['#machine_name']['source']); + // Prevent users from changing the auto-generated block machine_name. + $form['id']['#access'] = FALSE; + $form['#pre_render'][] = '\Drupal\views\Plugin\views\PluginBase::preRenderAddFieldsetMarkup'; + + // Allow to override the label on the actual page. + $form['views_label_checkbox'] = array( + '#type' => 'checkbox', + '#title' => $this->t('Override title'), + '#default_value' => !empty($this->configuration['views_label']), + ); + + $form['views_label_fieldset'] = array( + '#type' => 'fieldset', + '#states' => array( + 'visible' => array( + array( + ':input[name="settings[views_label_checkbox]"]' => array('checked' => TRUE), + ), + ), + ), + ); + + $form['views_label_warning'] = array( + '#markup' => $this->t('
In most cases you better change the title directly on the view. Overriding the title here will remove dynamic title support from arguments for example.
'), + '#type' => 'item', + '#states' => array( + 'visible' => array( + array( + ':input[name="settings[views_label_checkbox]"]' => array('checked' => TRUE), + ), + ), + ), + '#fieldset' => 'views_label_fieldset', + ); + + $form['views_label'] = array( + '#title' => $this->t('Title'), + '#type' => 'textfield', + '#default_value' => $this->configuration['views_label'] ?: $this->view->getTitle(), + '#states' => array( + 'visible' => array( + array( + ':input[name="settings[views_label_checkbox]"]' => array('checked' => TRUE), + ), + ), + ), + '#fieldset' => 'views_label_fieldset', + ); + + + return $form; } /** + * {@inheritdoc} + */ + public function blockSubmit($form, &$form_state) { + if (!empty($form_state['values']['views_label_checkbox'])) { + $this->configuration['views_label'] = $form_state['values']['views_label']; + } + else { + $this->configuration['views_label'] = ''; + } + } + + /** * Converts Views block content to a renderable array with contextual links. * * @param string|array $output