diff --git a/src/Annotation/FacetsProcessor.php b/src/Annotation/FacetsProcessor.php index fc8125e..7228926 100644 --- a/src/Annotation/FacetsProcessor.php +++ b/src/Annotation/FacetsProcessor.php @@ -60,4 +60,11 @@ class FacetsProcessor extends Plugin { */ public $stages; + /** + * Whether or not this processor is default enabled for new facets. + * + * @var bool + */ + public $default_enabled; + } diff --git a/src/Form/FacetSettingsForm.php b/src/Form/FacetSettingsForm.php index 7560c26..5ef811d 100644 --- a/src/Form/FacetSettingsForm.php +++ b/src/Form/FacetSettingsForm.php @@ -277,7 +277,9 @@ class FacetSettingsForm extends EntityForm { $processors_definitions = $this->getProcessorPluginManager()->getDefinitions(); foreach ($processors_definitions as $processor_id => $processor) { - if (isset($processor['locked']) && $processor['locked'] == TRUE) { + $is_locked = isset($processor['locked']) && $processor['locked'] == TRUE; + $is_default_enabled = isset($processor['default_enabled']) && $processor['default_enabled'] == TRUE; + if ($is_locked || $is_default_enabled) { $weights = []; foreach ($stages as $stage_id => $stage) { if (isset($processor['stages'][$stage_id])) { diff --git a/src/Plugin/facets/processor/ActiveWidgetOrderProcessor.php b/src/Plugin/facets/processor/ActiveWidgetOrderProcessor.php index 750434d..2527ad1 100644 --- a/src/Plugin/facets/processor/ActiveWidgetOrderProcessor.php +++ b/src/Plugin/facets/processor/ActiveWidgetOrderProcessor.php @@ -14,8 +14,9 @@ use Drupal\facets\Result\Result; * id = "active_widget_order", * label = @Translation("Sort by active state"), * description = @Translation("Sorts the widget results by active state."), + * default_enabled = TRUE, * stages = { - * "build" = 50 + * "build" = 20 * } * ) */ @@ -31,4 +32,11 @@ class ActiveWidgetOrderProcessor extends WidgetOrderPluginBase implements Widget return ($a->isActive()) ? -1 : 1; } + /** + * {@inheritdoc} + */ + public function defaultConfiguration() { + return ['sort' => 'DESC']; + } + } diff --git a/src/Plugin/facets/processor/CountWidgetOrderProcessor.php b/src/Plugin/facets/processor/CountWidgetOrderProcessor.php index 1bface7..0b1f637 100644 --- a/src/Plugin/facets/processor/CountWidgetOrderProcessor.php +++ b/src/Plugin/facets/processor/CountWidgetOrderProcessor.php @@ -13,8 +13,9 @@ use Drupal\facets\Result\Result; * id = "count_widget_order", * label = @Translation("Sort by count"), * description = @Translation("Sorts the widget results by count."), + * default_enabled = TRUE, * stages = { - * "build" = 50 + * "build" = 30 * } * ) */ @@ -30,4 +31,11 @@ class CountWidgetOrderProcessor extends WidgetOrderPluginBase implements WidgetO return ($a->getCount() < $b->getCount()) ? -1 : 1; } + /** + * {@inheritdoc} + */ + public function defaultConfiguration() { + return ['sort' => 'DESC']; + } + } diff --git a/src/Plugin/facets/processor/DisplayValueWidgetOrderProcessor.php b/src/Plugin/facets/processor/DisplayValueWidgetOrderProcessor.php index 7f5be4f..cbcbde0 100644 --- a/src/Plugin/facets/processor/DisplayValueWidgetOrderProcessor.php +++ b/src/Plugin/facets/processor/DisplayValueWidgetOrderProcessor.php @@ -16,8 +16,9 @@ use Symfony\Component\DependencyInjection\ContainerInterface; * id = "display_value_widget_order", * label = @Translation("Sort by display value"), * description = @Translation("Sorts the widget results by display value."), + * default_enabled = TRUE, * stages = { - * "build" = 50 + * "build" = 40 * } * ) */ diff --git a/src/Tests/ProcessorIntegrationTest.php b/src/Tests/ProcessorIntegrationTest.php index def41cf..863c322 100644 --- a/src/Tests/ProcessorIntegrationTest.php +++ b/src/Tests/ProcessorIntegrationTest.php @@ -163,10 +163,12 @@ 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['facet_sorting[display_value_widget_order][status]'] = TRUE; $values['processors[display_value_widget_order][weights][build]'] = 2; $this->drupalGet('admin/config/search/facets/' . $id . '/edit'); + $this->disableAllFacetSorts('admin/config/search/facets/' . $id . '/edit'); $this->drupalPostForm(NULL, $values, $this->t('Save')); $expected_results = [ @@ -188,6 +190,7 @@ class ProcessorIntegrationTest extends WebTestBase { $values['facet_sorting[display_value_widget_order][status]'] = TRUE; $values['facet_sorting[count_widget_order][status]'] = TRUE; + $values['facet_sorting[count_widget_order][settings][sort]'] = 'ASC'; $this->drupalGet('admin/config/search/facets/' . $id . '/edit'); $this->drupalPostForm(NULL, $values, $this->t('Save')); $this->assertFieldChecked( @@ -363,8 +366,10 @@ class ProcessorIntegrationTest extends WebTestBase { * Tests the active widget order. */ protected function checkSortByActive() { + $this->disableAllFacetSorts(); $form = [ 'facet_sorting[active_widget_order][status]' => TRUE, + 'facet_sorting[active_widget_order][settings][sort]' => 'ASC', ]; $this->drupalPostForm($this->editForm, $form, $this->t('Save')); @@ -392,9 +397,11 @@ class ProcessorIntegrationTest extends WebTestBase { * Tests the active widget order. */ protected function checkSortByCount() { + $this->disableAllFacetSorts(); $form = [ 'widget_configs[show_numbers]' => TRUE, 'facet_sorting[count_widget_order][status]' => TRUE, + 'facet_sorting[count_widget_order][settings][sort]' => 'ASC', ]; $this->drupalPostForm($this->editForm, $form, $this->t('Save')); @@ -425,6 +432,7 @@ class ProcessorIntegrationTest extends WebTestBase { * Tests the display order. */ protected function checkSortByDisplay() { + $this->disableAllFacetSorts(); $form = ['facet_sorting[display_value_widget_order][status]' => TRUE]; $this->drupalPostForm($this->editForm, $form, $this->t('Save')); @@ -450,6 +458,7 @@ class ProcessorIntegrationTest extends WebTestBase { * Tests the display order. */ protected function checkSortByRaw() { + $this->disableAllFacetSorts(); $form = [ 'facet_sorting[raw_value_widget_order][status]' => TRUE, ]; @@ -490,4 +499,21 @@ class ProcessorIntegrationTest extends WebTestBase { $this->blocks[$id] = $this->drupalPlaceBlock($plugin_id, $settings); } -} + /** + * Disables all sorting processors for a clean testing base. + */ + protected function disableAllFacetSorts($path = FALSE) { + $settings = [ + 'facet_sorting[raw_value_widget_order][status]' => FALSE, + 'facet_sorting[display_value_widget_order][status]' => FALSE, + 'facet_sorting[count_widget_order][status]' => FALSE, + 'facet_sorting[active_widget_order][status]' => FALSE, + + ]; + if(!$path){ + $path = $this->editForm; + } + $this->drupalPostForm($path, $settings, $this->t('Save')); + } + +} \ No newline at end of file diff --git a/tests/src/Unit/Plugin/processor/ActiveWidgetOrderProcessorTest.php b/tests/src/Unit/Plugin/processor/ActiveWidgetOrderProcessorTest.php index 21aaff3..e2c4ac6 100644 --- a/tests/src/Unit/Plugin/processor/ActiveWidgetOrderProcessorTest.php +++ b/tests/src/Unit/Plugin/processor/ActiveWidgetOrderProcessorTest.php @@ -76,7 +76,7 @@ class ActiveWidgetOrderProcessorTest extends UnitTestCase { */ public function testDefaultConfiguration() { $config = $this->processor->defaultConfiguration(); - $this->assertEquals(['sort' => 'ASC'], $config); + $this->assertEquals(['sort' => 'DESC'], $config); } } diff --git a/tests/src/Unit/Plugin/processor/CountWidgetOrderProcessorTest.php b/tests/src/Unit/Plugin/processor/CountWidgetOrderProcessorTest.php index 91427a1..3ec0529 100644 --- a/tests/src/Unit/Plugin/processor/CountWidgetOrderProcessorTest.php +++ b/tests/src/Unit/Plugin/processor/CountWidgetOrderProcessorTest.php @@ -61,7 +61,7 @@ class CountWidgetOrderProcessorTest extends UnitTestCase { */ public function testDefaultConfiguration() { $config = $this->processor->defaultConfiguration(); - $this->assertEquals(['sort' => 'ASC'], $config); + $this->assertEquals(['sort' => 'DESC'], $config); } }