You are here

class SearchApiSubscriber in Facets 8

Same name in this branch
  1. 8 src/EventSubscriber/SearchApiSubscriber.php \Drupal\facets\EventSubscriber\SearchApiSubscriber
  2. 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'
facets.services.yml in ./facets.services.yml
facets.services.yml
1 service uses SearchApiSubscriber
facets.search_api_subscriber in ./facets.services.yml
Drupal\facets\EventSubscriber\SearchApiSubscriber

File

src/EventSubscriber/SearchApiSubscriber.php, line 10

Namespace

Drupal\facets\EventSubscriber
View 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

Namesort descending Modifiers Type Description Overrides
SearchApiSubscriber::$facetManager private property
SearchApiSubscriber::getSubscribedEvents public static function Returns an array of event names this subscriber wants to listen to.
SearchApiSubscriber::queryAlter public function Reacts to the query alter event.
SearchApiSubscriber::__construct public function Constructs a new class instance.