-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #6 from openeuropa/OPENEUROPA-3373
OPENEUROPA-3373: Add query functionality
- Loading branch information
Showing
13 changed files
with
621 additions
and
139 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,97 @@ | ||
<?php | ||
|
||
namespace Drupal\oe_list_pages\EventSubscriber; | ||
|
||
use Drupal\facets\FacetManager\DefaultFacetManager; | ||
use Drupal\facets\QueryType\QueryTypePluginManager; | ||
use Drupal\search_api\Event\QueryPreExecuteEvent; | ||
use Drupal\search_api\Event\SearchApiEvents; | ||
use Symfony\Component\EventDispatcher\EventSubscriberInterface; | ||
|
||
/** | ||
* Provides an event subscriber that allows to alter list source queries. | ||
*/ | ||
class QuerySubscriber implements EventSubscriberInterface { | ||
|
||
/** | ||
* The facets manager. | ||
* | ||
* @var \Drupal\facets\FacetManager\DefaultFacetManager | ||
*/ | ||
protected $facetManager; | ||
|
||
/** | ||
* The query type plugin manager. | ||
* | ||
* @var \Drupal\facets\QueryType\QueryTypePluginManager | ||
*/ | ||
protected $queryTypePluginManager; | ||
|
||
/** | ||
* QuerySubscriber Constructor. | ||
* | ||
* @param \Drupal\facets\FacetManager\DefaultFacetManager $facetManager | ||
* The facets manager. | ||
* @param \Drupal\facets\QueryType\QueryTypePluginManager $queryTypePluginManager | ||
* The query type plugin manager. | ||
*/ | ||
public function __construct(DefaultFacetManager $facetManager, QueryTypePluginManager $queryTypePluginManager) { | ||
$this->facetManager = $facetManager; | ||
$this->queryTypePluginManager = $queryTypePluginManager; | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public static function getSubscribedEvents() { | ||
return [ | ||
SearchApiEvents::QUERY_PRE_EXECUTE => 'queryAlter', | ||
]; | ||
} | ||
|
||
/** | ||
* Reacts to the query alter event. | ||
* | ||
* @param \Drupal\search_api\Event\QueryPreExecuteEvent $event | ||
* The query alter event. | ||
*/ | ||
public function queryAlter(QueryPreExecuteEvent $event) { | ||
$query = $event->getQuery(); | ||
|
||
$ignored_filters = $preset_filters = []; | ||
|
||
if (!$query->getIndex()->getServerInstance()->supportsFeature('search_api_facets')) { | ||
return; | ||
} | ||
|
||
$facetsource_id = $query->getSearchId(); | ||
/** @var \Drupal\oe_list_pages\ListQueryOptionsInterface $query_options */ | ||
$query_options = $query->getOption('oe_list_page_query_options'); | ||
|
||
if (!empty($query_options)) { | ||
$ignored_filters = $query_options->getIgnoredFilters(); | ||
$preset_filters = $query_options->getPresetFiltersValues(); | ||
} | ||
|
||
// Add the active filters. | ||
foreach ($this->facetManager->getFacetsByFacetSourceId($facetsource_id) as $facet) { | ||
// Handle preset filters. If filter is preset, set as active items. | ||
if (in_array($facet->id(), array_keys($preset_filters))) { | ||
$facet->setActiveItems([$preset_filters[$facet->id()]]); | ||
} | ||
|
||
// Handle ignored filters. If filter is ignored unset its active items. | ||
if (in_array($facet->id(), $ignored_filters)) { | ||
$facet->setActiveItems([]); | ||
} | ||
|
||
/** @var \Drupal\facets\QueryType\QueryTypeInterface $query_type_plugin */ | ||
$query_type_plugin = $this->queryTypePluginManager->createInstance($facet->getQueryType(), [ | ||
'query' => $query, | ||
'facet' => $facet, | ||
]); | ||
$query_type_plugin->execute(); | ||
} | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
<?php | ||
|
||
declare(strict_types = 1); | ||
|
||
namespace Drupal\oe_list_pages; | ||
|
||
/** | ||
* List source query options value object. | ||
* | ||
* Used to store list options that are applied to search api query. | ||
*/ | ||
class ListQueryOptions implements ListQueryOptionsInterface { | ||
|
||
/** | ||
* The ignored filters. | ||
* | ||
* @var array | ||
*/ | ||
protected $ignoredFilters = []; | ||
|
||
/** | ||
* The preset filters. | ||
* | ||
* @var array | ||
*/ | ||
protected $presetFiltersValues = []; | ||
|
||
/** | ||
* ListQueryOptions constructor. | ||
* | ||
* @param array $ignored_filters | ||
* The ignored filters. | ||
* @param array $preset_filters | ||
* The preset filters. | ||
*/ | ||
public function __construct(array $ignored_filters, array $preset_filters) { | ||
$this->ignoredFilters = $ignored_filters; | ||
$this->presetFiltersValues = $preset_filters; | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function getIgnoredFilters(): array { | ||
return $this->ignoredFilters; | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function setIgnoredFilters(array $ignoredFilters): void { | ||
$this->ignoredFilters = $ignoredFilters; | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function getPresetFiltersValues(): array { | ||
return $this->presetFiltersValues; | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function setPresetFiltersValues(array $presetFiltersValues): void { | ||
$this->presetFiltersValues = $presetFiltersValues; | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
<?php | ||
|
||
declare(strict_types = 1); | ||
|
||
namespace Drupal\oe_list_pages; | ||
|
||
/** | ||
* Interface for List source query options value object. | ||
* | ||
* Used to store list page options that are applied to search api query. | ||
*/ | ||
interface ListQueryOptionsInterface { | ||
|
||
/** | ||
* Gets the ignored filters. | ||
* | ||
* @return array | ||
* The ignored filters. | ||
*/ | ||
public function getIgnoredFilters(): array; | ||
|
||
/** | ||
* Sets the ignored filters. | ||
* | ||
* @param array $ignoredFilters | ||
* The ignored filters. | ||
*/ | ||
public function setIgnoredFilters(array $ignoredFilters): void; | ||
|
||
/** | ||
* Gets the preset filters. | ||
* | ||
* @return array | ||
* The preset filters. | ||
*/ | ||
public function getPresetFiltersValues(): array; | ||
|
||
/** | ||
* Sets the preset filters. | ||
* | ||
* @param array $presetFiltersValues | ||
* The preset filters. | ||
*/ | ||
public function setPresetFiltersValues(array $presetFiltersValues): void; | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.