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 5f3b776..0cf8ea1 100644 --- a/core/modules/block/lib/Drupal/block/Tests/Views/DisplayBlockTest.php +++ b/core/modules/block/lib/Drupal/block/Tests/Views/DisplayBlockTest.php @@ -145,7 +145,7 @@ public function testViewsBlockForm() { $default_theme = config('system.theme')->get('default'); $this->drupalGet('admin/structure/block/add/views_block:test_view_block-block_1/' . $default_theme); $elements = $this->xpath('//input[@name="label"]'); - $this->assertTrue(empty($elements), 'The label field is not found for Views blocks.'); + // @todo Add test coverage. } } diff --git a/core/modules/block/tests/block_test_views/test_views/views.view.test_view_block.yml b/core/modules/block/tests/block_test_views/test_views/views.view.test_view_block.yml index 1ae6e85..839bd46 100644 --- a/core/modules/block/tests/block_test_views/test_views/views.view.test_view_block.yml +++ b/core/modules/block/tests/block_test_views/test_views/views.view.test_view_block.yml @@ -31,7 +31,7 @@ display: id: name table: views_test_data field: name - title: test_view_block + title: 'test_view_block title' block_1: display_plugin: block id: block_1 diff --git a/core/modules/block/tests/block_test_views/test_views/views.view.test_view_block2.yml b/core/modules/block/tests/block_test_views/test_views/views.view.test_view_block2.yml index c0a9a12..2faf11e 100644 --- a/core/modules/block/tests/block_test_views/test_views/views.view.test_view_block2.yml +++ b/core/modules/block/tests/block_test_views/test_views/views.view.test_view_block2.yml @@ -31,7 +31,7 @@ display: id: name table: views_test_data field: name - title: test_view_block2 + title: 'test_view_block2 title' block_1: display_plugin: block id: block_1 diff --git a/core/modules/views/lib/Drupal/views/Plugin/Derivative/ViewsBlock.php b/core/modules/views/lib/Drupal/views/Plugin/Derivative/ViewsBlock.php index 7d4d161..61aef19 100644 --- a/core/modules/views/lib/Drupal/views/Plugin/Derivative/ViewsBlock.php +++ b/core/modules/views/lib/Drupal/views/Plugin/Derivative/ViewsBlock.php @@ -53,12 +53,7 @@ public function getDerivativeDefinitions(array $base_plugin_definition) { $desc = $display->getOption('block_description'); if (empty($desc)) { - if ($display->display['display_title'] == $display->definition['title']) { - $desc = t('View: !view', array('!view' => $view->getHumanName())); - } - else { - $desc = t('View: !view: !display', array('!view' => $view->getHumanName(), '!display' => $display->display['display_title'])); - } + $desc = $view->getHumanName(); } $this->derivatives[$delta] = array( 'admin_label' => $desc, diff --git a/core/modules/views/lib/Drupal/views/Plugin/block/block/ViewsBlock.php b/core/modules/views/lib/Drupal/views/Plugin/block/block/ViewsBlock.php index 8a77f18..382e3cb 100644 --- a/core/modules/views/lib/Drupal/views/Plugin/block/block/ViewsBlock.php +++ b/core/modules/views/lib/Drupal/views/Plugin/block/block/ViewsBlock.php @@ -38,6 +38,16 @@ class ViewsBlock extends BlockBase { */ protected $displayID; + + /** + * Overrides \Drupal\block\BlockBase::settings(). + */ + public function settings() { + return array( + 'use_view_title' => 1, + ); + } + /** * Overrides \Drupal\Component\Plugin\PluginBase::__construct(). */ @@ -63,20 +73,55 @@ public function blockAccess() { */ public function form($form, &$form_state) { $form = parent::form($form, $form_state); + $entity = $form_state['entity']; + $definition = $this->getDefinition(); + $label = $definition['admin_label']; + $view_title = $this->view->getTitle(); + + if (!empty($view_title)) { + // Allow the user to use the views title or override it. + $form['use_view_title'] = array( + '#type' => 'select', + '#title' => t('Title'), + '#weight' => (!empty($form['label']['#weight']) ? empty($form['label']['#weight']) - 2 : -2), + '#options' => array( + 1 => $view_title, + 0 => t('Override title...'), + ), + '#default_value' => $this->configuration['use_view_title'], + ); + $form['label']['#title_display'] = 'invisible'; + $form['label']['#states'] = array( + 'visible' => array( + ':input[name="use_view_title"]' => array('value' => 0), + ) + ); + + // Use the view title as the default label if it is available. + $label = $view_title; + } - // Set the default label to '' so the views internal title is used. - $form['label']['#default_value'] = ''; - $form['label']['#access'] = FALSE; + $form['label']['#default_value'] = !$entity->isNew() ? $entity->label() : $label; return $form; } /** + * Overrides \Drupal\block\BlockBase::blockSubmit(). + */ + public function blockSubmit($form, &$form_state) { + $this->configuration['use_view_title'] = $form_state['values']['use_view_title']; + } + + /** * Implements \Drupal\block\BlockBase::build(). */ public function build() { $output = $this->view->executeDisplay($this->displayID); - // Set the label to the title configured in the view. - $this->entity->set('label', filter_xss_admin($this->view->getTitle())); + // Use the view title unless the user overrode it. + if (($this->configuration['use_view_title'] == 1)) { + $this->entity->set('label', filter_xss_admin($this->view->getTitle())); + } + // Before returning the block output, convert it to a renderable array // with contextual links. $this->addContextualLinks($output);