From 6123df49a2038b71d3c3129fbcea53981703be4c Mon Sep 17 00:00:00 2001 From: Rosie Le Faive Date: Tue, 26 Sep 2023 12:51:02 -0300 Subject: [PATCH 1/7] Add event subscriber. --- advanced_search.services.yml | 4 +++ .../PostConvertedQueryEventSubscriber.php | 31 +++++++++++++++++++ 2 files changed, 35 insertions(+) create mode 100644 advanced_search.services.yml create mode 100644 src/EventSubscriber/PostConvertedQueryEventSubscriber.php diff --git a/advanced_search.services.yml b/advanced_search.services.yml new file mode 100644 index 0000000..981151a --- /dev/null +++ b/advanced_search.services.yml @@ -0,0 +1,4 @@ +services: + Drupal\advanced_search\EventSubscriber\PostConvertedQueryEventSubscriber: + tags: + - { name: 'event_subscriber' } diff --git a/src/EventSubscriber/PostConvertedQueryEventSubscriber.php b/src/EventSubscriber/PostConvertedQueryEventSubscriber.php new file mode 100644 index 0000000..9f8ed49 --- /dev/null +++ b/src/EventSubscriber/PostConvertedQueryEventSubscriber.php @@ -0,0 +1,31 @@ +getSearchApiQuery(); + $solarium_query = $event->getSolariumQuery(); + $advanced_search_query = new AdvancedSearchQuery(); + $advanced_search_query->alterQuery(\Drupal::request(), $solarium_query, $search_api_query); + } + +} From 4db8602124b1597b7f6b287cc76804be7632babb Mon Sep 17 00:00:00 2001 From: Rosie Le Faive Date: Tue, 26 Sep 2023 13:01:34 -0300 Subject: [PATCH 2/7] PHPCS. --- .../PostConvertedQueryEventSubscriber.php | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/src/EventSubscriber/PostConvertedQueryEventSubscriber.php b/src/EventSubscriber/PostConvertedQueryEventSubscriber.php index 9f8ed49..572c4bd 100644 --- a/src/EventSubscriber/PostConvertedQueryEventSubscriber.php +++ b/src/EventSubscriber/PostConvertedQueryEventSubscriber.php @@ -3,10 +3,15 @@ namespace Drupal\advanced_search\EventSubscriber; use Drupal\advanced_search\AdvancedSearchQuery; -use Drupal\search_api_solr\Event\SearchAPISolrEvents; use Drupal\search_api_solr\Event\PostConvertedQueryEvent; +use Drupal\search_api_solr\Event\SearchAPISolrEvents; -class PostConvertedQueryEventSubscriber extends EventSubscriberInterface() { +/** + * Subscribes to PostConvertedQueryEvents. + * + * @package Drupal\advanced_search\EventSubscriber + */ +class PostConvertedQueryEventSubscriber extends EventSubscriberInterface { /** * {@inheritdoc} From eb63eb8c4ca4771ea4e336bc6b2da49afc8e69bb Mon Sep 17 00:00:00 2001 From: Rosie Le Faive Date: Tue, 26 Sep 2023 13:02:55 -0300 Subject: [PATCH 3/7] Use EventSubscriberInterface. --- src/EventSubscriber/PostConvertedQueryEventSubscriber.php | 1 + 1 file changed, 1 insertion(+) diff --git a/src/EventSubscriber/PostConvertedQueryEventSubscriber.php b/src/EventSubscriber/PostConvertedQueryEventSubscriber.php index 572c4bd..8287507 100644 --- a/src/EventSubscriber/PostConvertedQueryEventSubscriber.php +++ b/src/EventSubscriber/PostConvertedQueryEventSubscriber.php @@ -5,6 +5,7 @@ namespace Drupal\advanced_search\EventSubscriber; use Drupal\advanced_search\AdvancedSearchQuery; use Drupal\search_api_solr\Event\PostConvertedQueryEvent; use Drupal\search_api_solr\Event\SearchAPISolrEvents; +use Symfony\Component\EventDispatcher\EventSubscriberInterface; /** * Subscribes to PostConvertedQueryEvents. From ca08cbb0b4775330744067e3a63ac5a6bf97c68d Mon Sep 17 00:00:00 2001 From: Rosie Le Faive Date: Tue, 26 Sep 2023 13:04:50 -0300 Subject: [PATCH 4/7] Implement --- src/EventSubscriber/PostConvertedQueryEventSubscriber.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/EventSubscriber/PostConvertedQueryEventSubscriber.php b/src/EventSubscriber/PostConvertedQueryEventSubscriber.php index 8287507..1fbe113 100644 --- a/src/EventSubscriber/PostConvertedQueryEventSubscriber.php +++ b/src/EventSubscriber/PostConvertedQueryEventSubscriber.php @@ -12,7 +12,7 @@ use Symfony\Component\EventDispatcher\EventSubscriberInterface; * * @package Drupal\advanced_search\EventSubscriber */ -class PostConvertedQueryEventSubscriber extends EventSubscriberInterface { +class PostConvertedQueryEventSubscriber implements EventSubscriberInterface { /** * {@inheritdoc} From f60b4f28e6fb1200d5cd8b2bace2b05b6bf533aa Mon Sep 17 00:00:00 2001 From: Rosie Le Faive Date: Tue, 26 Sep 2023 13:09:03 -0300 Subject: [PATCH 5/7] Case sensitive. --- src/EventSubscriber/PostConvertedQueryEventSubscriber.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/EventSubscriber/PostConvertedQueryEventSubscriber.php b/src/EventSubscriber/PostConvertedQueryEventSubscriber.php index 1fbe113..afff4a1 100644 --- a/src/EventSubscriber/PostConvertedQueryEventSubscriber.php +++ b/src/EventSubscriber/PostConvertedQueryEventSubscriber.php @@ -4,7 +4,7 @@ namespace Drupal\advanced_search\EventSubscriber; use Drupal\advanced_search\AdvancedSearchQuery; use Drupal\search_api_solr\Event\PostConvertedQueryEvent; -use Drupal\search_api_solr\Event\SearchAPISolrEvents; +use Drupal\search_api_solr\Event\SearchApiSolrEvents; use Symfony\Component\EventDispatcher\EventSubscriberInterface; /** From 346478be3028260f79add79dd04920666eb3efcd Mon Sep 17 00:00:00 2001 From: Rosie Le Faive Date: Tue, 26 Sep 2023 13:11:15 -0300 Subject: [PATCH 6/7] Deprecate hook_search_api_solr_converted_query_alter, use PostConvertedQueryEvent instead. --- advanced_search.module | 13 ------------- 1 file changed, 13 deletions(-) diff --git a/advanced_search.module b/advanced_search.module index d398f6c..25e3307 100644 --- a/advanced_search.module +++ b/advanced_search.module @@ -51,19 +51,6 @@ function advanced_search_library_info_alter(&$libraries, $extension) { } } -/** - * Implements hook_search_api_solr_converted_query_alter(). - */ -function advanced_search_search_api_solr_converted_query_alter(SolariumQueryInterface $solarium_query, DrupalQueryInterface $search_api_query) { - // We must modify the query itself rather than the representation the - // search_api presents as it is not possible to use the 'OR' operator - // with it as it converts conditions into separate filter queries. - // Additionally filter queries do not affect the score so are not - // suitable for use in the advanced search queries. - $advanced_search_query = new AdvancedSearchQuery(); - $advanced_search_query->alterQuery(\Drupal::request(), $solarium_query, $search_api_query); -} - /** * Implements hook_form_form_id_alter(). */ From c07f09082b5fe8ad053c06bed79d776397303748 Mon Sep 17 00:00:00 2001 From: Rosie Le Faive Date: Tue, 26 Sep 2023 14:04:02 -0300 Subject: [PATCH 7/7] Replace comment. --- src/EventSubscriber/PostConvertedQueryEventSubscriber.php | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/src/EventSubscriber/PostConvertedQueryEventSubscriber.php b/src/EventSubscriber/PostConvertedQueryEventSubscriber.php index afff4a1..2781c69 100644 --- a/src/EventSubscriber/PostConvertedQueryEventSubscriber.php +++ b/src/EventSubscriber/PostConvertedQueryEventSubscriber.php @@ -30,6 +30,12 @@ class PostConvertedQueryEventSubscriber implements EventSubscriberInterface { public function alter(PostConvertedQueryEvent $event) { $search_api_query = $event->getSearchApiQuery(); $solarium_query = $event->getSolariumQuery(); + + // We must modify the query itself rather than the representation the + // search_api presents as it is not possible to use the 'OR' operator + // with it as it converts conditions into separate filter queries. + // Additionally filter queries do not affect the score so are not + // suitable for use in the advanced search queries. $advanced_search_query = new AdvancedSearchQuery(); $advanced_search_query->alterQuery(\Drupal::request(), $solarium_query, $search_api_query); }