Commit e1108f03 authored by borisson_'s avatar borisson_ Committed by borisson_

Issue #2909262 by borisson_: Limit taxonomy weight processor to entity reference fields, add a test

parent 3323ecff
......@@ -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;
}
}
......@@ -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.
*/
......
......@@ -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;
}
......
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