diff --git a/src/Plugin/facets/processor/ListItemProcessor.php b/src/Plugin/facets/processor/ListItemProcessor.php index d10d6f8..7bd683f 100644 --- a/src/Plugin/facets/processor/ListItemProcessor.php +++ b/src/Plugin/facets/processor/ListItemProcessor.php @@ -18,7 +18,7 @@ use Symfony\Component\DependencyInjection\ContainerInterface; * @FacetsProcessor( * id = "list_item", * label = @Translation("List item label"), - * description = @Translation("Fields that are a list (such as list (integer), list (text)) can use this processor to show the value instead of the key."), + * description = @Translation("Fields that are a list (such as list (integer), list (text)) or a bundle field can use this processor to show the value instead of the key."), * stages = { * "build" = 5 * } @@ -48,15 +48,26 @@ class ListItemProcessor extends ProcessorPluginBase implements BuildProcessorInt private $entityTypeBundleInfo; /** - * {@inheritdoc} + * Constructs a Drupal\Component\Plugin\PluginBase object. + * + * @param array $configuration + * A configuration array containing information about the plugin instance. + * @param string $plugin_id + * The plugin_id for the plugin instance. + * @param mixed $plugin_definition + * The plugin implementation definition. + * @param \Drupal\Core\Config\ConfigManagerInterface $config_manager + * The config manager. + * @param \Drupal\Core\Entity\EntityFieldManagerInterface $entity_field_manager + * The entity field manager. + * @param \Drupal\Core\Entity\EntityTypeBundleInfoInterface $entity_type_bundle_info + * The entity bundle info service. */ public function __construct(array $configuration, $plugin_id, $plugin_definition, ConfigManagerInterface $config_manager, EntityFieldManagerInterface $entity_field_manager, EntityTypeBundleInfoInterface $entity_type_bundle_info) { parent::__construct($configuration, $plugin_id, $plugin_definition); $this->configManager = $config_manager; - $this->entityFieldManager = $entity_field_manager; - $this->entityTypeBundleInfo = $entity_type_bundle_info; } @@ -114,6 +125,8 @@ class ListItemProcessor extends ProcessorPluginBase implements BuildProcessorInt $allowed_values = ${$function}($field); } + // If no values are found for the current field, try to see if this is a + // bundle field. if (empty($allowed_values) && $bundles = $this->entityTypeBundleInfo->getBundleInfo($entity)) { foreach ($bundles as $key => $bundle) { $allowed_values[$key] = $bundle['label']; diff --git a/tests/src/Unit/Plugin/processor/ListItemProcessorTest.php b/tests/src/Unit/Plugin/processor/ListItemProcessorTest.php index c5c4db7..a941d42 100644 --- a/tests/src/Unit/Plugin/processor/ListItemProcessorTest.php +++ b/tests/src/Unit/Plugin/processor/ListItemProcessorTest.php @@ -118,6 +118,65 @@ class ListItemProcessorTest extends UnitTestCase { } /** + * Tests facet build with field.module field. + */ + public function testBundle() { + $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([]); + + $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(); + + $entity_type_bundle_info = $this->getMockBuilder(EntityTypeBundleInfo::class) + ->disableOriginalConstructor() + ->getMock(); + $entity_type_bundle_info->expects($this->exactly(1)) + ->method('getBundleInfo') + ->willReturn([ + 1 => ['label' => 'Monkey'], + 2 => ['label' => 'Donkey'], + 3 => ['label' => 'Kong'], + ]); + + $processor_id = 'list_item'; + $processor = new ListItemProcessor([], $processor_id, [], $config_manager, $entity_field_manager, $entity_type_bundle_info); + + // Config entity field facet. + $module_field_facet = new Facet([], 'facets_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('Monkey', $module_field_results[0]->getDisplayValue()); + $this->assertEquals('Donkey', $module_field_results[1]->getDisplayValue()); + $this->assertEquals('Kong', $module_field_results[2]->getDisplayValue()); + } + + /** * Tests facet build with base props. */ public function testFacetBasepropBuild() {