Commit 65e4a1b6 authored by borisson_'s avatar borisson_
Browse files

Revert "Issue #2773113 by borisson_, ndrake86, YurkinPark, swentel: Using the...

Revert "Issue #2773113 by borisson_, ndrake86, YurkinPark, swentel: Using the List Value processor to display label for Search API Fields throws an error"

This reverts commit 5132b292.
parent 5132b292
......@@ -76,6 +76,3 @@ plugin.plugin_configuration.facets_processor.hide_non_narrowing_result_processor
plugin.plugin_configuration.facets_processor.hide_active_items_processor:
type: config_object
plugin.plugin_configuration.facets_processor.list_item:
type: config_object
......@@ -5,7 +5,6 @@ namespace Drupal\facets\Plugin\facets\processor;
use Drupal\Core\Config\ConfigManagerInterface;
use Drupal\Core\Entity\EntityFieldManagerInterface;
use Drupal\Core\Entity\EntityTypeBundleInfoInterface;
use Drupal\Core\Field\FieldStorageDefinitionInterface;
use Drupal\Core\Plugin\ContainerFactoryPluginInterface;
use Drupal\facets\FacetInterface;
use Drupal\facets\FacetSource\SearchApiFacetSourceInterface;
......@@ -92,8 +91,6 @@ class ListItemProcessor extends ProcessorPluginBase implements BuildProcessorInt
public function build(FacetInterface $facet, array $results) {
$field_identifier = $facet->getFieldIdentifier();
$entity = 'node';
$field = FALSE;
$allowed_values = [];
// Support multiple entities when using Search API.
if ($facet->getFacetSource() instanceof SearchApiFacetSourceInterface) {
......@@ -118,45 +115,34 @@ class ListItemProcessor extends ProcessorPluginBase implements BuildProcessorInt
$field = $this->configManager->loadConfigEntityByName($config_entity_name);
}
if ($field instanceof FieldStorageDefinitionInterface) {
if ($field->getType() !== 'entity_reference' && $field->getName() !== 'type') {
$allowed_values = options_allowed_values($field);
return $this->overWriteDisplayValues($results, $allowed_values);
}
}
if ($field) {
$function = $field->getSetting('allowed_values_function');
// If no values are found for the current field, try to see if this is a
// bundle field.
$list_bundles = $this->entityTypeBundleInfo->getBundleInfo($entity);
if ($list_bundles) {
foreach ($list_bundles as $key => $bundle) {
$allowed_values[$key] = $bundle['label'];
if (empty($function)) {
$allowed_values = $field->getSetting('allowed_values');
}
else {
$allowed_values = ${$function}($field);
}
return $this->overWriteDisplayValues($results, $allowed_values);
}
return $results;
}
// 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'];
}
}
/**
* Overwrite the display value of the result with a new text.
*
* @param \Drupal\facets\Result\ResultInterface[] $results
* An array of results to work on.
* @param array $replacements
* An array of values that contain possible replacements for the orignal
* values.
*
* @return \Drupal\facets\Result\ResultInterface[]
* The changed results.
*/
protected function overWriteDisplayValues(array $results, array $replacements) {
/** @var \Drupal\facets\Result\ResultInterface $a */
foreach ($results as &$a) {
if (isset($replacements[$a->getRawValue()])) {
$a->setDisplayValue($replacements[$a->getRawValue()]);
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;
}
......
......@@ -247,7 +247,7 @@ class IntegrationTest extends FacetsTestBase {
$this->blocks[$facet_id] = $this->createBlock($facet_id);
$this->blocks[$depending_facet_id] = $this->createBlock($depending_facet_id);
// Go to the view and test that both facets are shown. Item and article
// Go the the view and test that both facets are shown. Item and article
// come from the DependableFacet, orange and grape come from DependingFacet.
$this->drupalGet('search-api-test-fulltext');
$this->assertFacetLabel('grape');
......
......@@ -18,13 +18,6 @@ class ProcessorIntegrationTest extends FacetsTestBase {
*/
protected $editForm;
/**
* {@inheritdoc}
*/
public static $modules = [
'options',
];
/**
* {@inheritdoc}
*/
......@@ -540,45 +533,4 @@ class ProcessorIntegrationTest extends FacetsTestBase {
$this->drupalPostForm($path, $settings, 'Save');
}
/**
* Checks if the list processor changes machine name to the display label.
*/
public function testListProcessor() {
entity_test_create_bundle('basic', "Basic page", 'entity_test_mulrev_changed');
$entity_test_storage = \Drupal::entityTypeManager()
->getStorage('entity_test_mulrev_changed');
// Add an entity with basic page content type.
$entity_test_storage->create([
'name' => 'AC0871108',
'body' => 'Eamus Catuli',
'type' => 'basic',
])->save();
$this->indexItems($this->indexId);
$facet_name = "Eamus Catuli";
$facet_id = "eamus_catuli";
$editForm = 'admin/config/search/facets/' . $facet_id . '/edit';
$this->createFacet($facet_name, $facet_id);
// Go to the overview and check that the machine names are used as facets.
$this->drupalGet('search-api-test-fulltext');
$this->assertText('Displaying 11 search results');
$this->assertFacetLabel('basic');
// Edit the facet to use the list_item processor.
$edit = [
'facet_settings[list_item][status]' => TRUE,
];
$this->drupalPostForm($editForm, $edit, 'Save');
$this->assertResponse(200);
$this->assertFieldChecked('edit-facet-settings-list-item-status');
// Go back to the overview and check that now the label is being used
// instead.
$this->drupalGet('search-api-test-fulltext');
$this->assertText('Displaying 11 search results');
$this->assertFacetLabel('Basic page');
}
}
......@@ -57,23 +57,34 @@ class ListItemProcessorTest extends UnitTestCase {
->disableOriginalConstructor()
->getMock();
$this->processor = new ListItemProcessor([], 'list_item', [], $config_manager, $entity_field_manager, $entity_type_bundle_info);
$processor_id = 'list_item';
$this->processor = new ListItemProcessor([], $processor_id, [], $config_manager, $entity_field_manager, $entity_type_bundle_info);
}
/**
* Tests facet build with field.module field.
*/
public function testBuildConfigurableField() {
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',
]);
// Make sure that when the processor calls loadConfigEntityByName the field
// we created here is called.
$config_manager = $this->getMockBuilder(ConfigManager::class)
->disableOriginalConstructor()
->getMock();
$config_manager->expects($this->exactly(2))
$config_manager->expects($this->any())
->method('loadConfigEntityByName')
->willReturn($module_field);
......@@ -85,7 +96,8 @@ class ListItemProcessorTest extends UnitTestCase {
->disableOriginalConstructor()
->getMock();
$processor = new ListItemProcessor([], 'list_item', [], $config_manager, $entity_field_manager, $entity_type_bundle_info);
$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');
......@@ -108,15 +120,23 @@ class ListItemProcessorTest extends UnitTestCase {
/**
* Tests facet build with field.module field.
*/
public function testBuildBundle() {
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->exactly(2))
$config_manager->expects($this->any())
->method('loadConfigEntityByName')
->willReturn($module_field);
......@@ -127,8 +147,16 @@ class ListItemProcessorTest extends UnitTestCase {
$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 = new ListItemProcessor([], 'list_item', [], $config_manager, $entity_field_manager, $entity_type_bundle_info);
$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');
......@@ -143,15 +171,15 @@ class ListItemProcessorTest extends UnitTestCase {
$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());
$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 testBuildBaseField() {
public function testFacetBasepropBuild() {
$config_manager = $this->getMockBuilder(ConfigManager::class)
->disableOriginalConstructor()
->getMock();
......@@ -159,6 +187,19 @@ class ListItemProcessorTest extends UnitTestCase {
$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()
......@@ -174,7 +215,8 @@ class ListItemProcessorTest extends UnitTestCase {
->disableOriginalConstructor()
->getMock();
$processor = new ListItemProcessor([], 'list_item', [], $config_manager, $entity_field_manager, $entity_type_bundle_info);
$processor_id = 'list_item';
$processor = new ListItemProcessor([], $processor_id, [], $config_manager, $entity_field_manager, $entity_type_bundle_info);
// Base prop facet.
$base_prop_facet = new Facet([], 'facets_facet');
......@@ -190,9 +232,9 @@ class ListItemProcessorTest extends UnitTestCase {
$base_prop_results = $processor->build($base_prop_facet, $this->results);
$this->assertCount(3, $base_prop_results);
$this->assertEquals('llama', $base_prop_results[0]->getDisplayValue());
$this->assertEquals('badger', $base_prop_results[1]->getDisplayValue());
$this->assertEquals('kitten', $base_prop_results[2]->getDisplayValue());
$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());
}
/**
......@@ -225,15 +267,3 @@ class ListItemProcessorTest extends UnitTestCase {
}
}
namespace Drupal\facets\Plugin\facets\processor;
if (!function_exists('options_allowed_values')) {
function options_allowed_values() {
return [
1 => 'llama',
2 => 'badger',
3 => 'kitten',
];
}
}
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