Issue #2720101 by Manuel Garcia, dobe, JacobSanford, camilocodes, Jo Fitzgerald, joelpittet, alexpott, dawehner, Berdir: Label (Title) not set for Views block (exposed filters in Block)

8.7.x
Nathaniel Catchpole 2018-11-20 11:54:11 +00:00
parent c92b0337f9
commit 099d3e65fe
7 changed files with 139 additions and 0 deletions

View File

@ -3,6 +3,7 @@
namespace Drupal\views\Plugin\Block;
use Drupal\Core\Cache\Cache;
use Drupal\Component\Utility\Xss;
/**
* Provides a 'Views Exposed Filter' block.
@ -47,6 +48,14 @@ class ViewsExposedFilterBlock extends ViewsBlockBase {
// contextual links.
$this->addContextualLinks($output, 'exposed_filter');
// Set the blocks title.
if (!empty($this->configuration['label_display']) && ($this->view->getTitle() || !empty($this->configuration['views_label']))) {
$output['#title'] = [
'#markup' => empty($this->configuration['views_label']) ? $this->view->getTitle() : $this->configuration['views_label'],
'#allowed_tags' => Xss::getHtmlTagList(),
];
}
return $output;
}

View File

@ -0,0 +1,22 @@
langcode: en
status: true
dependencies:
config:
- views.view.test_exposed_block
module:
- views
theme:
- bartik
id: exposedformtest_exposed_blockpage_1
theme: bartik
region: content
weight: 0
provider: null
plugin: 'views_exposed_filter_block:test_exposed_block-page_1'
settings:
id: 'views_exposed_filter_block:test_exposed_block-page_1'
label: ''
provider: views
label_display: visible
views_label: ''
visibility: { }

View File

@ -0,0 +1,29 @@
<?php
/**
* @file
* Test fixture.
*/
use Drupal\Core\Database\Database;
use Drupal\Core\Serialization\Yaml;
$connection = Database::getConnection();
// Install the view configuration.
$connection->insert('config')
->fields([
'collection' => '',
'name' => 'views.view.test_exposed_block',
'data' => serialize(Yaml::decode(file_get_contents('core/modules/views/tests/modules/views_test_config/test_views/views.view.test_exposed_block.yml'))),
])
->execute();
// Install the block configuration.
$connection->insert('config')
->fields([
'collection' => '',
'name' => 'block.block.exposedformtest_exposed_blockpage_1',
'data' => serialize(Yaml::decode(file_get_contents('core/modules/views/tests/fixtures/update/block.block.exposedformtest_exposed_blockpage_1.yml'))),
])
->execute();

View File

@ -14,6 +14,7 @@ core: '8'
display:
default:
display_options:
title: 'Test Exposed Block'
access:
type: none
cache:

View File

@ -199,7 +199,31 @@ class ExposedFormTest extends ViewTestBase {
$view = Views::getView('test_exposed_block');
$view->setDisplay('page_1');
$block = $this->drupalPlaceBlock('views_exposed_filter_block:test_exposed_block-page_1');
// Set label to display on the exposed filter form block.
$block->getPlugin()->setConfigurationValue('label_display', TRUE);
$block->save();
// Test that the block label is found.
$this->drupalGet('test_exposed_block');
$this->assertText($view->getTitle(), 'Block title found.');
// Set a custom label on the exposed filter form block.
$block->getPlugin()->setConfigurationValue('views_label', '<strong>Custom</strong> title<script>alert("hacked!");</script>');
$block->save();
// Test that the custom block label is found.
$this->drupalGet('test_exposed_block');
$this->assertRaw('<strong>Custom</strong> titlealert("hacked!");', 'Custom block title found.');
// Set label to hidden on the exposed filter form block.
$block->getPlugin()->setConfigurationValue('label_display', FALSE);
$block->save();
// Test that the label is removed.
$this->drupalGet('test_exposed_block');
$this->assertNoRaw('<strong>Custom</strong> titlealert("hacked!");', 'Custom title was not displayed.');
$this->assertNoText($view->getTitle(), 'Block title was not displayed.');
// Test there is an exposed form in a block.
$xpath = $this->buildXPathQuery('//div[@id=:id]/form/@id', [':id' => Html::getUniqueId('block-' . $block->id())]);

View File

@ -0,0 +1,39 @@
<?php
namespace Drupal\Tests\views\Functional\Update;
use Drupal\block\Entity\Block;
use Drupal\FunctionalTests\Update\UpdatePathTestBase;
/**
* Tests that the additional settings are added to the entity link field.
*
* @see views_post_update_entity_link_url()
*
* @group legacy
*/
class ExposedFilterBlocksUpdateTest extends UpdatePathTestBase {
/**
* {@inheritdoc}
*/
protected function setDatabaseDumpFiles() {
$this->databaseDumpFiles = [
__DIR__ . '/../../../../../system/tests/fixtures/update/drupal-8.bare.standard.php.gz',
__DIR__ . '/../../../fixtures/update/exposed-filter-blocks.php',
];
}
/**
* Tests that exposed filter blocks label display are disabled.
*/
public function testViewsPostUpdateExposedFilterBlocks() {
$this->runUpdates();
// Assert the label display has been disabled after the update.
$block = Block::load('exposedformtest_exposed_blockpage_1');
$config = $block->getPlugin()->getConfiguration();
$this->assertEquals('0', $config['label_display']);
}
}

View File

@ -366,3 +366,18 @@ function views_post_update_table_display_cache_max_age(&$sandbox = NULL) {
return FALSE;
});
}
/**
* Update exposed filter blocks label display to be disabled.
*/
function views_post_update_exposed_filter_blocks_label_display(&$sandbox = NULL) {
\Drupal::classResolver(ConfigEntityUpdater::class)->update($sandbox, 'block', function ($block) {
/** @var \Drupal\block\BlockInterface $block */
if (strpos($block->getPluginId(), 'views_exposed_filter_block:') === 0) {
$block->getPlugin()->setConfigurationValue('label_display', '0');
return TRUE;
}
return FALSE;
});
}