Commit 3b10da82 authored by jeroen_drenth's avatar jeroen_drenth Committed by borisson_
Browse files

Issue #2675426 by jeroen_drenth, borisson_, stBorchert: Display labels from lists instead of keys

parent f60b0842
<?php
/**
* @file
* Contains \Drupal\facets\FacetSource\SearchApiFacetSourceInterface.
*/
namespace Drupal\facets\FacetSource;
/**
* A facet source that uses search api as a base.
*/
interface SearchApiFacetSourceInterface {
/**
* Returns the search_api index.
*
* @return \Drupal\search_api\IndexInterface
* The search api index.
*/
public function getIndex();
}
......@@ -8,6 +8,7 @@
namespace Drupal\facets\Plugin\facets\facet_source;
use Drupal\Core\DependencyInjection\DependencySerializationTrait;
use Drupal\facets\FacetSource\SearchApiFacetSourceInterface;
use Drupal\search_api\Plugin\views\query\SearchApiQuery;
use Drupal\search_api\Query\ResultSetInterface;
use Drupal\views\Views;
......@@ -21,7 +22,7 @@ use Drupal\views\Views;
* deriver = "Drupal\facets\Plugin\facets\facet_source\SearchApiViewsPageDeriver"
* )
*/
class SearchApiViewsPage extends SearchApiBaseFacetSource {
class SearchApiViewsPage extends SearchApiBaseFacetSource implements SearchApiFacetSourceInterface {
use DependencySerializationTrait;
......@@ -46,6 +47,13 @@ class SearchApiViewsPage extends SearchApiBaseFacetSource {
*/
protected $configFactory;
/**
* The search index the query should is executed on.
*
* @var \Drupal\search_api\IndexInterface
*/
protected $index;
/**
* {@inheritdoc}
*/
......@@ -132,4 +140,11 @@ class SearchApiViewsPage extends SearchApiBaseFacetSource {
return FALSE;
}
/**
* {@inheritdoc}
*/
public function getIndex() {
return $this->index;
}
}
<?php
/**
* @file
* Contains \Drupal\facets\Plugin\facets\ListItemProcessor.
*/
namespace Drupal\facets\Plugin\facets\processor;
use Drupal\Core\Config\ConfigManager;
use Drupal\Core\Plugin\ContainerFactoryPluginInterface;
use Drupal\facets\FacetInterface;
use Drupal\facets\FacetSource\SearchApiFacetSourceInterface;
use Drupal\facets\Processor\BuildProcessorInterface;
use Drupal\facets\Processor\ProcessorPluginBase;
use Symfony\Component\DependencyInjection\ContainerInterface;
/**
* Provides a processor that transforms the results to show the list item label.
*
* @FacetsProcessor(
* id = "list_item",
* label = @Translation("List item label"),
* description = @Translation("Display the list item label instead of the key"),
* stages = {
* "build" = 5
* }
* )
*/
class ListItemProcessor extends ProcessorPluginBase implements BuildProcessorInterface, ContainerFactoryPluginInterface {
/**
* @var \Drupal\Core\Entity\EntityTypeManager
*/
private $configManager;
/**
* {@inheritdoc}
*/
public function __construct(array $configuration, $plugin_id, $plugin_definition, ConfigManager $config_manager) {
parent::__construct($configuration, $plugin_id, $plugin_definition);
$this->configManager = $config_manager;
}
/**
* {@inheritdoc}
*/
public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
return new static(
$configuration,
$plugin_id,
$plugin_definition,
$container->get('config.manager')
);
}
/**
* {@inheritdoc}
*/
public function build(FacetInterface $facet, array $results) {
$field_identifier = $facet->getFieldIdentifier();
$entity = 'node';
// Support multiple entities when using Search API.
if ($facet->getFacetSource() instanceof SearchApiFacetSourceInterface) {
$index = $facet->getFacetSource()->getIndex();
$field = $index->getField($field_identifier);
$entity = str_replace('entity:' , '', $field->getDatasourceId());
}
$config_entity_name = sprintf('field.storage.%s.%s', $entity, $field_identifier);
if ($field = $this->configManager->loadConfigEntityByName($config_entity_name)) {
$function = $field->getSetting('allowed_values_function');
if (empty($function)) {
$allowed_values = $field->getSetting('allowed_values');
}
else {
$allowed_values = ${$function}($field);
}
if (is_array($allowed_values)) {
/** @var \Drupal\facets\Result\ResultInterface $result */
foreach ($results as &$result) {
if (isset($allowed_values[$result->getRawValue()])) {
$result->setDisplayValue($allowed_values[$result->getRawValue()]);
}
}
}
}
return $results;
}
}
<?php
/**
* @file
* Contains \Drupal\Tests\facets\Plugin\Processor\LimitItemProcessorTest.
*/
namespace Drupal\Tests\facets\Unit\Plugin\processor;
use Drupal\facets\Entity\Facet;
use Drupal\facets\Plugin\facets\processor\ListItemProcessor;
use Drupal\facets\Result\Result;
use Drupal\field\Entity\FieldStorageConfig;
use Drupal\Tests\UnitTestCase;
use Drupal\Core\Config\ConfigManager;
use Symfony\Component\DependencyInjection\ContainerBuilder;
/**
* Unit test for processor.
*
* @group facets
*/
class LimitItemProcessorTest 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(1, 1, 10),
new Result(2, 2, 5),
new Result(3, 3, 15),
];
$config_manager = $this->getMockBuilder(ConfigManager::class)
->disableOriginalConstructor()
->getMock();
$processor_id = 'list_item';
$this->processor = new ListItemProcessor([], $processor_id, [], $config_manager);
}
/**
* Tests facet build method.
*/
public function testNoFilter() {
$field = $this->getMockBuilder(FieldStorageConfig::class)
->disableOriginalConstructor()
->getMock();
$field->expects($this->at(0))
->method('getSetting')
->with('allowed_values_function')
->willReturn('');
$field->expects($this->at(1))
->method('getSetting')
->with('allowed_values')
->willReturn([1 => 'llama', 2 => 'badger', 3 => 'kitten']);
$config_manager = $this->getMockBuilder(ConfigManager::class)
->disableOriginalConstructor()
->getMock();
$config_manager->expects($this->any())
->method('loadConfigEntityByName')
->willReturn($field);
$processor_id = 'list_item';
$processor = new ListItemProcessor([], $processor_id, [], $config_manager);
$facet = new Facet([], 'facet');
$facet->setFieldIdentifier('test_facet');
$facet->setResults($this->originalResults);
$facet->addProcessor([
'processor_id' => 'list_item',
'weights' => [],
'settings' => [],
]);
$sorted_results = $processor->build($facet, $this->originalResults);
$this->assertCount(3, $sorted_results);
$this->assertEquals('llama', $sorted_results[0]->getDisplayValue());
$this->assertEquals('badger', $sorted_results[1]->getDisplayValue());
$this->assertEquals('kitten', $sorted_results[2]->getDisplayValue());
}
/**
* Tests configuration.
*/
public function testConfiguration() {
$config = $this->processor->defaultConfiguration();
$this->assertEquals([], $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());
}
}
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