diff --git a/src/FacetManager/DefaultFacetManager.php b/src/FacetManager/DefaultFacetManager.php index a044f6b..41105a1 100644 --- a/src/FacetManager/DefaultFacetManager.php +++ b/src/FacetManager/DefaultFacetManager.php @@ -289,23 +289,21 @@ class DefaultFacetManager { $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 (!$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); + } + + // 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_SORT) as $processor) { + $active_sorts[] = $processors[$processor->getPluginId()]; } uasort($results, function ($a, $b) use ($active_sorts) { $return = 0; diff --git a/src/Plugin/facets/processor/ActiveWidgetOrderProcessor.php b/src/Plugin/facets/processor/ActiveWidgetOrderProcessor.php index 2527ad1..e5bfda8 100644 --- a/src/Plugin/facets/processor/ActiveWidgetOrderProcessor.php +++ b/src/Plugin/facets/processor/ActiveWidgetOrderProcessor.php @@ -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 * } * ) */ diff --git a/src/Plugin/facets/processor/CountWidgetOrderProcessor.php b/src/Plugin/facets/processor/CountWidgetOrderProcessor.php index 0b1f637..d7367ee 100644 --- a/src/Plugin/facets/processor/CountWidgetOrderProcessor.php +++ b/src/Plugin/facets/processor/CountWidgetOrderProcessor.php @@ -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 * } * ) */ diff --git a/src/Plugin/facets/processor/DisplayValueWidgetOrderProcessor.php b/src/Plugin/facets/processor/DisplayValueWidgetOrderProcessor.php index cbcbde0..eb9df8a 100644 --- a/src/Plugin/facets/processor/DisplayValueWidgetOrderProcessor.php +++ b/src/Plugin/facets/processor/DisplayValueWidgetOrderProcessor.php @@ -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 * } * ) */ diff --git a/src/Plugin/facets/processor/RawValueWidgetOrderProcessor.php b/src/Plugin/facets/processor/RawValueWidgetOrderProcessor.php index 1417e93..7b43c67 100644 --- a/src/Plugin/facets/processor/RawValueWidgetOrderProcessor.php +++ b/src/Plugin/facets/processor/RawValueWidgetOrderProcessor.php @@ -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 * } * ) */ diff --git a/src/Processor/ProcessorInterface.php b/src/Processor/ProcessorInterface.php index 2429841..778fb80 100644 --- a/src/Processor/ProcessorInterface.php +++ b/src/Processor/ProcessorInterface.php @@ -28,6 +28,11 @@ interface ProcessorInterface extends ConfigurablePluginInterface, PluginInspecti const STAGE_BUILD = 'build'; /** + * Processing stage: sort. + */ + const STAGE_SORT = 'sort'; + + /** * Adds a configuration form for this processor. * * @param array $form diff --git a/src/Processor/ProcessorPluginManager.php b/src/Processor/ProcessorPluginManager.php index 09a27ce..88bf52b 100644 --- a/src/Processor/ProcessorPluginManager.php +++ b/src/Processor/ProcessorPluginManager.php @@ -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'), + ), ); } diff --git a/src/Processor/SortProcessorInterface.php b/src/Processor/SortProcessorInterface.php new file mode 100644 index 0000000..dfe0bb2 --- /dev/null +++ b/src/Processor/SortProcessorInterface.php @@ -0,0 +1,25 @@ +getProcessors(); $config = $processors[$this->getPluginId()]; diff --git a/src/Processor/WidgetOrderProcessorInterface.php b/src/Processor/WidgetOrderProcessorInterface.php index f1f081f..934afde 100644 --- a/src/Processor/WidgetOrderProcessorInterface.php +++ b/src/Processor/WidgetOrderProcessorInterface.php @@ -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. diff --git a/src/Tests/ProcessorIntegrationTest.php b/src/Tests/ProcessorIntegrationTest.php index 0e59758..14f66d5 100644 --- a/src/Tests/ProcessorIntegrationTest.php +++ b/src/Tests/ProcessorIntegrationTest.php @@ -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')); diff --git a/tests/src/Unit/Processor/ProcessorPluginManagerTest.php b/tests/src/Unit/Processor/ProcessorPluginManagerTest.php index 2165dcb..d65227d 100644 --- a/tests/src/Unit/Processor/ProcessorPluginManagerTest.php +++ b/tests/src/Unit/Processor/ProcessorPluginManagerTest.php @@ -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()));