You are here

class AddSearchMetaEventSubscriber in JSON:API Search API 8

Event subscriber to add facets into search results.

Hierarchy

  • class \Drupal\jsonapi_search_api_facets\EventSubscriber\AddSearchMetaEventSubscriber implements \Symfony\Component\EventDispatcher\EventSubscriberInterface

Expanded class hierarchy of AddSearchMetaEventSubscriber

1 string reference to 'AddSearchMetaEventSubscriber'
jsonapi_search_api_facets.services.yml in modules/jsonapi_search_api_facets/jsonapi_search_api_facets.services.yml
modules/jsonapi_search_api_facets/jsonapi_search_api_facets.services.yml
1 service uses AddSearchMetaEventSubscriber
jsonapi_search_api_facets.add_search_meta_subscriber in modules/jsonapi_search_api_facets/jsonapi_search_api_facets.services.yml
Drupal\jsonapi_search_api_facets\EventSubscriber\AddSearchMetaEventSubscriber

File

modules/jsonapi_search_api_facets/src/EventSubscriber/AddSearchMetaEventSubscriber.php, line 14

Namespace

Drupal\jsonapi_search_api_facets\EventSubscriber
View source
class AddSearchMetaEventSubscriber implements EventSubscriberInterface {

  /**
   * The facet manager.
   *
   * @var \Drupal\facets\FacetManager\DefaultFacetManager
   */
  protected $facetManager;

  /**
   * The entity storage used for facets.
   *
   * @var \Drupal\Core\Entity\EntityStorageInterface
   */
  protected $facetStorage;

  /**
   * Constructs a new event subscriber.
   *
   * @param Drupal\facets\FacetManager\DefaultFacetManager $facet_manager
   *   The facet manager.
   * @param Drupal\Core\Entity\EntityTypeManager $entity_type_manager
   *   The entity type manager.
   */
  public function __construct(DefaultFacetManager $facet_manager, EntityTypeManager $entity_type_manager) {
    $this->facetManager = $facet_manager;
    $this->facetStorage = $entity_type_manager
      ->getStorage('facets_facet');
  }

  /**
   * {@inheritdoc}
   */
  public static function getSubscribedEvents() {
    $events = [];
    $events[Events::ADD_SEARCH_META][] = [
      'appendFacets',
    ];
    return $events;
  }

  /**
   * Adds facet information into the meta of the JSON:API Search API response.
   *
   * @param \Drupal\jsonapi_search_api\Event\AddSearchMetaEvent $event
   *   The event being subscribed to.
   */
  public function appendFacets(AddSearchMetaEvent $event) {
    $query = $event
      ->getQuery();
    if (strpos($query
      ->getSearchId(), 'jsonapi_search_api:') === 0 && $query
      ->getIndex()
      ->getServerInstance()
      ->supportsFeature('search_api_facets')) {
      $facet_source_id = strtr('jsonapi_search_api_facets:!index', [
        '!index' => $query
          ->getIndex()
          ->id(),
      ]);
      $facets = $this->facetManager
        ->getFacetsByFacetSourceId($facet_source_id);
      $meta = [];
      foreach ($facets as $facet) {

        // No need to build the facet if it does not need to be visible.
        if ($facet
          ->getOnlyVisibleWhenFacetSourceIsVisible() && (!$facet
          ->getFacetSource() || !$facet
          ->getFacetSource()
          ->isRenderedInCurrentRequest())) {
          continue;
        }

        // Let the facet_manager build the facets, it returns each facet wrapped
        // in an array for some reason. For better readability get rid of that.
        $facet_results = $this->facetManager
          ->build($facet);
        $facet_data = reset($facet_results);

        // If there are no results, the facet manager adds empty behavior render
        // array data. We need to strip that out.
        if (count($facet
          ->getResults()) === 0) {
          $facet_data = reset($facet_data);
        }
        if ($facet_data) {
          $meta[] = $facet_data;
        }
      }
      if (!empty($meta)) {
        $event
          ->setMeta('facets', $meta);
      }
    }
  }

}

Members

Namesort descending Modifiers Type Description Overrides
AddSearchMetaEventSubscriber::$facetManager protected property The facet manager.
AddSearchMetaEventSubscriber::$facetStorage protected property The entity storage used for facets.
AddSearchMetaEventSubscriber::appendFacets public function Adds facet information into the meta of the JSON:API Search API response.
AddSearchMetaEventSubscriber::getSubscribedEvents public static function
AddSearchMetaEventSubscriber::__construct public function Constructs a new event subscriber.