Commit 262fe2ed authored by bc's avatar bc Committed by borisson_
Browse files

Issue #2709515 by bc, borisson_: Display labels for entity base fields as well...

Issue #2709515 by bc, borisson_: Display labels for entity base fields as well as field.module fields
parent e9045d48
......@@ -3,6 +3,7 @@
namespace Drupal\facets\Plugin\facets\processor;
use Drupal\Core\Config\ConfigManagerInterface;
use Drupal\Core\Entity\EntityFieldManagerInterface;
use Drupal\Core\Plugin\ContainerFactoryPluginInterface;
use Drupal\facets\FacetInterface;
use Drupal\facets\FacetSource\SearchApiFacetSourceInterface;
......@@ -31,13 +32,22 @@ class ListItemProcessor extends ProcessorPluginBase implements BuildProcessorInt
*/
private $configManager;
/**
* The entity field manager.
*
* @var \Drupal\Core\Entity\EntityFieldManagerInterface
*/
private $entityFieldManager;
/**
* {@inheritdoc}
*/
public function __construct(array $configuration, $plugin_id, $plugin_definition, ConfigManagerInterface $config_manager) {
public function __construct(array $configuration, $plugin_id, $plugin_definition, ConfigManagerInterface $config_manager, EntityFieldManagerInterface $entity_field_manager) {
parent::__construct($configuration, $plugin_id, $plugin_definition);
$this->configManager = $config_manager;
$this->entityFieldManager = $entity_field_manager;
}
/**
......@@ -48,7 +58,8 @@ class ListItemProcessor extends ProcessorPluginBase implements BuildProcessorInt
$configuration,
$plugin_id,
$plugin_definition,
$container->get('config.manager')
$container->get('config.manager'),
$container->get('entity_field.manager')
);
}
......@@ -67,8 +78,22 @@ class ListItemProcessor extends ProcessorPluginBase implements BuildProcessorInt
$entity = str_replace('entity:', '', $field->getDatasourceId());
}
// If it's an entity base field, we find it in the field definitions.
// We don't have access to the bundle via SearchApiFacetSourceInterface, so
// we check the entity's base fields only.
$base_fields = $this->entityFieldManager->getFieldDefinitions($entity, '');
// This only works for configurable fields.
$config_entity_name = sprintf('field.storage.%s.%s', $entity, $field_identifier);
if ($field = $this->configManager->loadConfigEntityByName($config_entity_name)) {
if (isset($base_fields[$field_identifier])) {
$field = $base_fields[$field_identifier];
}
elseif ($this->configManager->loadConfigEntityByName($config_entity_name) !== NULL) {
$field = $this->configManager->loadConfigEntityByName($config_entity_name);
}
if ($field) {
$function = $field->getSetting('allowed_values_function');
if (empty($function)) {
......@@ -87,6 +112,7 @@ class ListItemProcessor extends ProcessorPluginBase implements BuildProcessorInt
}
}
}
return $results;
}
......
......@@ -8,13 +8,15 @@ use Drupal\facets\Result\Result;
use Drupal\field\Entity\FieldStorageConfig;
use Drupal\Tests\UnitTestCase;
use Drupal\Core\Config\ConfigManager;
use Drupal\Core\Entity\EntityFieldManager;
use Drupal\Core\Field\BaseFieldDefinition;
/**
* Unit test for processor.
*
* @group facets
*/
class LimitItemProcessorTest extends UnitTestCase {
class ListItemProcessorTest extends UnitTestCase {
/**
* The processor to be tested.
......@@ -28,7 +30,7 @@ class LimitItemProcessorTest extends UnitTestCase {
*
* @var \Drupal\facets\Result\Result[]
*/
protected $originalResults;
protected $results;
/**
* Creates a new processor object for use in the tests.
......@@ -36,7 +38,7 @@ class LimitItemProcessorTest extends UnitTestCase {
protected function setUp() {
parent::setUp();
$this->originalResults = [
$this->results = [
new Result(1, 1, 10),
new Result(2, 2, 5),
new Result(3, 3, 15),
......@@ -46,51 +48,118 @@ class LimitItemProcessorTest extends UnitTestCase {
->disableOriginalConstructor()
->getMock();
$entity_field_manager = $this->getMockBuilder(EntityFieldManager::class)
->disableOriginalConstructor()
->getMock();
$processor_id = 'list_item';
$this->processor = new ListItemProcessor([], $processor_id, [], $config_manager);
$this->processor = new ListItemProcessor([], $processor_id, [], $config_manager, $entity_field_manager);
}
/**
* Tests facet build method.
* Tests facet build with field.module field.
*/
public function testNoFilter() {
$field = $this->getMockBuilder(FieldStorageConfig::class)
public function testFacetFieldmoduleBuild() {
$module_field = $this->getMockBuilder(FieldStorageConfig::class)
->disableOriginalConstructor()
->getMock();
$field->expects($this->at(0))
$module_field->expects($this->at(0))
->method('getSetting')
->with('allowed_values_function')
->willReturn('');
$field->expects($this->at(1))
$module_field->expects($this->at(1))
->method('getSetting')
->with('allowed_values')
->willReturn([1 => 'llama', 2 => 'badger', 3 => 'kitten']);
->willReturn([
1 => 'llama',
2 => 'badger',
3 => 'kitten',
]);
$config_manager = $this->getMockBuilder(ConfigManager::class)
->disableOriginalConstructor()
->getMock();
$config_manager->expects($this->any())
->method('loadConfigEntityByName')
->willReturn($field);
->willReturn($module_field);
$entity_field_manager = $this->getMockBuilder(EntityFieldManager::class)
->disableOriginalConstructor()
->getMock();
$processor_id = 'list_item';
$processor = new ListItemProcessor([], $processor_id, [], $config_manager, $entity_field_manager);
// Config entity field facet.
$module_field_facet = new Facet([], 'facet');
$module_field_facet->setFieldIdentifier('test_facet');
$module_field_facet->setResults($this->results);
$module_field_facet->addProcessor([
'processor_id' => 'list_item',
'weights' => [],
'settings' => [],
]);
/** @var \Drupal\facets\Result\Result[] $module_field_facet- */
$module_field_results = $processor->build($module_field_facet, $this->results);
$this->assertCount(3, $module_field_results);
$this->assertEquals('llama', $module_field_results[0]->getDisplayValue());
$this->assertEquals('badger', $module_field_results[1]->getDisplayValue());
$this->assertEquals('kitten', $module_field_results[2]->getDisplayValue());
}
/**
* Tests facet build with base props.
*/
public function testFacetBasepropBuild() {
$config_manager = $this->getMockBuilder(ConfigManager::class)
->disableOriginalConstructor()
->getMock();
$base_field = $this->getMockBuilder(BaseFieldDefinition::class)
->disableOriginalConstructor()
->getMock();
$base_field->expects($this->any())
->method('getSetting')
->willReturnMap([
['allowed_values_function', ''],
['allowed_values', [
1 => 'blue whale',
2 => 'lynx',
3 => 'dog-wolf-lion',
]],
]);
$entity_field_manager = $this->getMockBuilder(EntityFieldManager::class)
->disableOriginalConstructor()
->getMock();
$entity_field_manager->expects($this->any())
->method('getFieldDefinitions')
->with('node', '')
->willReturn([
'test_facet_baseprop' => $base_field,
]);
$processor_id = 'list_item';
$processor = new ListItemProcessor([], $processor_id, [], $config_manager);
$processor = new ListItemProcessor([], $processor_id, [], $config_manager, $entity_field_manager);
$facet = new Facet([], 'facet');
$facet->setFieldIdentifier('test_facet');
$facet->setResults($this->originalResults);
$facet->addProcessor([
// Base prop facet.
$base_prop_facet = new Facet([], 'facet');
$base_prop_facet->setFieldIdentifier('test_facet_baseprop');
$base_prop_facet->setResults($this->results);
$base_prop_facet->addProcessor([
'processor_id' => 'list_item',
'weights' => [],
'settings' => [],
]);
/** @var \Drupal\facets\Result\Result[] $sorted_results */
$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());
/** @var \Drupal\facets\Result\Result[] $base_prop_results */
$base_prop_results = $processor->build($base_prop_facet, $this->results);
$this->assertCount(3, $base_prop_results);
$this->assertEquals('blue whale', $base_prop_results[0]->getDisplayValue());
$this->assertEquals('lynx', $base_prop_results[1]->getDisplayValue());
$this->assertEquals('dog-wolf-lion', $base_prop_results[2]->getDisplayValue());
}
/**
......
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