class SearchApiSubscriber in Facets 8
Same name in this branch
- 8 src/EventSubscriber/SearchApiSubscriber.php \Drupal\facets\EventSubscriber\SearchApiSubscriber
- 8 modules/facets_summary/src/EventSubscriber/SearchApiSubscriber.php \Drupal\facets_summary\EventSubscriber\SearchApiSubscriber
Hierarchy
- class \Drupal\facets\EventSubscriber\SearchApiSubscriber implements \Symfony\Component\EventDispatcher\EventSubscriberInterface
Expanded class hierarchy of SearchApiSubscriber
1 string reference to 'SearchApiSubscriber'
1 service uses SearchApiSubscriber
File
- src/
EventSubscriber/ SearchApiSubscriber.php, line 10
Namespace
Drupal\facets\EventSubscriberView source
class SearchApiSubscriber implements EventSubscriberInterface {
/**
* @var \Drupal\facets\FacetManager\DefaultFacetManager
*/
private $facetManager;
/**
* Constructs a new class instance.
*
* @param \Drupal\facets\FacetManager\DefaultFacetManager $facetManager
* The messenger.
*/
public function __construct(DefaultFacetManager $facetManager) {
$this->facetManager = $facetManager;
}
/**
* 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();
if ($query
->getIndex()
->getServerInstance()
->supportsFeature('search_api_facets')) {
// It's safe to hardcode this to the search api scheme because this is in
// an event subscriber. If this generated source is not correct,
// implementing the same subscriber and directly calling
// $manager->alterQuery($query, $your_facetsource_id); will fix that.
$facet_source = 'search_api:' . str_replace(':', '__', $query
->getSearchId());
// Add the active filters.
$this->facetManager
->alterQuery($query, $facet_source);
}
}
/**
* {@inheritdoc}
*/
public static function getSubscribedEvents() {
// Workaround to avoid a fatal error during site install from existing config.
// @see https://www.drupal.org/project/facets/issues/3199156
if (!class_exists('\\Drupal\\search_api\\Event\\SearchApiEvents', TRUE)) {
return [];
}
return [
SearchApiEvents::QUERY_PRE_EXECUTE => 'queryAlter',
];
}
}
Members
Name | Modifiers | Type | Description | Overrides |
---|---|---|---|---|
SearchApiSubscriber:: |
private | property | ||
SearchApiSubscriber:: |
public static | function | Returns an array of event names this subscriber wants to listen to. | |
SearchApiSubscriber:: |
public | function | Reacts to the query alter event. | |
SearchApiSubscriber:: |
public | function | Constructs a new class instance. |