diff --git a/facetapi.services.yml b/facetapi.services.yml index 4ed28e4d4173c4f40955e2e2cce48c63d8160f7d..a55282f9062964041c6e91cc0d5cf6890cfea7c4 100644 --- a/facetapi.services.yml +++ b/facetapi.services.yml @@ -5,9 +5,6 @@ services: plugin.manager.facetapi.query_type: class: Drupal\facetapi\QueryType\QueryTypePluginManager parent: default_plugin_manager - plugin.manager.facetapi.url_processor: - class: Drupal\facetapi\UrlProcessor\UrlProcessorPluginManager - parent: default_plugin_manager plugin.manager.facetapi.widget: class: Drupal\facetapi\Widget\WidgetPluginManager parent: default_plugin_manager diff --git a/src/FacetManager/FacetManagerPluginBase.php b/src/FacetManager/FacetManagerPluginBase.php index 4c67e3b64a137f7b99cb1591b914a8e63dda0547..41e1f682781838b07464985a2c5361cdbe3ce96b 100644 --- a/src/FacetManager/FacetManagerPluginBase.php +++ b/src/FacetManager/FacetManagerPluginBase.php @@ -14,15 +14,14 @@ use Drupal\facetapi\FacetApiException; use Drupal\facetapi\FacetInterface; use Drupal\facetapi\FacetSource\FacetSourcePluginManager; use Drupal\facetapi\Processor\BuildProcessorInterface; +use Drupal\facetapi\Processor\PreQueryProcessorInterface; use Drupal\facetapi\Processor\ProcessorInterface; use Drupal\facetapi\Processor\ProcessorPluginManager; +use Drupal\facetapi\Processor\UrlProcessorInterface; use Drupal\facetapi\QueryType\QueryTypePluginManager; use Drupal\facetapi\Result\Result; -use Drupal\facetapi\UrlProcessor\UrlProcessorInterface; -use Drupal\facetapi\UrlProcessor\UrlProcessorPluginManager; use Drupal\facetapi\Widget\WidgetPluginManager; use Symfony\Component\DependencyInjection\ContainerInterface; -use Drupal\Component\Plugin\PluginManagerInterface; use \Drupal\facetapi\Entity\Facet; /** @@ -43,13 +42,6 @@ abstract class FacetManagerPluginBase extends PluginBase implements FacetManager */ protected $query_type_plugin_manager; - /** - * The url processor plugin manager. - * - * @var UrlProcessorPluginManager - */ - protected $url_processor_plugin_manager; - /** * The facet source plugin manager. * @@ -149,10 +141,6 @@ abstract class FacetManagerPluginBase extends PluginBase implements FacetManager /** @var \Drupal\facetapi\QueryType\QueryTypePluginManager $query_type_plugin_manager */ $query_type_plugin_manager = $container->get('plugin.manager.facetapi.query_type'); - // Insert the plugin manager for url processors. - /** @var UrlProcessorPluginManager $url_processor_plugin_manager */ - $url_processor_plugin_manager = $container->get('plugin.manager.facetapi.url_processor'); - /** @var \Drupal\facetapi\Widget\WidgetPluginManager $widget_plugin_manager */ $widget_plugin_manager = $container->get('plugin.manager.facetapi.widget'); @@ -162,7 +150,7 @@ abstract class FacetManagerPluginBase extends PluginBase implements FacetManager /** @var \Drupal\facetapi\Processor\ProcessorPluginManager $processor_plugin_manager */ $processor_plugin_manager = $container->get('plugin.manager.facetapi.processor'); - return new static($configuration, $plugin_id, $plugin_definition, $module_handler, $query_type_plugin_manager, $url_processor_plugin_manager, $widget_plugin_manager, $facet_source_plugin_manager, $processor_plugin_manager); + return new static($configuration, $plugin_id, $plugin_definition, $module_handler, $query_type_plugin_manager, $widget_plugin_manager, $facet_source_plugin_manager, $processor_plugin_manager); } /** @@ -188,7 +176,6 @@ abstract class FacetManagerPluginBase extends PluginBase implements FacetManager $plugin_id, $plugin_definition, ModuleHandlerInterface $module_handler, QueryTypePluginManager $query_type_plugin_manager, - UrlProcessorPluginManager $url_processor_plugin_manager, WidgetPluginManager $widget_plugin_manager, FacetSourcePluginManager $facet_source_manager, ProcessorPluginManager $processor_plugin_manager @@ -197,7 +184,6 @@ abstract class FacetManagerPluginBase extends PluginBase implements FacetManager parent::__construct($configuration, $plugin_id, $plugin_definition); $this->module_handler = $module_handler; $this->query_type_plugin_manager = $query_type_plugin_manager; - $this->url_processor_plugin_manager = $url_processor_plugin_manager; $this->widget_plugin_manager = $widget_plugin_manager; $this->facet_source_manager = $facet_source_manager; $this->processor_plugin_manager = $processor_plugin_manager; @@ -346,9 +332,6 @@ abstract class FacetManagerPluginBase extends PluginBase implements FacetManager // First add the results to the facets. $this->updateResults(); - // Then update the urls - $this->updateResultUrls(); - // Set the facets to be processed. $this->processed = TRUE; } @@ -357,17 +340,25 @@ abstract class FacetManagerPluginBase extends PluginBase implements FacetManager /** * Initialize enabled facets. * - * In this method the url processor is used - * to check for each facet what the active items are. + * In this method all pre-query processors get called and their contents are + * executed. */ protected function initFacets() { if (empty($this->facets)) { $this->facets = $this->getEnabledFacets(); foreach ($this->facets as $facet) { - /** @var UrlProcessorInterface $url_processor */ - $url_processor_name = $facet->getUrlProcessorName(); - $url_processor = $this->url_processor_plugin_manager->createInstance($url_processor_name); - $url_processor->processFacet($facet); + + foreach ($facet->getProcessorConfigs() as $processor_configuration) { + $processor_definition = $this->processor_plugin_manager->getDefinition($processor_configuration['processor_id']); + if (is_array($processor_definition['stages']) && array_key_exists(ProcessorInterface::STAGE_PRE_QUERY, $processor_definition['stages'])) { + /** @var PreQueryProcessorInterface $pre_query_processor */ + $pre_query_processor = $this->processor_plugin_manager->createInstance($processor_configuration['processor_id']); + if (!$pre_query_processor instanceof PreQueryProcessorInterface) { + throw new FacetApiException($this->t("The processor @processor has a pre_query definition but doesn't implement the required PreQueryProcessorInterface interface", ['@processor' => $processor_configuration['processor_id']])); + } + $pre_query_processor->preQuery($facet); + } + } } } } @@ -427,14 +418,4 @@ abstract class FacetManagerPluginBase extends PluginBase implements FacetManager } abstract public function updateResults(); - - protected function updateResultUrls() { - // Create the urls for the facets using the url processor. - foreach ($this->facets as $facet) { - /** @var UrlProcessorInterface $url_processor */ - $url_processor_name = $facet->getUrlProcessorName(); - $url_processor = $this->url_processor_plugin_manager->createInstance($url_processor_name); - $url_processor->addUriToResults($facet); - } - } } diff --git a/src/Form/FacetForm.php b/src/Form/FacetForm.php index f44322c90bf4440c67eed32a9092516841ed05bc..0b5957a2242b05f3575a0caba96a21da9d29d3f0 100644 --- a/src/Form/FacetForm.php +++ b/src/Form/FacetForm.php @@ -285,7 +285,7 @@ class FacetForm extends EntityForm { $form['processor_configs'][$id]['processor_id'] = [ '#title' => 'id', '#type' => 'hidden', - '#value' => 'count_widget_order' + '#value' => $id ]; $form['processor_configs'][$id]['settings'] = [ diff --git a/src/Plugin/facetapi/facet_manager/DefaultFacetManager.php b/src/Plugin/facetapi/facet_manager/DefaultFacetManager.php index 03a4ce8a5e175c0867d011f0d736e9c89f2050f5..938146fc4c5946a1c224d7b3fc1a570eb246bbd6 100644 --- a/src/Plugin/facetapi/facet_manager/DefaultFacetManager.php +++ b/src/Plugin/facetapi/facet_manager/DefaultFacetManager.php @@ -50,10 +50,6 @@ class DefaultFacetManager extends FacetManagerPluginBase { /** @var \Drupal\search_api\Query\ResultsCacheInterface $results_cache */ $results_cache = $container->get('search_api.results_static_cache'); - // Insert the plugin manager for url processors. - /** @var UrlProcessorPluginManager $url_processor_plugin_manager */ - $url_processor_plugin_manager = $container->get('plugin.manager.facetapi.url_processor'); - /** @var \Drupal\facetapi\Widget\WidgetPluginManager $widget_plugin_manager */ $widget_plugin_manager = $container->get('plugin.manager.facetapi.widget'); @@ -63,7 +59,7 @@ class DefaultFacetManager extends FacetManagerPluginBase { /** @var \Drupal\facetapi\Processor\ProcessorPluginManager $processor_plugin_manager */ $processor_plugin_manager = $container->get('plugin.manager.facetapi.processor'); - return new static($configuration, $plugin_id, $plugin_definition, $module_handler, $query_type_plugin_manager, $results_cache, $url_processor_plugin_manager, $widget_plugin_manager, $facet_plugin_manager, $processor_plugin_manager); + return new static($configuration, $plugin_id, $plugin_definition, $module_handler, $query_type_plugin_manager, $results_cache, $widget_plugin_manager, $facet_plugin_manager, $processor_plugin_manager); } public function __construct( @@ -72,13 +68,12 @@ class DefaultFacetManager extends FacetManagerPluginBase { ModuleHandlerInterface $module_handler, QueryTypePluginManager $query_type_plugin_manager, ResultsCacheInterface $results_cache, - UrlProcessorPluginManager $url_processor_plugin_manager, WidgetPluginManager $widget_plugin_manager, FacetSourcePluginManager $facet_source_manager, ProcessorPluginManager $processor_plugin_manager ) { - parent::__construct($configuration, $plugin_id, $plugin_definition, $module_handler, $query_type_plugin_manager, $url_processor_plugin_manager, $widget_plugin_manager, $facet_source_manager, $processor_plugin_manager); + parent::__construct($configuration, $plugin_id, $plugin_definition, $module_handler, $query_type_plugin_manager, $widget_plugin_manager, $facet_source_manager, $processor_plugin_manager); $this->searchResultsCache = $results_cache; } diff --git a/src/Plugin/facetapi/url_processor/UrlProcessorQueryString.php b/src/Plugin/facetapi/processor/QueryStringUrlProcessor.php similarity index 66% rename from src/Plugin/facetapi/url_processor/UrlProcessorQueryString.php rename to src/Plugin/facetapi/processor/QueryStringUrlProcessor.php index 80108af86f241162b5b969c72d552c2c5cb98418..17d4accc7576a7efaa429d054e53778890d62b25 100644 --- a/src/Plugin/facetapi/url_processor/UrlProcessorQueryString.php +++ b/src/Plugin/facetapi/processor/QueryStringUrlProcessor.php @@ -5,61 +5,68 @@ * Contains Drupal\facetapi\Plugin\facetapi\url_processor\UrlProcessorQueryString */ -namespace Drupal\facetapi\Plugin\facetapi\url_processor; +namespace Drupal\facetapi\Plugin\facetapi\processor; use Drupal\Core\Url; use Drupal\facetapi\FacetInterface; -use Drupal\facetapi\UrlProcessor\UrlProcessorPluginBase; +use Drupal\facetapi\Processor\UrlProcessorPluginBase; +use Drupal\facetapi\Result\Result; use Symfony\Component\HttpFoundation\Request; /** - * @FacetApiUrlProcessor( + * @FacetApiProcessor( * id = "query_string", * label = @Translation("Query string url processor"), * description = @Translation("Most simple url processor which uses the query sting."), + * stages = { + * "pre_query" = 50, + * "build" = 15, + * } * ) - * - * Class UrlProcessorQueryString - * @package Drupal\facetapi\Plugin\facetapi\url_processor */ -class UrlProcessorQueryString extends UrlProcessorPluginBase{ +class QueryStringUrlProcessor extends UrlProcessorPluginBase { + /** + * A string that separates the filters in the query string + */ const SEPARATOR = ':'; - protected $active_filters = array(); - - public function __construct( - array $configuration, - $plugin_id, - $plugin_definition, - Request $request - ) { - parent::__construct($configuration, $plugin_id, $plugin_definition, - $request); + /** + * @var array + * An array containing the active filters + */ + protected $active_filters = []; + /** + * {@inheritdoc} + */ + public function __construct(array $configuration, $plugin_id, $plugin_definition, Request $request) { + parent::__construct($configuration, $plugin_id, $plugin_definition, $request); $this->initializeActiveFilters(); } - - public function addUriToResults(FacetInterface $facet) { + /** + * {@inheritdoc} + */ + public function build(FacetInterface $facet, array $results) { // Create links for all the values. // First get the current list of get parameters. $get_params = $this->request->query; - $results = $facet->getResults(); - - // No results are found for this facet, so dont try to create urls. + // No results are found for this facet, so don't try to create urls. if (is_null($results)) { - return; + return []; } - foreach ($results as $result) { + $return_results = []; + + /** @var Result $result */ + foreach ($results as &$result) { $filter_string = $facet->getFieldAlias() . ':' . $result->getValue(); $result_get_params = clone $get_params; $filter_params = $result_get_params->get($this->filter_key, [], TRUE); - // If the value is active, remove the filter string from the - // parameters. + // If the value is active, remove the filter string from the parameters. if ($result->isActive()) { foreach ($filter_params as $key => $filter_param) { if ($filter_param == $filter_string) { @@ -79,12 +86,18 @@ class UrlProcessorQueryString extends UrlProcessorPluginBase{ } $url = Url::createFromRequest($request); $url->setOption('query', $result_get_params->all()); + + $result->setUrl($url); } + return $results; } - public function processFacet(FacetInterface $facet) { + /** + * {@inheritdoc} + */ + public function preQuery(FacetInterface $facet) { // Get the filterkey of the facet. if (isset($this->active_filters[$facet->getFieldAlias()])) { foreach ($this->active_filters[$facet->getFieldAlias()] as $value) { @@ -96,11 +109,9 @@ class UrlProcessorQueryString extends UrlProcessorPluginBase{ /** * Initialize the active filters. * - * Get all the filters that are active. - * This method only get's all the filters, - * but doesn't assign them to facets. - * In the processFacet method the active values - * for a specific facet are added to the facet. + * Get all the filters that are active. This method only get's all the + * filters but doesn't assign them to facets. In the processFacet method the + * active values for a specific facet are added to the facet. */ protected function initializeActiveFilters() { $url_parameters = $this->request->query; diff --git a/src/Plugin/facetapi/widget/LinksWidget.php b/src/Plugin/facetapi/widget/LinksWidget.php index dffc7ec51488bbcfc058e1a921c783492342ec82..f33974b3cc329f28bb089dfe309bba90b86a89da 100644 --- a/src/Plugin/facetapi/widget/LinksWidget.php +++ b/src/Plugin/facetapi/widget/LinksWidget.php @@ -32,7 +32,7 @@ class LinksWidget implements WidgetInterface { $build = array(); /** @var Result[] $results */ $results = $facet->getResults(); - if (! empty ($results)) { + if (!empty($results)) { $items = array(); foreach ($results as $result) { if ($result->getCount()) { diff --git a/src/Processor/PreQueryProcessorInterface.php b/src/Processor/PreQueryProcessorInterface.php index 1d8652fb9c2b1d3dee9d9aa1d8c904aa89b786e9..10cf24f197e7d12c9f74398904e10911c26487a3 100644 --- a/src/Processor/PreQueryProcessorInterface.php +++ b/src/Processor/PreQueryProcessorInterface.php @@ -7,8 +7,7 @@ namespace Drupal\facetapi\Processor; -use \Drupal\facetapi\QueryType\QueryTypeInterface; -use \Drupal\search_api\Query\Query; +use Drupal\facetapi\FacetInterface; /** * Processor runs before the query is executed. @@ -22,9 +21,8 @@ interface PreQueryProcessorInterface extends ProcessorInterface { * alteration to the query was added before the query is executed in the * backend? * - * @param \Drupal\facetapi\QueryType\QueryTypeInterface $queryType - * @param \Drupal\search_api\Query\Query $query + * @param \Drupal\facetapi\FacetInterface $queryType */ - public function preQuery(QueryTypeInterface $queryType, Query $query); + public function preQuery(FacetInterface $facet); } diff --git a/src/Processor/UrlProcessorInterface.php b/src/Processor/UrlProcessorInterface.php new file mode 100644 index 0000000000000000000000000000000000000000..d892c50bd630630b0d5ecd5090c7f0f6da665e84 --- /dev/null +++ b/src/Processor/UrlProcessorInterface.php @@ -0,0 +1,29 @@ +filter_key; + } + + /** + * Constructs a new instance of the class. + * + * @param array $configuration + * @param string $plugin_id + * @param mixed $plugin_definition + * @param \Symfony\Component\HttpFoundation\Request $request + */ + public function __construct(array $configuration, $plugin_id, $plugin_definition, Request $request) { + parent::__construct($configuration, $plugin_id, $plugin_definition); + $this->request = $request; + } + + /** + * {@inheritdoc} + */ + public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) { + /** @var Request $request */ + $request = $container->get('request_stack')->getCurrentRequest(); + return new static($configuration, $plugin_id, $plugin_definition, $request); + } +} diff --git a/src/UrlProcessor/UrlProcessorInterface.php b/src/UrlProcessor/UrlProcessorInterface.php deleted file mode 100644 index 2c03384127d8f137b72ec41adedfbc5c46480af8..0000000000000000000000000000000000000000 --- a/src/UrlProcessor/UrlProcessorInterface.php +++ /dev/null @@ -1,52 +0,0 @@ -filter_key; - } - - abstract public function processFacet(FacetInterface $facet); - - public function __construct( - array $configuration, - $plugin_id, - $plugin_definition, - Request $request - ) { - parent::__construct($configuration, $plugin_id, - $plugin_definition); - $this->request = $request; - } - - public static function create( - ContainerInterface $container, - array $configuration, - $plugin_id, - $plugin_definition - ) { - // Add the request. - /** @var Request $request */ - $request = $container->get('request_stack')->getCurrentRequest(); - $plugin = new static($configuration, $plugin_id, $plugin_definition, $request); - return $plugin; - } -} \ No newline at end of file diff --git a/src/UrlProcessor/UrlProcessorPluginManager.php b/src/UrlProcessor/UrlProcessorPluginManager.php deleted file mode 100644 index 17895bef737a83fc10b49b56680da5ffdd0d5387..0000000000000000000000000000000000000000 --- a/src/UrlProcessor/UrlProcessorPluginManager.php +++ /dev/null @@ -1,18 +0,0 @@ -