Skip to content
Snippets Groups Projects
Commit c84be094 authored by Andras Szilagyi's avatar Andras Szilagyi
Browse files

Issue #3336826 by Andras_Szilagyi, escuriola, vengador: Dropdown widget -...

Issue #3336826 by Andras_Szilagyi, escuriola, vengador: Dropdown widget - honour the "Empty facet behavior"
parent 3bfc247f
No related branches found
No related tags found
1 merge request!37Issue #3336826: Dropdown widget - honour the "Empty facet behavior"
...@@ -100,7 +100,7 @@ class DropdownWidget extends ArrayWidget implements FacetsFormWidgetInterface, C ...@@ -100,7 +100,7 @@ class DropdownWidget extends ArrayWidget implements FacetsFormWidgetInterface, C
], ],
'disabled_on_empty' => [ 'disabled_on_empty' => [
'#type' => 'checkbox', '#type' => 'checkbox',
'#title' => $this->t('Disable when there are no results'), '#title' => $this->t('Disable when there are no results. Enabling this will return the widget along with whatever is set in the "Empty facet behavior".'),
'#default_value' => $this->getConfiguration()['disabled_on_empty'], '#default_value' => $this->getConfiguration()['disabled_on_empty'],
], ],
] + parent::buildConfigurationForm($form, $form_state, $facet); ] + parent::buildConfigurationForm($form, $form_state, $facet);
...@@ -111,9 +111,15 @@ class DropdownWidget extends ArrayWidget implements FacetsFormWidgetInterface, C ...@@ -111,9 +111,15 @@ class DropdownWidget extends ArrayWidget implements FacetsFormWidgetInterface, C
*/ */
public function build(FacetInterface $facet) { public function build(FacetInterface $facet) {
$items = parent::build($facet)[$facet->getFieldIdentifier()] ?? []; $items = parent::build($facet)[$facet->getFieldIdentifier()] ?? [];
$this->processItems($items, $facet); $this->processItems($items, $facet);
// Honour the "Empty facet behavior", the widget's build still gets printed
// in the page even if the empty behaviour kicks in, so we need to empty the
// build if there are no results.
if (empty($items) && !$this->getConfiguration()['disabled_on_empty']) {
return [];
}
$options = $ancestors = []; $options = $ancestors = [];
if ($facet->getShowOnlyOneResult()) { if ($facet->getShowOnlyOneResult()) {
$options[NULL] = $this->getConfiguration()['default_option_label']; $options[NULL] = $this->getConfiguration()['default_option_label'];
......
...@@ -189,4 +189,67 @@ class IntegrationTest extends BrowserTestBase { ...@@ -189,4 +189,67 @@ class IntegrationTest extends BrowserTestBase {
); );
} }
/**
* Test facets form as dependent facet.
*/
public function testFacetsFormWithDependencies(): void {
$assert = $this->assertSession();
$page = $this->getSession()->getPage();
$facet_name = 'Emu';
$facet_id = 'emu';
$this->createFacet($facet_name, $facet_id);
$facet = Facet::load($facet_id);
$facet->setWidget('facets_form_dropdown');
$facet->save();
$depending_name = 'Llama';
$depending_id = 'llama';
$this->createFacet($depending_name, $depending_id, 'category');
$depending_facet = Facet::load($depending_id);
$processor = [
'processor_id' => 'dependent_processor',
'weights' => ['build' => 5],
'settings' => [
$facet_id => [
'enable' => TRUE,
'condition' => 'values',
'values' => 'item',
'negate' => FALSE,
],
],
];
$depending_facet->addProcessor($processor);
$depending_facet->setWidget('facets_form_dropdown');
$depending_facet->save();
// Place the Facets Form block.
$settings['button']['label']['submit'] = 'Refine';
$this->drupalPlaceBlock('facets_form:search_api:views_page__search_api_test_view__page_1', $settings);
$this->drupalGet('search-api-test-fulltext');
$assert->elementsCount('css', '.views-row', 5);
$form = $assert->elementExists('css', 'form#facets-form');
// Test that the dependent facet is not shown.
$assert->elementExists('css', 'select#edit-emu--2', $form);
$assert->elementNotExists('css', 'select#edit-llama--2', $form);
// Test that dependent facet exists when correct value is set.
$page->selectFieldOption('emu[]', 'item');
$assert->buttonExists('Refine', $form)->press();
$this->assertCurrentUrl('search-api-test-fulltext', ['f' => ['emu:item']]);
$assert->elementsCount('css', '.views-row', 3);
$assert->elementExists('css', 'select#edit-emu--2', $form);
$assert->elementExists('css', 'select#edit-llama--2', $form);
// Test that dependent facet doesn't exist when other value is set.
$page->selectFieldOption('emu[]', 'article');
$assert->buttonExists('Refine', $form)->press();
$this->assertCurrentUrl('search-api-test-fulltext', ['f' => ['emu:article']]);
$assert->elementsCount('css', '.views-row', 2);
$assert->elementExists('css', 'select#edit-emu--2', $form);
$assert->elementNotExists('css', 'select#edit-llama--2', $form);
}
} }
...@@ -25,6 +25,32 @@ class DropdownWidgetTest extends KernelTestBase { ...@@ -25,6 +25,32 @@ class DropdownWidgetTest extends KernelTestBase {
'facets_form_test', 'facets_form_test',
]; ];
/**
* Test the widget with empty items and different config.
*/
public function testWithEmptyItems(): void {
// Empty results should return an empty build.
$facet = new Facet(['id' => 'foo'], 'facets_facet');
$facet->setFacetSourceId('facets_form_test');
$facet->setWidget('facets_form_dropdown');
$facet->setResults($this->getResults($facet, []));
$build = $facet->getWidgetInstance()->build($facet);
$this->assertEmpty($build);
// Exception the disabled_on_empty behaviour, we still want the build but
// disabled, to indicate to the user that there are no results.
$facet->setWidget('facets_form_dropdown', ['disabled_on_empty' => TRUE]);
$build = $facet->getWidgetInstance()->build($facet)['foo'];
$this->assertSame('select', $build['#type']);
$this->assertSame('invisible', $build['#title_display']);
$this->assertEmpty($build['#default_value']);
$this->assertTrue($build['#multiple']);
$this->assertTrue($build['#disabled']);
$this->assertEmpty($build['#options']);
}
/** /**
* Test the Facets Form dropdown widget build. * Test the Facets Form dropdown widget build.
* *
...@@ -187,19 +213,6 @@ class DropdownWidgetTest extends KernelTestBase { ...@@ -187,19 +213,6 @@ class DropdownWidgetTest extends KernelTestBase {
'1.1.1' => '   One.One.One', '1.1.1' => '   One.One.One',
], ],
], ],
'empty_items' => [
[],
[
'disabled_on_empty' => TRUE,
],
[],
[],
'invisible',
[],
TRUE,
TRUE,
[],
],
'with_show_number' => [ 'with_show_number' => [
[], [],
[ [
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment