diff --git a/config/schema/facets.facet.schema.yml b/config/schema/facets.facet.schema.yml index 2a5ea04..bf7bb17 100644 --- a/config/schema/facets.facet.schema.yml +++ b/config/schema/facets.facet.schema.yml @@ -84,12 +84,6 @@ facets.facet.*: sequence: type: plugin.plugin_configuration.facets_facet_options.[%key] label: 'Facet plugin options' - sort_processor_configs: - type: sequence - label: 'Facet sort processors' - sequence: - type: plugin.plugin_configuration.facets_sort_processor.[%key] - label: 'Facet plugin options' condition.plugin.other_facet: type: condition.plugin diff --git a/config/schema/facets.processor.schema.yml b/config/schema/facets.processor.schema.yml index a651391..37c18fa 100644 --- a/config/schema/facets.processor.schema.yml +++ b/config/schema/facets.processor.schema.yml @@ -28,10 +28,17 @@ plugin.plugin_configuration.facets_processor.count_limit: type: integer label: 'Maximum amount of items to show.' -plugin.plugin_configuration.facets_processor.url_processor_handler: - type: config_object - plugin.plugin_configuration.facets_processor.sort_processor_handler: + type: mapping + label: settings + settings: + type: sequence + label: 'Facet sort processor configuration.' + sequence: + type: plugin.plugin_configuration.facets_sort_processor.[%key] + label: 'Facet plugin options' + +plugin.plugin_configuration.facets_processor.url_processor_handler: type: config_object plugin.plugin_configuration.facets_processor.hide_non_narrowing_result_processor: diff --git a/config/schema/facets.sort_processor.schema.yml b/config/schema/facets.sort_processor.schema.yml index cb6a811..b0d5ff9 100644 --- a/config/schema/facets.sort_processor.schema.yml +++ b/config/schema/facets.sort_processor.schema.yml @@ -1,4 +1,3 @@ - plugin.plugin_configuration.facets_sort_processor.raw_value_widget_order: type: mapping label: 'Raw value widget order' diff --git a/src/Form/FacetDisplayForm.php b/src/Form/FacetDisplayForm.php index 1f5cd03..83b8e17 100644 --- a/src/Form/FacetDisplayForm.php +++ b/src/Form/FacetDisplayForm.php @@ -7,18 +7,19 @@ namespace Drupal\facets\Form; -use Drupal\Core\Config\Config; use Drupal\Component\Utility\Html; +use Drupal\Core\Config\Config; use Drupal\Core\Entity\EntityForm; -use Drupal\Core\Entity\EntityTypeManager; +use Drupal\Core\Entity\EntityTypeManagerInterface; use Drupal\Core\Form\FormStateInterface; use Drupal\facets\Processor\ProcessorInterface; use Drupal\facets\Processor\ProcessorPluginManager; use Drupal\facets\SortProcessor\SortProcessorInterface; use Drupal\facets\SortProcessor\SortProcessorPluginManager; use Drupal\facets\UrlProcessor\UrlProcessorInterface; -use Symfony\Component\DependencyInjection\ContainerInterface; use Drupal\facets\Widget\WidgetPluginManager; +use Symfony\Component\DependencyInjection\ContainerInterface; + /** * Provides a form for configuring the processors of a facet. @@ -63,7 +64,7 @@ class FacetDisplayForm extends EntityForm { /** * Constructs an FacetDisplayForm object. * - * @param \Drupal\Core\Entity\EntityTypeManager $entity_type_manager + * @param \Drupal\Core\Entity\EntityTypeManagerInterface $entity_type_manager * The entity manager. * @param \Drupal\facets\Processor\ProcessorPluginManager $processor_plugin_manager * The processor plugin manager. @@ -72,7 +73,7 @@ class FacetDisplayForm extends EntityForm { * @param \Drupal\facets\SortProcessor\SortProcessorPluginManager $sort_processor_plugin_manager * The sort processor plugin manager. */ - public function __construct(EntityTypeManager $entity_type_manager, ProcessorPluginManager $processor_plugin_manager, WidgetPluginManager $widget_plugin_manager, SortProcessorPluginManager $sort_processor_plugin_manager) { + public function __construct(EntityTypeManagerInterface $entity_type_manager, ProcessorPluginManager $processor_plugin_manager, WidgetPluginManager $widget_plugin_manager, SortProcessorPluginManager $sort_processor_plugin_manager) { $this->entityTypeManager = $entity_type_manager; $this->processorPluginManager = $processor_plugin_manager; $this->widgetPluginManager = $widget_plugin_manager; @@ -83,7 +84,7 @@ class FacetDisplayForm extends EntityForm { * {@inheritdoc} */ public static function create(ContainerInterface $container) { - /** @var \Drupal\Core\Entity\EntityTypeManager $entity_type_manager */ + /** @var \Drupal\Core\Entity\EntityTypeManagerInterface $entity_type_manager */ $entity_type_manager = $container->get('entity_type.manager'); /** @var \Drupal\facets\Processor\ProcessorPluginManager $processor_plugin_manager */ @@ -95,12 +96,7 @@ class FacetDisplayForm extends EntityForm { /** @var \Drupal\facets\Widget\WidgetPluginManager $widget_plugin_manager */ $widget_plugin_manager = $container->get('plugin.manager.facets.widget'); - return new static( - $entity_type_manager, - $processor_plugin_manager, - $widget_plugin_manager, - $sort_processor_plugin_manager - ); + return new static($entity_type_manager, $processor_plugin_manager, $widget_plugin_manager, $sort_processor_plugin_manager); } /** @@ -136,7 +132,8 @@ class FacetDisplayForm extends EntityForm { // @todo Create, use and save SubFormState already here, not only in // validate(). Also, use proper subset of $form for first parameter? $config = $this->config('facets.facet.' . $this->entity->id()); - if ($config_form = $widget_instance->buildConfigurationForm([], $form_state, ($config instanceof Config) ? $config : NULL)) { + $config = ($config instanceof Config) ? $config : NULL; + if ($config_form = $widget_instance->buildConfigurationForm([], $form_state, $config)) { $form['widget_configs']['#type'] = 'fieldset'; $form['widget_configs']['#title'] = $this->t('%widget settings', ['%widget' => $this->getWidgetPluginManager()->getDefinition($widget)['label']]); @@ -294,14 +291,16 @@ class FacetDisplayForm extends EntityForm { ), ); + $sorting_settings = $all_processors['sort_processor_handler']->getConfiguration(); foreach ($this->sortProcessorPluginManager->getDefinitions() as $processor_id => $definition) { $processor = $this->sortProcessorPluginManager->createInstance($processor_id); $clean_css_id = Html::cleanCssIdentifier($processor_id); + $is_checked = !empty($sorting_settings[$processor_id]) && $sorting_settings[$processor_id]['status']; $form['facet_sorting'][$processor_id]['status'] = array( '#type' => 'checkbox', '#title' => (string) $processor->getPluginDefinition()['label'], - '#default_value' => $processor->isLocked() || !empty($enabled_processors[$processor_id]), + '#default_value' => $processor->isLocked() || $is_checked, '#description' => $processor->getDescription(), '#attributes' => array( 'class' => array( @@ -313,10 +312,7 @@ class FacetDisplayForm extends EntityForm { '#access' => !$processor->isHidden(), ); - $processor_form_state = new SubFormState( - $form_state, - array('facet_sorting', $processor_id, 'settings') - ); + $processor_form_state = new SubFormState($form_state, ['facet_sorting', $processor_id, 'settings']); $processor_form = $processor->buildConfigurationForm($form, $processor_form_state, $facet); if ($processor_form) { $form['facet_sorting'][$processor_id]['settings'] = array( @@ -336,6 +332,11 @@ class FacetDisplayForm extends EntityForm { ), ); $form['facet_sorting'][$processor_id]['settings'] += $processor_form; + + // Make sure that settings are saved as expected. + foreach ($sorting_settings[$processor_id]['settings'] as $k => $value) { + $form['facet_sorting'][$processor_id]['settings'][$k]['#default_value'] = $value; + } } } @@ -514,8 +515,6 @@ class FacetDisplayForm extends EntityForm { $values = $form_state->getValues(); // Store processor settings. - // @todo Go through all available processors, enable/disable with method on - // processor plugin to allow reaction. /** @var \Drupal\facets\FacetInterface $facet */ $facet = $this->entity; @@ -527,11 +526,11 @@ class FacetDisplayForm extends EntityForm { $facet->removeProcessor($processor_id); continue; } - $new_settings = array( + $new_settings = [ 'processor_id' => $processor_id, - 'weights' => array(), - 'settings' => array(), - ); + 'weights' => [], + 'settings' => [], + ]; if (!empty($values['processors'][$processor_id]['weights'])) { $new_settings['weights'] = $values['processors'][$processor_id]['weights']; } @@ -543,6 +542,12 @@ class FacetDisplayForm extends EntityForm { $processor->submitConfigurationForm($form[$form_container_key][$processor_id]['settings'], $processor_form_state, $facet); $new_settings['settings'] = $processor->getConfiguration(); } + + // The sort processor handler should have the settings for all sorting + // facets. + if ($processor_id === 'sort_processor_handler') { + $new_settings['settings'] = $values['facet_sorting']; + } $facet->addProcessor($new_settings); } diff --git a/src/Plugin/facets/processor/SortProcessorHandler.php b/src/Plugin/facets/processor/SortProcessorHandler.php index c2a33f6..c4bcd34 100644 --- a/src/Plugin/facets/processor/SortProcessorHandler.php +++ b/src/Plugin/facets/processor/SortProcessorHandler.php @@ -52,6 +52,20 @@ class SortProcessorHandler extends ProcessorPluginBase implements BuildProcessor * {@inheritdoc} */ public function build(FacetInterface $facet, array $results) { + $processors = $facet->getProcessors(); + $sort_processor_handler = $processors['sort_processor_handler']; + $configs = $sort_processor_handler->getConfiguration(); + + $plugin_manager = \Drupal::getContainer()->get('plugin.manager.facets.sort_processor'); + + foreach ($configs as $id => $sort_config) { + if ($sort_config['status'] == TRUE) { + /** @var \Drupal\facets\SortProcessor\SortProcessorInterface $sort */ + $sort = $plugin_manager->createInstance($id, $sort_config); + $results = $sort->sortResults($results, $sort_config['sort']); + return $results; + } + } return $results; } diff --git a/tests/src/Unit/Plugin/processor/SortProcessorHandlerTest.php b/tests/src/Unit/Plugin/processor/SortProcessorHandlerTest.php index 47fb1f4..9745315 100644 --- a/tests/src/Unit/Plugin/processor/SortProcessorHandlerTest.php +++ b/tests/src/Unit/Plugin/processor/SortProcessorHandlerTest.php @@ -7,7 +7,6 @@ namespace Drupal\Tests\facets\Unit\Plugin\processor; -use Drupal\Core\DependencyInjection\ContainerBuilder; use Drupal\facets\Entity\Facet; use Drupal\facets\Plugin\facets\processor\SortProcessorHandler; use Drupal\Tests\UnitTestCase;