From e1108f03efa6070dfcbcada4a7778f3f74bf08d0 Mon Sep 17 00:00:00 2001 From: borisson_ <borisson_@2393360.no-reply.drupal.org> Date: Tue, 5 Dec 2017 19:53:34 +0100 Subject: [PATCH] Issue #2909262 by borisson_: Limit taxonomy weight processor to entity reference fields, add a test --- .../TermWeightWidgetOrderProcessor.php | 28 ++++++++++++++++++ .../HierarchicalFacetIntegrationTest.php | 29 +++++++++++++++++++ .../Functional/ProcessorIntegrationTest.php | 10 +++++-- 3 files changed, 65 insertions(+), 2 deletions(-) diff --git a/src/Plugin/facets/processor/TermWeightWidgetOrderProcessor.php b/src/Plugin/facets/processor/TermWeightWidgetOrderProcessor.php index c471a4e0..c69c03a1 100644 --- a/src/Plugin/facets/processor/TermWeightWidgetOrderProcessor.php +++ b/src/Plugin/facets/processor/TermWeightWidgetOrderProcessor.php @@ -4,6 +4,9 @@ namespace Drupal\facets\Plugin\facets\processor; use Drupal\Core\Entity\EntityTypeManagerInterface; use Drupal\Core\Plugin\ContainerFactoryPluginInterface; +use Drupal\Core\TypedData\ComplexDataDefinitionInterface; +use Drupal\Core\TypedData\DataReferenceDefinitionInterface; +use Drupal\facets\FacetInterface; use Drupal\facets\Processor\SortProcessorPluginBase; use Drupal\facets\Result\Result; use Symfony\Component\DependencyInjection\ContainerInterface; @@ -86,4 +89,29 @@ class TermWeightWidgetOrderProcessor extends SortProcessorPluginBase implements return ($term_a->getWeight() < $term_b->getWeight()) ? -1 : 1; } + /** + * {@inheritdoc} + */ + public function supportsFacet(FacetInterface $facet) { + $data_definition = $facet->getDataDefinition(); + if ($data_definition->getDataType() === 'entity_reference') { + return TRUE; + } + if (!($data_definition instanceof ComplexDataDefinitionInterface)) { + return FALSE; + } + + $data_definition = $facet->getDataDefinition(); + $property_definitions = $data_definition->getPropertyDefinitions(); + foreach ($property_definitions as $definition) { + if ($definition instanceof DataReferenceDefinitionInterface + && $definition->getDataType() === 'entity_reference' + && $definition->getConstraint('EntityType') === 'taxonomy_term' + ) { + return TRUE; + } + } + return FALSE; + } + } diff --git a/tests/src/Functional/HierarchicalFacetIntegrationTest.php b/tests/src/Functional/HierarchicalFacetIntegrationTest.php index 9d36464c..7f3f220f 100644 --- a/tests/src/Functional/HierarchicalFacetIntegrationTest.php +++ b/tests/src/Functional/HierarchicalFacetIntegrationTest.php @@ -203,6 +203,35 @@ class HierarchicalFacetIntegrationTest extends FacetsTestBase { $this->assertStringPosition('Child 2', 'Child 1'); } + /** + * Tests sorting by weight of a taxonomy term. + */ + public function testWeightSort() { + $edit = [ + 'facet_settings[translate_entity][status]' => '1', + 'facet_sorting[term_weight_widget_order][status]' => '1', + ]; + $this->drupalPostForm($this->facetEditPage, $edit, 'Save'); + + $this->parents['Parent 1']->setWeight(15); + $this->parents['Parent 1']->save(); + $this->parents['Parent 2']->setWeight(25); + $this->parents['Parent 2']->save(); + + $this->drupalGet('search-api-test-fulltext'); + $this->assertFacetLabel('Parent 1'); + $this->assertFacetLabel('Parent 2'); + $this->assertStringPosition('Parent 1', 'Parent 2'); + + $this->parents['Parent 2']->setWeight(5); + $this->parents['Parent 2']->save(); + + $this->drupalGet('search-api-test-fulltext'); + $this->assertFacetLabel('Parent 1'); + $this->assertFacetLabel('Parent 2'); + $this->assertStringPosition('Parent 2', 'Parent 1'); + } + /** * Verify the "Enable parent when child gets disabled" option is working. */ diff --git a/tests/src/Functional/ProcessorIntegrationTest.php b/tests/src/Functional/ProcessorIntegrationTest.php index 2b901162..4c21686e 100644 --- a/tests/src/Functional/ProcessorIntegrationTest.php +++ b/tests/src/Functional/ProcessorIntegrationTest.php @@ -146,7 +146,7 @@ class ProcessorIntegrationTest extends FacetsTestBase { $index = $this->getIndex(); - // Index the taxonomy and entity reference fields. + // Index a boolean field. $boolean_field = new Field($index, $field_name); $boolean_field->setType('integer'); $boolean_field->setPropertyPath($field_name); @@ -806,6 +806,7 @@ class ProcessorIntegrationTest extends FacetsTestBase { $this->assertSession()->pageTextNotContains('Boolean item label'); $this->assertSession()->pageTextNotContains('Transform UID to user name'); $this->assertSession()->pageTextNotContains('Transform entity ID to label'); + $this->assertSession()->pageTextNotContains('Sort by taxonomy term weight'); } /** @@ -829,7 +830,12 @@ class ProcessorIntegrationTest extends FacetsTestBase { } // These processors are hidden by default, see also // ::testHiddenProcessors. - if (in_array($processor->getPluginId(), ['boolean_item', 'translate_entity', 'uid_to_username_callback'])) { + $hiddenProcessors = [ + 'boolean_item', + 'translate_entity', + 'uid_to_username_callback', + ]; + if (in_array($processor->getPluginId(), $hiddenProcessors)) { continue; } -- GitLab