Commit c8414fd5 authored by mpp's avatar mpp Committed by borisson_

Issue #3006725 by mpp: Hide result for boolean fields when label is empty

parent 63a3765d
......@@ -29,12 +29,19 @@ class BooleanItemProcessor extends ProcessorPluginBase implements BuildProcessor
$config = $this->getConfiguration();
/** @var \Drupal\facets\Result\Result $result */
foreach ($results as $result) {
foreach ($results as $key => $result) {
$value = '';
if ($result->getRawValue() == 0) {
$result->setDisplayValue($config['off_value']);
$value = $config['off_value'];
}
elseif ($result->getRawValue() == 1) {
$result->setDisplayValue($config['on_value']);
$value = $config['on_value'];
}
if ($value == '') {
unset($results[$key]);
}
else {
$result->setDisplayValue($value);
}
}
......@@ -51,9 +58,9 @@ class BooleanItemProcessor extends ProcessorPluginBase implements BuildProcessor
'#title' => $this->t('On value'),
'#type' => 'textfield',
'#default_value' => $config['on_value'],
'#description' => $this->t('Use this label instead of <em>0</em> for the <em>On</em> or <em>True</em> value.'),
'#description' => $this->t('Use this label instead of <em>0</em> for the <em>On</em> or <em>True</em> value. Leave empty to hide this item.'),
'#states' => [
'required' => ['input[name="facet_settings[boolean_item][status]"' => ['checked' => TRUE]],
'required' => ['input[name="facet_settings[boolean_item][settings][off_value]"' => ['empty' => TRUE]],
],
];
......@@ -61,9 +68,9 @@ class BooleanItemProcessor extends ProcessorPluginBase implements BuildProcessor
'#title' => $this->t('Off value'),
'#type' => 'textfield',
'#default_value' => $config['off_value'],
'#description' => $this->t('Use this label instead of <em>1</em> for the <em>Off</em> or <em>False</em> value.'),
'#description' => $this->t('Use this label instead of <em>1</em> for the <em>Off</em> or <em>False</em> value. Leave empty to hide this item.'),
'#states' => [
'required' => ['input[name="facet_settings[boolean_item][status]"' => ['checked' => TRUE]],
'required' => ['input[name="facet_settings[boolean_item][settings][on_value]"' => ['empty' => TRUE]],
],
];
......
......@@ -167,6 +167,14 @@ class ProcessorIntegrationTest extends FacetsTestBase {
'category' => 'item_category',
$field_name => TRUE,
])->save();
$entity_test_storage->create([
'name' => 'quux quuux',
'body' => 'test test',
'type' => 'item',
'keywords' => ['apple'],
'category' => 'item_category',
$field_name => FALSE,
])->save();
$this->indexItems($this->indexId);
......@@ -180,6 +188,7 @@ class ProcessorIntegrationTest extends FacetsTestBase {
// Check values.
$this->drupalGet('search-api-test-fulltext');
$this->assertFacetLabel('1');
$this->assertFacetLabel('0');
$form = [
'facet_settings[boolean_item][status]' => TRUE,
......@@ -192,6 +201,7 @@ class ProcessorIntegrationTest extends FacetsTestBase {
$this->drupalGet('search-api-test-fulltext');
$this->assertFacetLabel('Yes');
$this->assertFacetLabel('No');
$form = [
'facet_settings[boolean_item][status]' => TRUE,
......@@ -201,6 +211,19 @@ class ProcessorIntegrationTest extends FacetsTestBase {
$this->drupalGet('search-api-test-fulltext');
$this->assertFacetLabel('Øn');
$this->assertEmpty($this->findFacetLink('1'));
$this->assertEmpty($this->findFacetLink('0'));
$form = [
'facet_settings[boolean_item][status]' => TRUE,
'facet_settings[boolean_item][settings][off_value]' => 'Øff',
];
$this->drupalPostForm($this->editForm, $form, 'Save');
$this->drupalGet('search-api-test-fulltext');
$this->assertFacetLabel('Øff');
$this->assertEmpty($this->findFacetLink('1'));
$this->assertEmpty($this->findFacetLink('0'));
}
/**
......
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