Skip to content
Snippets Groups Projects
Commit e693ef36 authored by catch's avatar catch
Browse files

Issue #2596649 by legolasbo, rakesh.gectcr: Exposed form does not save state...

Issue #2596649 by legolasbo, rakesh.gectcr: Exposed form does not save state when it is placed in a block
parent 9b091977
No related branches found
No related tags found
No related merge requests found
......@@ -6,6 +6,7 @@
*/
namespace Drupal\views\Plugin\Block;
use Drupal\Core\Cache\Cache;
/**
* Provides a 'Views Exposed Filter' block.
......@@ -18,11 +19,20 @@
*/
class ViewsExposedFilterBlock extends ViewsBlockBase {
/**
* {@inheritdoc}
*/
public function getCacheContexts() {
$contexts = $this->view->display_handler->getCacheMetadata()->getCacheContexts();
return Cache::mergeContexts(parent::getCacheContexts(), $contexts);
}
/**
* {@inheritdoc}
*/
public function build() {
$output = $this->view->display_handler->viewExposedFormBlocks();
// Before returning the block output, convert it to a renderable array with
// contextual links.
$this->addContextualLinks($output, 'exposed_filter');
......
......@@ -359,6 +359,14 @@ public function getCacheContexts() {
}
}
// Merge in cache contexts for all exposed filters to prevent display of
// cached forms.
foreach ($this->displayHandler->getHandlers('filter') as $filter_hander) {
if ($filter_hander->isExposed()) {
$contexts = Cache::mergeContexts($contexts, $filter_hander->getCacheContexts());
}
}
return $contexts;
}
......
......@@ -151,6 +151,7 @@ public function testExposedFormRender() {
* Tests the exposed block functionality.
*/
public function testExposedBlock() {
$this->drupalCreateContentType(['type' => 'page']);
$view = Views::getView('test_exposed_block');
$view->setDisplay('page_1');
$block = $this->drupalPlaceBlock('views_exposed_filter_block:test_exposed_block-page_1');
......@@ -167,6 +168,15 @@ public function testExposedBlock() {
// Test there is only one views exposed form on the page.
$elements = $this->xpath('//form[@id=:id]', array(':id' => $this->getExpectedExposedFormId($view)));
$this->assertEqual(count($elements), 1, 'One exposed form block found.');
// Test that the correct option is selected after form submission.
$this->assertCacheContext('url');
$this->assertOptionSelected('edit-type','All');
foreach (['All', 'article', 'page'] as $argument) {
$this->drupalGet('test_exposed_block', ['query' => ['type' => $argument]]);
$this->assertCacheContext('url');
$this->assertOptionSelected('edit-type', $argument);
}
}
/**
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment