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_summary\EventSubscriber\SearchApiSubscriber implements \Symfony\Component\EventDispatcher\EventSubscriberInterface

Expanded class hierarchy of SearchApiSubscriber

1 string reference to 'SearchApiSubscriber'
facets_summary.services.yml in modules/facets_summary/facets_summary.services.yml
modules/facets_summary/facets_summary.services.yml
1 service uses SearchApiSubscriber
facets_summary.search_api_subscriber in modules/facets_summary/facets_summary.services.yml
Drupal\facets_summary\EventSubscriber\SearchApiSubscriber

File

modules/facets_summary/src/EventSubscriber/SearchApiSubscriber.php, line 10

Namespace

Drupal\facets_summary\EventSubscriber
View source
class SearchApiSubscriber implements EventSubscriberInterface {

  /**
   * @var \Drupal\Core\Entity\EntityTypeManagerInterface
   */
  private $entityTypeManager;

  /**
   * Constructs a new class instance.
   *
   * @param \Drupal\Core\Entity\EntityTypeManagerInterface $entityTypeManager
   *   The entity type manager.
   */
  public function __construct(EntityTypeManagerInterface $entityTypeManager) {
    $this->entityTypeManager = $entityTypeManager;
  }

  /**
   * 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();
    $facet_source_id = 'search_api:' . str_replace(':', '__', $query
      ->getSearchId());
    $storage = $this->entityTypeManager
      ->getStorage('facets_summary');

    // Get all the facet summaries for the facet source.
    $facet_summaries = $storage
      ->loadByProperties([
      'facet_source_id' => $facet_source_id,
    ]);

    /** @var \Drupal\facets_summary\FacetsSummaryInterface $facet_summary */
    foreach ($facet_summaries as $facet_summary) {
      $processors = $facet_summary
        ->getProcessors();

      // If the count processor is enabled, results count must not be skipped.
      if (in_array('show_count', array_keys($processors))) {
        $query
          ->setOption('skip result count', FALSE);
        break;
      }
    }
  }

  /**
   * {@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::$entityTypeManager 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.