Skip to content
Snippets Groups Projects
Commit ae38a8e4 authored by Jimmy Henderickx's avatar Jimmy Henderickx Committed by Joris Vercammen
Browse files

Issue #2724171 by StryKaizer, borisson_: Create a new processor stage 'Sort'

parent 86ea1740
No related branches found
No related tags found
No related merge requests found
Showing
with 55 additions and 30 deletions
......@@ -13,7 +13,6 @@ use Drupal\facets\Processor\PostQueryProcessorInterface;
use Drupal\facets\Processor\PreQueryProcessorInterface;
use Drupal\facets\Processor\ProcessorInterface;
use Drupal\facets\Processor\ProcessorPluginManager;
use Drupal\facets\Processor\WidgetOrderProcessorInterface;
use Drupal\facets\QueryType\QueryTypePluginManager;
use Drupal\facets\Widget\WidgetPluginManager;
......@@ -287,31 +286,23 @@ class DefaultFacetManager {
// @see \Drupal\facets\Processor\WidgetOrderProcessorInterface.
$results = $facet->getResults();
$active_sorts = [];
// Load all processors, because getProcessorsByStage does not return the
// correct configuration for the processors.
// @todo: Fix when https://www.drupal.org/node/2722267 is fixed.
$processors = $facet->getProcessors();
foreach ($facet->getProcessorsByStage(ProcessorInterface::STAGE_BUILD) as $processor) {
/** @var \Drupal\facets\Processor\BuildProcessorInterface $build_processor */
$build_processor = $this->processorPluginManager->createInstance($processor->getPluginDefinition()['id'], ['facet' => $facet]);
if ($build_processor instanceof WidgetOrderProcessorInterface) {
// Sorting is handled last and together, to support nested sorts.
$active_sorts[] = $processors[$build_processor->getPluginId()];
}
else {
if (!$build_processor instanceof BuildProcessorInterface) {
throw new InvalidProcessorException("The processor {$processor->getPluginDefinition()['id']} has a build definition but doesn't implement the required BuildProcessorInterface interface");
}
$results = $build_processor->build($facet, $results);
if (!$processor instanceof BuildProcessorInterface) {
throw new InvalidProcessorException("The processor {$processor->getPluginDefinition()['id']} has a build definition but doesn't implement the required BuildProcessorInterface interface");
}
$results = $processor->build($facet, $results);
}
// Trigger sort stage.
$active_sort_processors = [];
foreach ($facet->getProcessorsByStage(ProcessorInterface::STAGE_SORT) as $processor) {
$active_sort_processors[] = $processor;
}
uasort($results, function ($a, $b) use ($active_sorts) {
uasort($results, function ($a, $b) use ($active_sort_processors) {
$return = 0;
foreach ($active_sorts as $sort) {
if ($return = $sort->sortResults($a, $b)) {
if ($sort->getConfiguration()['sort'] == 'DESC') {
foreach ($active_sort_processors as $sort_processor) {
if ($return = $sort_processor->sortResults($a, $b)) {
if ($sort_processor->getConfiguration()['sort'] == 'DESC') {
$return *= -1;
}
break;
......
......@@ -16,7 +16,7 @@ use Drupal\facets\Result\Result;
* description = @Translation("Sorts the widget results by active state."),
* default_enabled = TRUE,
* stages = {
* "build" = 20
* "sort" = 20
* }
* )
*/
......
......@@ -15,7 +15,7 @@ use Drupal\facets\Result\Result;
* description = @Translation("Sorts the widget results by count."),
* default_enabled = TRUE,
* stages = {
* "build" = 30
* "sort" = 30
* }
* )
*/
......
......@@ -18,7 +18,7 @@ use Symfony\Component\DependencyInjection\ContainerInterface;
* description = @Translation("Sorts the widget results by display value."),
* default_enabled = TRUE,
* stages = {
* "build" = 40
* "sort" = 40
* }
* )
*/
......
......@@ -14,7 +14,7 @@ use Drupal\facets\Result\Result;
* label = @Translation("Sort by raw value"),
* description = @Translation("Sorts the widget results by raw value."),
* stages = {
* "build" = 50
* "sort" = 50
* }
* )
*/
......
......@@ -27,6 +27,11 @@ interface ProcessorInterface extends ConfigurablePluginInterface, PluginInspecti
*/
const STAGE_BUILD = 'build';
/**
* Processing stage: sort.
*/
const STAGE_SORT = 'sort';
/**
* Adds a configuration form for this processor.
*
......
......@@ -52,6 +52,9 @@ class ProcessorPluginManager extends DefaultPluginManager {
ProcessorInterface::STAGE_BUILD => array(
'label' => $this->t('Build stage'),
),
ProcessorInterface::STAGE_SORT => array(
'label' => $this->t('Sort stage'),
),
);
}
......
<?php
namespace Drupal\facets\Processor;
use Drupal\facets\FacetInterface;
/**
* Processor runs after the build processor for sorting.
*/
interface SortProcessorInterface extends ProcessorInterface {
/**
* Runs after the build processor for sorting.
*
* @param \Drupal\facets\FacetInterface $facet
* The facet being changed.
* @param \Drupal\facets\Result\Result[] $results
* The results being changed.
*
* @return \Drupal\facets\Result\Result[] $results
* The changed results.
*/
public function sort(FacetInterface $facet, array $results);
}
......@@ -33,7 +33,7 @@ abstract class WidgetOrderPluginBase extends ProcessorPluginBase implements Widg
/**
* {@inheritdoc}
*/
public function build(FacetInterface $facet, array $results) {
public function sort(FacetInterface $facet, array $results) {
$processors = $facet->getProcessors();
$config = $processors[$this->getPluginId()];
......
......@@ -6,7 +6,7 @@ use Drupal\facets\Result\Result;
/**
* Processor runs before the renderable array is created.
*/
interface WidgetOrderProcessorInterface extends BuildProcessorInterface {
interface WidgetOrderProcessorInterface extends SortProcessorInterface {
/**
* Orders results and return the new order of results.
......
......@@ -165,9 +165,9 @@ class ProcessorIntegrationTest extends WebTestBase {
// Sort by count, then by display value.
$values['facet_sorting[count_widget_order][status]'] = TRUE;
$values['facet_sorting[count_widget_order][settings][sort]'] = 'ASC';
$values['processors[count_widget_order][weights][build]'] = 1;
$values['processors[count_widget_order][weights][sort]'] = 1;
$values['facet_sorting[display_value_widget_order][status]'] = TRUE;
$values['processors[display_value_widget_order][weights][build]'] = 2;
$values['processors[display_value_widget_order][weights][sort]'] = 2;
$this->disableAllFacetSorts();
$this->drupalPostForm($this->editForm, $values, $this->t('Save'));
......
......@@ -123,6 +123,7 @@ class ProcessorPluginManagerTest extends UnitTestCase {
ProcessorInterface::STAGE_PRE_QUERY,
ProcessorInterface::STAGE_POST_QUERY,
ProcessorInterface::STAGE_BUILD,
ProcessorInterface::STAGE_SORT,
];
$this->assertEquals($stages, array_keys($sut->getProcessingStages()));
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment