Commit bf7f3bec authored by borisson_'s avatar borisson_ Committed by borisson_
Browse files

Issue #2625188 by borisson_, Leksat, StryKaizer: Allow limiting to one active item

parent 35aaf32b
......@@ -57,6 +57,9 @@ facets.facet.*:
only_visible_when_facet_source_is_visible:
type: boolean
label: 'Show this facet only when the facet source is visible.'
show_only_one_result:
type: boolean
label: 'Show only one result'
processor_configs:
type: sequence
label: 'Processor settings'
......
......@@ -38,6 +38,7 @@ use Drupal\facets\FacetInterface;
* "id",
* "name",
* "url_alias",
* "show_only_one_result",
* "field_identifier",
* "query_type_name",
* "facet_source_id",
......@@ -222,6 +223,13 @@ class Facet extends ConfigEntityBase implements FacetInterface {
*/
protected $only_visible_when_facet_source_is_visible;
/**
* Determines if only one result can be selected in the facet at one time.
*
* @var bool
*/
protected $show_only_one_result = FALSE;
/**
* The no-result configuration.
*
......@@ -476,6 +484,20 @@ class Facet extends ConfigEntityBase implements FacetInterface {
return $this->facet_source_instance;
}
/**
* {@inheritdoc}
*/
public function getShowOnlyOneResult() {
return $this->show_only_one_result;
}
/**
* {@inheritdoc}
*/
public function setShowOnlyOneResult($show_only_one_result) {
$this->show_only_one_result = $show_only_one_result;
}
/**
* {@inheritdoc}
*/
......
......@@ -112,6 +112,22 @@ interface FacetInterface extends ConfigEntityInterface {
*/
public function isActiveValue($value);
/**
* Returns the show_only_one_result option.
*
* @return bool
* Show only one result.
*/
public function getShowOnlyOneResult();
/**
* Sets the show_only_one_result option.
*
* @param bool $show_only_one_result
* Show only one result.
*/
public function setShowOnlyOneResult($show_only_one_result);
/**
* Returns the result for the facet.
*
......
......@@ -326,6 +326,13 @@ class FacetDisplayForm extends EntityForm {
'#default_value' => $facet->getOnlyVisibleWhenFacetSourceIsVisible(),
];
$form['facet_settings']['show_only_one_result'] = [
'#type' => 'checkbox',
'#title' => $this->t('Make sure only one result can be shown.'),
'#description' => $this->t('When checked, this will make sure that only one result can be selected for this facet at one time.'),
'#default_value' => $facet->getShowOnlyOneResult(),
];
$empty_behavior_config = $facet->getEmptyBehavior();
$form['facet_settings']['empty_behavior'] = [
'#type' => 'radios',
......@@ -522,6 +529,7 @@ class FacetDisplayForm extends EntityForm {
$facet->setWidget($form_state->getValue('widget'));
$facet->setWidgetConfigs($form_state->getValue('widget_configs'));
$facet->setOnlyVisibleWhenFacetSourceIsVisible($form_state->getValue(['facet_settings', 'only_visible_when_facet_source_is_visible']));
$facet->setShowOnlyOneResult($form_state->getValue(['facet_settings', 'show_only_one_result']));
$empty_behavior_config = [];
$empty_behavior = $form_state->getValue(['facet_settings', 'empty_behavior']);
......
......@@ -72,7 +72,7 @@ class QueryString extends UrlProcessorPluginBase {
}
$url = Url::createFromRequest($request);
/** @var \Drupal\facets\Result\ResultInterface $result */
/** @var \Drupal\facets\Result\ResultInterface[] $results */
foreach ($results as &$result) {
$filter_string = $this->urlAlias . self::SEPARATOR . $result->getRawValue();
$result_get_params = clone $get_params;
......@@ -89,6 +89,20 @@ class QueryString extends UrlProcessorPluginBase {
// If the value is not active, add the filter string.
else {
$filter_params[] = $filter_string;
// Exclude currently active results from the filter params if we are in
// the show_only_one_result mode.
if ($facet->getShowOnlyOneResult()) {
foreach ($results as $result2) {
if ($result2->isActive()) {
$active_filter_string = $this->urlAlias . self::SEPARATOR . $result2->getRawValue();
foreach ($filter_params as $key2 => $filter_param2) {
if ($filter_param2 == $active_filter_string) {
unset($filter_params[$key2]);
}
}
}
}
}
}
$result_get_params->set($this->filterKey, $filter_params);
......
......@@ -487,6 +487,37 @@ class IntegrationTest extends FacetWebTestBase {
$this->assertNoText('foo baz');
}
/**
* Tests allow only one active item.
*/
public function testAllowOneActiveItem() {
$facet_name = 'Spotted wood owl';
$facet_id = 'spotted_wood_owl';
$facet_edit_page = 'admin/config/search/facets/' . $facet_id;
$this->addFacet($facet_name, 'keywords');
$this->createFacetBlock($facet_id);
$this->drupalGet($facet_edit_page . '/display');
$edit = ['facet_settings[show_only_one_result]' => TRUE];
$this->drupalPostForm(NULL, $edit, $this->t('Save'));
$this->drupalGet('search-api-test-fulltext');
$this->assertText('Displaying 5 search results');
$this->assertLink('grape');
$this->assertLink('orange');
$this->clickLink('grape');
$this->assertText('Displaying 3 search results');
$this->assertLink('(-) grape');
$this->assertLink('orange');
$this->clickLink('orange');
$this->assertText('Displaying 3 search results');
$this->assertLink('grape');
$this->assertLink('(-) orange');
}
/**
* Deletes a facet block by id.
*
......
Markdown is supported
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