Commit ba6d0c6d authored by catch's avatar catch
Browse files

Issue #2568889 by smustgrave, berenddeboer, Lendude, Anandhi Karnan, ckaotik,...

Issue #2568889 by smustgrave, berenddeboer, Lendude, Anandhi Karnan, ckaotik, boromino, diaodiallo, Yago Elias, yashingole, Abhijith S, Amber Himes Matz, dawehner, Scott Weston: Views exposed text filter set to required shows an empty error and form error on page load

(cherry picked from commit 7a7d73a1)
parent 81aeb3cb
......@@ -246,6 +246,32 @@ public function exposedFormAlter(&$form, FormStateInterface $form_state) {
}
}
// Get an array of exposed filters and exposed required filters,
// keyed by identifier option.
$exposed_filters = [];
$exposed_required_filters = [];
foreach ($this->view->filter as $id => $handler) {
if ($handler->canExpose() && $handler->isExposed() && !empty($handler->options['expose']['identifier'])) {
if ($handler->options['expose']['required'] && $handler->options['plugin_id'] !== 'boolean') {
$exposed_required_filters[$handler->options['expose']['identifier']] = $id;
}
$exposed_filters[$handler->options['expose']['identifier']] = $id;
}
}
// If any required exposed filters loop through them to see if they have any
// input. If not don't auto process the form.
if (!empty($exposed_required_filters)) {
// Prevent form validation if exposed filter is not set.
$exposed_input = $this->view->getExposedInput();
foreach ($exposed_required_filters as $key => $required_filter) {
if (!$exposed_input || empty($exposed_input[$key])) {
$form_state->setAlwaysProcess(FALSE);
break;
}
}
}
if (!empty($this->options['reset_button'])) {
$form['actions']['reset'] = [
'#value' => $this->options['reset_button_label'],
......@@ -253,13 +279,6 @@ public function exposedFormAlter(&$form, FormStateInterface $form_state) {
'#weight' => 10,
];
// Get an array of exposed filters, keyed by identifier option.
$exposed_filters = [];
foreach ($this->view->filter as $id => $handler) {
if ($handler->canExpose() && $handler->isExposed() && !empty($handler->options['expose']['identifier'])) {
$exposed_filters[$handler->options['expose']['identifier']] = $id;
}
}
$all_exposed = array_merge($exposed_sorts, $exposed_filters);
// Set the access to FALSE if there is no exposed input.
......
langcode: en
status: true
dependencies:
config:
- core.entity_view_mode.node.teaser
- node.type.article
- node.type.page
- node.type.recipe
module:
- node
id: test_exposed_form_buttons_required
label: ''
module: views
description: ''
tag: ''
base_table: node_field_data
base_field: nid
display:
default:
id: default
display_title: Default
display_plugin: default
position: 0
display_options:
pager:
type: full
exposed_form:
type: input_required
options:
submit_button: Apply
reset_button: true
reset_button_label: Reset
exposed_sorts_label: 'Sort by'
expose_sort_order: true
sort_asc_label: Asc
sort_desc_label: Desc
text_input_required: 'Select any filter and click on Apply to see results'
text_input_required_format: basic_html
access:
type: none
cache:
type: tag
filters:
type:
id: type
table: node_field_data
field: type
relationship: none
group_type: group
admin_label: ''
entity_type: node
entity_field: type
plugin_id: in_operator
operator: in
value:
all: all
article: article
page: page
group: 1
exposed: true
expose:
operator_id: type_op
label: 'Content: Type'
description: 'Exposed description'
use_operator: false
operator: ''
operator_limit_selection: false
operator_list: { }
identifier: type
required: true
remember: false
multiple: false
remember_roles:
authenticated: authenticated
anonymous: '0'
administrator: '0'
author: '0'
editor: '0'
reduce: false
is_grouped: false
group_info:
label: ''
description: ''
identifier: ''
optional: true
widget: select
multiple: false
remember: false
default_group: All
default_group_multiple: { }
group_items: { }
style:
type: default
row:
type: 'entity:node'
query:
type: views_query
options:
query_comment: ''
display_extenders: { }
cache_metadata:
max-age: -1
contexts:
- 'languages:language_interface'
- url
- url.query_args
- 'user.node_grants:view'
tags: { }
page_1:
id: page_1
display_title: Page
display_plugin: page
position: 0
display_options:
display_extenders: { }
path: test_exposed_form_buttons_required
cache_metadata:
max-age: -1
contexts:
- 'languages:language_interface'
- url
- url.query_args
- 'user.node_grants:view'
tags: { }
......@@ -8,7 +8,6 @@
use Drupal\Tests\views\Functional\ViewTestBase;
use Drupal\views\ViewExecutable;
use Drupal\views\Views;
use Drupal\views\Entity\View;
/**
* Tests exposed forms functionality.
......@@ -24,7 +23,7 @@ class ExposedFormTest extends ViewTestBase {
*
* @var array
*/
public static $testViews = ['test_exposed_form_buttons', 'test_exposed_block', 'test_exposed_form_sort_items_per_page', 'test_exposed_form_pager'];
public static $testViews = ['test_exposed_form_buttons', 'test_exposed_form_buttons_required', 'test_exposed_block', 'test_exposed_form_sort_items_per_page', 'test_exposed_form_pager'];
/**
* Modules to enable.
......@@ -298,23 +297,27 @@ public function providerTestExposedBlock() {
* Tests the input required exposed form type.
*/
public function testInputRequired() {
$view = View::load('test_exposed_form_buttons');
$display = &$view->getDisplay('default');
$display['display_options']['exposed_form']['type'] = 'input_required';
$view->save();
$this->drupalGet('test_exposed_form_buttons');
$this->drupalGet('test_exposed_form_buttons_required');
$this->assertSession()->statusCodeEquals(200);
$this->helperButtonHasLabel('edit-submit-test-exposed-form-buttons', 'Apply');
$this->helperButtonHasLabel('edit-submit-test-exposed-form-buttons-required', 'Apply');
// Ensure that no results are displayed by default when no input is
// provided.
$this->assertSession()->elementNotExists('xpath', "//div[contains(@class, 'views-row')]");
$this->drupalGet('test_exposed_form_buttons', ['query' => ['type' => 'article']]);
// Ensure that no error element is shown.
$this->assertSession()->elementNotExists('css', '.messages--error');
$this->assertFalse($this->getSession()->getPage()->findField('type')->hasClass('error'));
$edit = [
'type' => 'article',
];
$this->submitForm($edit, 'Apply');
// Ensure that results are displayed by default when input is provided.
$this->assertSession()->elementsCount('xpath', "//div[contains(@class, 'views-row')]", 5);
// Test exposed filter on preview.
$this->drupalGet('admin/structure/views/view/test_view/test_exposed_form_buttons_required');
$this->assertSession()->elementNotExists('css', '.messages--error');
}
/**
......@@ -325,7 +328,6 @@ public function testTextInputRequired() {
$display = &$view->storage->getDisplay('default');
$display['display_options']['exposed_form']['type'] = 'input_required';
// Set up the "on demand text".
// @see https://www.drupal.org/node/535868
$on_demand_text = 'Select any filter and click Apply to see results.';
$display['display_options']['exposed_form']['options']['text_input_required'] = $on_demand_text;
$display['display_options']['exposed_form']['options']['text_input_required_format'] = filter_default_format();
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment