Commit 637550f4 authored by borisson_'s avatar borisson_ Committed by borisson_

Issue #2864336 by borisson_, sun-fire: Facet sorting for hierarchical vocabularies

parent 9868f953
......@@ -330,18 +330,11 @@ class DefaultFacetManager {
foreach ($facet->getProcessorsByStage(ProcessorInterface::STAGE_SORT) as $processor) {
$active_sort_processors[] = $processor;
}
uasort($results, function ($a, $b) use ($active_sort_processors) {
$return = 0;
foreach ($active_sort_processors as $sort_processor) {
if ($return = $sort_processor->sortResults($a, $b)) {
if ($sort_processor->getConfiguration()['sort'] == 'DESC') {
$return *= -1;
}
break;
}
}
return $return;
});
// Sort the actual results if we have enabled sort processors.
if (!empty($active_sort_processors)) {
$results = $this->sortFacetResults($active_sort_processors, $results);
}
$facet->setResults($results);
......@@ -443,4 +436,42 @@ class DefaultFacetManager {
return $keyed_results;
}
/**
* Sort the facet results, and recurse to children to do the same.
*
* @param \Drupal\facets\Processor\SortProcessorInterface[] $active_sort_processors
* An array of sort processors.
* @param \Drupal\facets\Result\ResultInterface[] $results
* An array of results.
*
* @return \Drupal\facets\Result\ResultInterface[]
* A sorted array of results.
*/
protected function sortFacetResults(array $active_sort_processors, $results) {
uasort($results, function ($a, $b) use ($active_sort_processors) {
$return = 0;
foreach ($active_sort_processors as $sort_processor) {
if ($return = $sort_processor->sortResults($a, $b)) {
if ($sort_processor->getConfiguration()['sort'] == 'DESC') {
$return *= -1;
}
break;
}
}
return $return;
});
// Loop over the results and see if they have any children, if they do, fire
// a request to this same method again with the children.
foreach ($results as &$result) {
if (!empty($result->getChildren())) {
$children = $this->sortFacetResults($active_sort_processors, $result->getChildren());
$result->setChildren($children);
}
}
// Return the sorted results.
return $results;
}
}
......@@ -170,6 +170,38 @@ class HierarchicalFacetIntegrationTest extends FacetsTestBase {
$this->assertFacetLabel('Child 4');
}
/**
* Tests sorting of hierarchy.
*/
public function testHierarchySorting() {
// Expand the hierarchy and verify that all items are visible initially.
$edit = [
'facet_settings[expand_hierarchy]' => '1',
'facet_settings[use_hierarchy]' => '1',
'facet_settings[translate_entity][status]' => '1',
'facet_sorting[count_widget_order][status]' => '0',
'facet_sorting[active_widget_order][status]' => '0',
];
$this->drupalPostForm($this->facetEditPage, $edit, 'Save');
$this->drupalGet('search-api-test-fulltext');
$this->assertStringPosition('Parent 1', 'Parent 2');
$this->assertStringPosition('Child 1', 'Child 2');
$this->assertStringPosition('Child 2', 'Child 3');
$this->assertStringPosition('Child 3', 'Child 4');
$edit = [
'facet_sorting[display_value_widget_order][settings][sort]' => 'DESC',
];
$this->drupalPostForm($this->facetEditPage, $edit, 'Save');
$this->drupalGet('search-api-test-fulltext');
$this->assertStringPosition('Parent 2', 'Parent 1');
$this->assertStringPosition('Child 4', 'Child 3');
$this->assertStringPosition('Child 3', 'Child 2');
$this->assertStringPosition('Child 2', 'Child 1');
}
/**
* Verify the "Enable parent when child gets disabled" option is working.
*/
......
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