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

Issue #2764659 by dragos-dumi, borisson_, ifrik: No labels are displayed for boolean fields

parent d77805a6
...@@ -68,6 +68,17 @@ plugin.plugin_configuration.facets_processor.count_limit: ...@@ -68,6 +68,17 @@ plugin.plugin_configuration.facets_processor.count_limit:
type: integer type: integer
label: 'Maximum amount of items to show.' label: 'Maximum amount of items to show.'
plugin.plugin_configuration.facets_processor.boolean_item:
type: mapping
label: 'Boolean processor'
mapping:
on_value:
type: string
label: 'On value'
off_value:
type: string
label: 'Off value'
plugin.plugin_configuration.facets_processor.url_processor_handler: plugin.plugin_configuration.facets_processor.url_processor_handler:
type: config_object type: config_object
......
<?php
namespace Drupal\facets\Plugin\facets\processor;
use Drupal\facets\FacetInterface;
use Drupal\Core\Form\FormStateInterface;
use Drupal\facets\Processor\BuildProcessorInterface;
use Drupal\facets\Processor\ProcessorPluginBase;
/**
* Provides a processor for boolean labels.
*
* @FacetsProcessor(
* id = "boolean_item",
* label = @Translation("Boolean item label"),
* description = @Translation("Fields that are boolean can use this processor to show the configured On/Off values instead of values 0,1"),
* stages = {
* "build" = 35
* }
* )
*/
class BooleanItemProcessor extends ProcessorPluginBase implements BuildProcessorInterface {
/**
* {@inheritdoc}
*/
public function build(FacetInterface $facet, array $results) {
$config = $this->getConfiguration();
/** @var \Drupal\facets\Result\Result $result */
foreach ($results as $id => $result) {
if ($result->getRawValue() == 0) {
$result->setDisplayValue($config['off_value']);
}
else if ($result->getRawValue() == 1) {
$result->setDisplayValue($config['on_value']);
}
}
return $results;
}
/**
* {@inheritdoc}
*/
public function buildConfigurationForm(array $form, FormStateInterface $form_state, FacetInterface $facet) {
$processors = $facet->getProcessors();
$config = isset($processors[$this->getPluginId()]) ? $processors[$this->getPluginId()] : NULL;
$build['on_value'] = [
'#title' => $this->t('On value'),
'#type' => 'textfield',
'#default_value' => !is_null($config) ? $config->getConfiguration()['on_value'] : $this->defaultConfiguration()['on_value'],
'#description' => $this->t('Set the display value for "On" facet result.'),
'#states' => [
'required' => ['input[name="facet_settings[boolean_item][status]"' => ['checked' => TRUE]],
],
];
$build['off_value'] = [
'#title' => $this->t('Off value'),
'#type' => 'textfield',
'#default_value' => !is_null($config) ? $config->getConfiguration()['off_value'] : $this->defaultConfiguration()['off_value'],
'#description' => $this->t('Set the display value for "Off" facet result.'),
'#states' => [
'required' => ['input[name="facet_settings[boolean_item][status]"' => ['checked' => TRUE]],
],
];
return $build;
}
/**
* {@inheritdoc}
*/
public function validateConfigurationForm(array $form, FormStateInterface $form_state, FacetInterface $facet) {}
/**
* {@inheritdoc}
*/
public function defaultConfiguration() {
return [
'on_value' => 'On',
'off_value' => 'Off',
];
}
}
...@@ -2,6 +2,9 @@ ...@@ -2,6 +2,9 @@
namespace Drupal\Tests\facets\Functional; namespace Drupal\Tests\facets\Functional;
use Drupal\field\Entity\FieldConfig;
use Drupal\field\Entity\FieldStorageConfig;
use Drupal\search_api\Item\Field;
use Symfony\Component\DependencyInjection\ContainerInterface; use Symfony\Component\DependencyInjection\ContainerInterface;
/** /**
...@@ -134,6 +137,82 @@ class ProcessorIntegrationTest extends FacetsTestBase { ...@@ -134,6 +137,82 @@ class ProcessorIntegrationTest extends FacetsTestBase {
$this->checkHideActiveItems(); $this->checkHideActiveItems();
} }
/**
* Tests the for processors in the frontend with a 'boolean' facet.
*/
public function testBooleanProcessorIntegration() {
$field_name = 'field_boolean';
$field_storage = FieldStorageConfig::create([
'field_name' => $field_name,
'entity_type' => 'entity_test_mulrev_changed',
'type' => 'boolean',
]);
$field_storage->save();
$field = FieldConfig::create([
'field_storage' => $field_storage,
'bundle' => 'item',
]);
$field->save();
$index = $this->getIndex();
// Index the taxonomy and entity reference fields.
$boolean_field = new Field($index, $field_name);
$boolean_field->setType('integer');
$boolean_field->setPropertyPath($field_name);
$boolean_field->setDatasourceId('entity:entity_test_mulrev_changed');
$boolean_field->setLabel('BooleanField');
$index->addField($boolean_field);
$index->save();
$this->indexItems($this->indexId);
$entity_test_storage = \Drupal::entityTypeManager()
->getStorage('entity_test_mulrev_changed');
$entity_test_storage->create([
'name' => 'foo bar baz',
'body' => 'test test',
'type' => 'item',
'keywords' => ['orange'],
'category' => 'item_category',
$field_name => TRUE,
])->save();
$this->indexItems($this->indexId);
$facet_name = "Boolean";
$facet_id = "boolean";
// Create facet
$this->editForm = 'admin/config/search/facets/' . $facet_id . '/edit';
$this->createFacet($facet_name, $facet_id, $field_name);
// Check values
$this->drupalGet('search-api-test-fulltext');
$this->assertFacetLabel('1');
$form = [
'facet_settings[boolean_item][status]' => TRUE,
'facet_settings[boolean_item][settings][on_value]' => 'Yes',
'facet_settings[boolean_item][settings][off_value]' => 'No',
];
$this->drupalPostForm($this->editForm, $form, 'Save');
$this->assertResponse(200);
$this->assertFieldChecked('edit-facet-settings-boolean-item-status');
$this->drupalGet('search-api-test-fulltext');
$this->assertFacetLabel('Yes');
$form = [
'facet_settings[boolean_item][status]' => TRUE,
'facet_settings[boolean_item][settings][on_value]' => 'Øn',
];
$this->drupalPostForm($this->editForm, $form, 'Save');
$this->drupalGet('search-api-test-fulltext');
$this->assertFacetLabel('Øn');
}
/** /**
* Tests the for sorting processors in the frontend with a 'keywords' facet. * Tests the for sorting processors in the frontend with a 'keywords' facet.
*/ */
......
<?php
namespace Drupal\Tests\facets\Unit\Plugin\processor;
use Drupal\facets\Entity\Facet;
use Drupal\facets\Plugin\facets\processor\BooleanItemProcessor;
use Drupal\facets\Result\Result;
use Drupal\Tests\UnitTestCase;
/**
* Unit test for processor.
*
* @group facets
*/
class BooleanItemProcessorTest extends UnitTestCase {
/**
* The processor to be tested.
*
* @var \Drupal\facets\processor\BuildProcessorInterface
*/
protected $processor;
/**
* An array containing the results before the processor has ran.
*
* @var \Drupal\facets\Result\Result[]
*/
protected $originalResults;
/**
* Creates a new processor object for use in the tests.
*/
protected function setUp() {
parent::setUp();
$this->originalResults = [
new Result(0, 0, 10),
new Result(1, 1, 15),
];
$this->processor = new BooleanItemProcessor([], 'boolean_item_processor', []);
}
/**
* Tests filtering of results.
*/
public function testBuild() {
$facet = new Facet([], 'facets_facet');
$facet->setResults($this->originalResults);
$filtered_results = $this->processor->build($facet, $this->originalResults);
// The default values for on / off are On and Off.
$this->assertEquals('Off', $filtered_results[0]->getDisplayValue());
$this->assertEquals('On', $filtered_results[1]->getDisplayValue());
// Overwrite the on/off values.
$configuration = ['on_value' => 'True', 'off_value' => 'False'];
$this->processor->setConfiguration($configuration);
$filtered_results = $this->processor->build($facet, $this->originalResults);
$this->assertEquals('False', $filtered_results[0]->getDisplayValue());
$this->assertEquals('True', $filtered_results[1]->getDisplayValue());
}
/**
* Tests configuration.
*/
public function testConfiguration() {
$config = $this->processor->defaultConfiguration();
$this->assertEquals(['on_value' => 'On', 'off_value' => 'Off'], $config);
}
/**
* Tests testDescription().
*/
public function testDescription() {
$this->assertEquals('', $this->processor->getDescription());
}
/**
* Tests isHidden().
*/
public function testIsHidden() {
$this->assertEquals(FALSE, $this->processor->isHidden());
}
/**
* Tests isLocked().
*/
public function testIsLocked() {
$this->assertEquals(FALSE, $this->processor->isLocked());
}
}
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