diff --git a/src/Plugin/facets/processor/ListItemProcessor.php b/src/Plugin/facets/processor/ListItemProcessor.php index 1d2011b..12516c3 100644 --- a/src/Plugin/facets/processor/ListItemProcessor.php +++ b/src/Plugin/facets/processor/ListItemProcessor.php @@ -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; @@ -32,12 +33,21 @@ 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; } diff --git a/tests/src/Unit/Plugin/processor/LimitItemProcessorTest.php b/tests/src/Unit/Plugin/processor/LimitItemProcessorTest.php deleted file mode 100644 index a12efea..0000000 --- a/tests/src/Unit/Plugin/processor/LimitItemProcessorTest.php +++ /dev/null @@ -1,125 +0,0 @@ -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' => [], - ]); - /** @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()); - } - - /** - * 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()); - } - -} diff --git a/tests/src/Unit/Plugin/processor/ListItemProcessorTest.php b/tests/src/Unit/Plugin/processor/ListItemProcessorTest.php new file mode 100644 index 0000000..8462d19 --- /dev/null +++ b/tests/src/Unit/Plugin/processor/ListItemProcessorTest.php @@ -0,0 +1,194 @@ +results = [ + new Result(1, 1, 10), + new Result(2, 2, 5), + new Result(3, 3, 15), + ]; + + $config_manager = $this->getMockBuilder(ConfigManager::class) + ->disableOriginalConstructor() + ->getMock(); + + $entity_field_manager = $this->getMockBuilder(EntityFieldManager::class) + ->disableOriginalConstructor() + ->getMock(); + + $processor_id = 'list_item'; + $this->processor = new ListItemProcessor([], $processor_id, [], $config_manager, $entity_field_manager); + } + + /** + * Tests facet build with field.module field. + */ + public function testFacetFieldmoduleBuild() { + $module_field = $this->getMockBuilder(FieldStorageConfig::class) + ->disableOriginalConstructor() + ->getMock(); + $module_field->expects($this->at(0)) + ->method('getSetting') + ->with('allowed_values_function') + ->willReturn(''); + $module_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($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, $entity_field_manager); + + // 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[] $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()); + } + + /** + * 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()); + } + +}