You are here

class SearchApiDisplay in Facets 8

Provides a facet source based on a Search API display.

Plugin annotation


@FacetsFacetSource(
  id = "search_api",
  deriver = "Drupal\facets\Plugin\facets\facet_source\SearchApiDisplayDeriver"
)

Hierarchy

Expanded class hierarchy of SearchApiDisplay

4 files declare their use of SearchApiDisplay
FacetFacetSourceTest.php in tests/src/Kernel/Entity/FacetFacetSourceTest.php
FacetForm.php in src/Form/FacetForm.php
ListItemProcessor.php in src/Plugin/facets/processor/ListItemProcessor.php
TranslateEntityAggregatedFieldProcessor.php in src/Plugin/facets/processor/TranslateEntityAggregatedFieldProcessor.php

File

src/Plugin/facets/facet_source/SearchApiDisplay.php, line 32

Namespace

Drupal\facets\Plugin\facets\facet_source
View source
class SearchApiDisplay extends FacetSourcePluginBase implements SearchApiFacetSourceInterface {

  /**
   * The search index the query should is executed on.
   *
   * @var \Drupal\search_api\IndexInterface
   */
  protected $index;

  /**
   * The display plugin manager.
   *
   * @var \Drupal\search_api\Display\DisplayPluginManager
   */
  protected $displayPluginManager;

  /**
   * The search result cache.
   *
   * @var \Drupal\search_api\Utility\QueryHelper
   */
  protected $searchApiQueryHelper;

  /**
   * The clone of the current request.
   *
   * @var \Symfony\Component\HttpFoundation\Request
   */
  protected $request;

  /**
   * The Drupal module handler.
   *
   * @var \Drupal\Core\Extension\ModuleHandler
   */
  protected $moduleHandler;

  /**
   * Constructs a SearchApiBaseFacetSource object.
   *
   * @param array $configuration
   *   A configuration array containing information about the plugin instance.
   * @param string $plugin_id
   *   The plugin_id for the plugin instance.
   * @param mixed $plugin_definition
   *   The plugin implementation definition.
   * @param \Drupal\facets\QueryType\QueryTypePluginManager $query_type_plugin_manager
   *   The query type plugin manager.
   * @param \Drupal\search_api\Utility\QueryHelper $search_results_cache
   *   The query type plugin manager.
   * @param \Drupal\search_api\Display\DisplayPluginManager $display_plugin_manager
   *   The display plugin manager.
   * @param \Symfony\Component\HttpFoundation\Request $request
   *   A request object for the current request.
   * @param \Drupal\Core\Extension\ModuleHandler $moduleHandler
   *   Core's module handler class.
   */
  public function __construct(array $configuration, $plugin_id, $plugin_definition, QueryTypePluginManager $query_type_plugin_manager, QueryHelper $search_results_cache, DisplayPluginManager $display_plugin_manager, Request $request, ModuleHandler $moduleHandler) {
    parent::__construct($configuration, $plugin_id, $plugin_definition, $query_type_plugin_manager);
    $this->searchApiQueryHelper = $search_results_cache;
    $this->displayPluginManager = $display_plugin_manager;
    $this->moduleHandler = $moduleHandler;
    $this->request = clone $request;
  }

  /**
   * {@inheritdoc}
   */
  public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {

    // If the Search API module is not enabled, we should just return an empty
    // object. This allows us to have this class in the module without having a
    // dependency on the Search API module.
    if (!$container
      ->get('module_handler')
      ->moduleExists('search_api')) {
      return new \stdClass();
    }
    return new static($configuration, $plugin_id, $plugin_definition, $container
      ->get('plugin.manager.facets.query_type'), $container
      ->get('search_api.query_helper'), $container
      ->get('plugin.manager.search_api.display'), $container
      ->get('request_stack')
      ->getMasterRequest(), $container
      ->get('module_handler'));
  }

  /**
   * Retrieves the Search API index for this facet source.
   *
   * @return \Drupal\search_api\IndexInterface
   *   The search index.
   */
  public function getIndex() {
    if ($this->index === NULL) {
      $this->index = $this
        ->getDisplay()
        ->getIndex();
    }
    return $this->index;
  }

  /**
   * {@inheritdoc}
   */
  public function getPath() {

    // The implementation in search api tells us that this is a base path only
    // if a path is defined, and false if that isn't done. This means that we
    // have to check for this + create our own uri if that's needed.
    if ($this
      ->getDisplay()
      ->getPath()) {
      return $this
        ->getDisplay()
        ->getPath();
    }
    return \Drupal::service('path.current')
      ->getPath();
  }

  /**
   * {@inheritdoc}
   */
  public function fillFacetsWithResults(array $facets) {
    $search_id = $this
      ->getDisplay()
      ->getPluginId();

    // Check if the results for this search id are already populated in the
    // query helper. This is usually the case for views displays that are
    // rendered on the same page, such as views_page.
    $results = $this->searchApiQueryHelper
      ->getResults($search_id);

    // If there are no results, we can check the Search API Display plugin has
    // configuration for views. If that configuration exists, we can execute
    // that view and try to use it's results.
    $display_definition = $this
      ->getDisplay()
      ->getPluginDefinition();
    if ($results === NULL && isset($display_definition['view_id'])) {
      $view = Views::getView($display_definition['view_id']);
      $view
        ->setDisplay($display_definition['view_display']);
      $view
        ->execute();
      $results = $this->searchApiQueryHelper
        ->getResults($search_id);
    }
    if (!$results instanceof ResultSetInterface) {
      return;
    }

    // Get our facet data.
    $facet_results = $results
      ->getExtraData('search_api_facets');

    // If no data is found in the 'search_api_facets' extra data, we can stop
    // execution here.
    if ($facet_results === []) {
      return;
    }

    // Loop over each facet and execute the build method from the given
    // query type.
    foreach ($facets as $facet) {
      $configuration = [
        'query' => $results
          ->getQuery(),
        'facet' => $facet,
        'results' => isset($facet_results[$facet
          ->getFieldIdentifier()]) ? $facet_results[$facet
          ->getFieldIdentifier()] : [],
      ];

      // Get the Facet Specific Query Type so we can process the results
      // using the build() function of the query type.
      $query_type = $this->queryTypePluginManager
        ->createInstance($facet
        ->getQueryType(), $configuration);
      $query_type
        ->build();
    }
  }

  /**
   * {@inheritdoc}
   */
  public function isRenderedInCurrentRequest() {
    return $this
      ->getDisplay()
      ->isRenderedInCurrentRequest();
  }

  /**
   * {@inheritdoc}
   */
  public function buildConfigurationForm(array $form, FormStateInterface $form_state) {
    $form['field_identifier'] = [
      '#type' => 'select',
      '#options' => $this
        ->getFields(),
      '#title' => $this
        ->t('Field'),
      '#description' => $this
        ->t('The field from the selected facet source which contains the data to build a facet for.<br> The field types supported are <strong>boolean</strong>, <strong>date</strong>, <strong>decimal</strong>, <strong>integer</strong> and <strong>string</strong>.'),
      '#required' => TRUE,
      '#default_value' => $this->facet
        ->getFieldIdentifier(),
    ];
    return $form;
  }

  /**
   * {@inheritdoc}
   */
  public function getFields() {
    $indexed_fields = [];
    $index = $this
      ->getIndex();
    $fields = $index
      ->getFields();
    $server = $index
      ->getServerInstance();
    $backend = $server
      ->getBackend();
    foreach ($fields as $field) {
      $data_type_plugin_id = $field
        ->getDataTypePlugin()
        ->getPluginId();
      $query_types = $this
        ->getQueryTypesForDataType($backend, $data_type_plugin_id);
      if (!empty($query_types)) {
        $indexed_fields[$field
          ->getFieldIdentifier()] = $field
          ->getLabel() . ' (' . $field
          ->getPropertyPath() . ')';
      }
    }
    return $indexed_fields;
  }

  /**
   * {@inheritdoc}
   */
  public function getQueryTypesForFacet(FacetInterface $facet) {

    // Get our Facets Field Identifier, which is equal to the Search API Field
    // identifier.
    $field_id = $facet
      ->getFieldIdentifier();

    /** @var \Drupal\search_api\IndexInterface $index */
    $index = $this
      ->getIndex();

    // Get the Search API Server.
    $server = $index
      ->getServerInstance();

    // Get the Search API Backend.
    $backend = $server
      ->getBackend();
    $fields = $index
      ->getFields();
    foreach ($fields as $field) {
      if ($field
        ->getFieldIdentifier() == $field_id) {
        return $this
          ->getQueryTypesForDataType($backend, $field
          ->getType());
      }
    }
    throw new InvalidQueryTypeException("No available query types were found for facet {$facet->getName()}");
  }

  /**
   * Retrieves the query types for a specified data type.
   *
   * Backend plugins can use this method to override the default query types
   * provided by the Search API with backend-specific ones that better use
   * features of that backend.
   *
   * @param \Drupal\search_api\Backend\BackendInterface $backend
   *   The backend that we want to get the query types for.
   * @param string $data_type_plugin_id
   *   The identifier of the data type.
   *
   * @return string[]
   *   An associative array with the plugin IDs of allowed query types, keyed by
   *   the generic name of the query_type.
   *
   * @see hook_facets_search_api_query_type_mapping_alter()
   */
  protected function getQueryTypesForDataType(BackendInterface $backend, $data_type_plugin_id) {
    $query_types = [];
    $query_types['string'] = 'search_api_string';

    // Add additional query types for specific data types.
    switch ($data_type_plugin_id) {
      case 'date':
        $query_types['date'] = 'search_api_date';
        $query_types['range'] = 'search_api_range';
        break;
      case 'decimal':
      case 'integer':
        $query_types['numeric'] = 'search_api_granular';
        $query_types['range'] = 'search_api_range';
        break;
    }

    // Find out if the backend implemented the Interface to retrieve specific
    // query types for the supported data_types.
    if ($backend instanceof FacetsQueryTypeMappingInterface) {
      $mapping = [
        $data_type_plugin_id => &$query_types,
      ];
      $backend
        ->alterFacetQueryTypeMapping($mapping);
    }

    // Add it to a variable so we can pass it by reference. Alter hook complains
    // due to the property of the backend object is not passable by reference.
    $backend_plugin_id = $backend
      ->getPluginId();

    // Let modules alter this mapping.
    \Drupal::moduleHandler()
      ->alter('facets_search_api_query_type_mapping', $backend_plugin_id, $query_types);
    return $query_types;
  }

  /**
   * {@inheritdoc}
   */
  public function calculateDependencies() {
    $display = $this
      ->getDisplay();
    if ($display instanceof DependentPluginInterface) {
      return $display
        ->calculateDependencies();
    }
    return [];
  }

  /**
   * {@inheritdoc}
   */
  public function getDisplay() {
    return $this->displayPluginManager
      ->createInstance($this->pluginDefinition['display_id']);
  }

  /**
   * {@inheritdoc}
   */
  public function getViewsDisplay() {
    if (!$this->moduleHandler
      ->moduleExists('views')) {
      return NULL;
    }
    $search_api_display_definition = $this
      ->getDisplay()
      ->getPluginDefinition();
    if (empty($search_api_display_definition['view_id'])) {
      return NULL;
    }
    $view_id = $search_api_display_definition['view_id'];
    $view_display = $search_api_display_definition['view_display'];
    $view = Views::getView($view_id);
    $view
      ->setDisplay($view_display);
    return $view;
  }

  /**
   * {@inheritdoc}
   */
  public function getDataDefinition($field_name) {
    $field = $this
      ->getIndex()
      ->getField($field_name);
    if ($field) {
      return $field
        ->getDataDefinition();
    }
    throw new Exception("Field with name {$field_name} does not have a definition");
  }

  /**
   * {@inheritdoc}
   */
  public function buildFacet() {
    $build = parent::buildFacet();
    $view = $this
      ->getViewsDisplay();
    if ($view === NULL) {
      return $build;
    }

    // Add JS for Views with Ajax Enabled.
    if ($view->display_handler
      ->ajaxEnabled()) {
      $js_settings = [
        'view_id' => $view
          ->id(),
        'current_display_id' => $view->current_display,
        'view_base_path' => ltrim($view
          ->getPath(), '/'),
        'ajax_path' => Url::fromRoute('views.ajax')
          ->toString(),
      ];
      $build['#attached']['library'][] = 'facets/drupal.facets.views-ajax';
      $build['#attached']['drupalSettings']['facets_views_ajax'] = [
        $this->facet
          ->id() => $js_settings,
      ];
      $build['#use_ajax'] = TRUE;
    }
    return $build;
  }

  /**
   * {@inheritdoc}
   */
  public function getCount() {
    $search_id = $this
      ->getDisplay()
      ->getPluginId();
    if ($search_id && !empty($search_id)) {
      if ($this->searchApiQueryHelper
        ->getResults($search_id) !== NULL) {
        return $this->searchApiQueryHelper
          ->getResults($search_id)
          ->getResultCount();
      }
    }
  }

}

Members

Namesort descending Modifiers Type Description Overrides
DependencySerializationTrait::$_entityStorages protected property An array of entity type IDs keyed by the property name of their storages.
DependencySerializationTrait::$_serviceIds protected property An array of service IDs keyed by property name used for serialization.
DependencySerializationTrait::__sleep public function 1
DependencySerializationTrait::__wakeup public function 2
FacetSourcePluginBase::$facet protected property The facet we're editing for.
FacetSourcePluginBase::$keys protected property The search keys, or query text, submitted by the user.
FacetSourcePluginBase::$queryTypePluginManager protected property The plugin manager.
FacetSourcePluginBase::getSearchKeys public function Returns the search keys, or query text, submitted by the user. Overrides FacetSourcePluginInterface::getSearchKeys
FacetSourcePluginBase::setSearchKeys public function Sets the search keys, or query text, submitted by the user. Overrides FacetSourcePluginInterface::setSearchKeys
FacetSourcePluginBase::submitConfigurationForm public function Form submission handler. Overrides PluginFormInterface::submitConfigurationForm
FacetSourcePluginBase::validateConfigurationForm public function Form validation handler. Overrides PluginFormInterface::validateConfigurationForm
MessengerTrait::$messenger protected property The messenger. 29
MessengerTrait::messenger public function Gets the messenger. 29
MessengerTrait::setMessenger public function Sets the messenger.
PluginBase::$configuration protected property Configuration information passed into the plugin. 1
PluginBase::$pluginDefinition protected property The plugin implementation definition. 1
PluginBase::$pluginId protected property The plugin_id.
PluginBase::DERIVATIVE_SEPARATOR constant A string which is used to separate base plugin IDs from the derivative ID.
PluginBase::getBaseId public function Gets the base_plugin_id of the plugin instance. Overrides DerivativeInspectionInterface::getBaseId
PluginBase::getDerivativeId public function Gets the derivative_id of the plugin instance. Overrides DerivativeInspectionInterface::getDerivativeId
PluginBase::getPluginDefinition public function Gets the definition of the plugin implementation. Overrides PluginInspectionInterface::getPluginDefinition 3
PluginBase::getPluginId public function Gets the plugin_id of the plugin instance. Overrides PluginInspectionInterface::getPluginId
PluginBase::isConfigurable public function Determines if the plugin is configurable.
SearchApiDisplay::$displayPluginManager protected property The display plugin manager.
SearchApiDisplay::$index protected property The search index the query should is executed on.
SearchApiDisplay::$moduleHandler protected property The Drupal module handler.
SearchApiDisplay::$request protected property The clone of the current request.
SearchApiDisplay::$searchApiQueryHelper protected property The search result cache.
SearchApiDisplay::buildConfigurationForm public function Form constructor. Overrides PluginFormInterface::buildConfigurationForm
SearchApiDisplay::buildFacet public function Builds and returns an extra renderable array for this facet block plugin. Overrides FacetSourcePluginBase::buildFacet
SearchApiDisplay::calculateDependencies public function Calculates dependencies for the configured plugin. Overrides DependentPluginInterface::calculateDependencies
SearchApiDisplay::create public static function Creates an instance of the plugin. Overrides FacetSourcePluginBase::create
SearchApiDisplay::fillFacetsWithResults public function Fills the facet entities with results from the facet source. Overrides FacetSourcePluginInterface::fillFacetsWithResults
SearchApiDisplay::getCount public function Returns the number of results that were found for this search. Overrides FacetSourcePluginBase::getCount
SearchApiDisplay::getDataDefinition public function Returns a single field's data definition from the facet source. Overrides FacetSourcePluginInterface::getDataDefinition
SearchApiDisplay::getDisplay public function Retrieves the Search API display plugin associated with this facet source. Overrides SearchApiFacetSourceInterface::getDisplay
SearchApiDisplay::getFields public function Returns an array of fields that are defined on the facet source. Overrides FacetSourcePluginBase::getFields
SearchApiDisplay::getIndex public function Retrieves the Search API index for this facet source. Overrides SearchApiFacetSourceInterface::getIndex
SearchApiDisplay::getPath public function Returns the path of the facet source, used to build the facet url. Overrides FacetSourcePluginInterface::getPath
SearchApiDisplay::getQueryTypesForDataType protected function Retrieves the query types for a specified data type.
SearchApiDisplay::getQueryTypesForFacet public function Returns the allowed query types for a given facet for the facet source. Overrides FacetSourcePluginBase::getQueryTypesForFacet
SearchApiDisplay::getViewsDisplay public function Retrieves the Views entity with the correct display set. Overrides SearchApiFacetSourceInterface::getViewsDisplay
SearchApiDisplay::isRenderedInCurrentRequest public function Returns true if the Facet source is being rendered in the current request. Overrides FacetSourcePluginBase::isRenderedInCurrentRequest
SearchApiDisplay::__construct public function Constructs a SearchApiBaseFacetSource object. Overrides FacetSourcePluginBase::__construct
StringTranslationTrait::$stringTranslation protected property The string translation service. 1
StringTranslationTrait::formatPlural protected function Formats a string containing a count of items.
StringTranslationTrait::getNumberOfPlurals protected function Returns the number of plurals supported by a given language.
StringTranslationTrait::getStringTranslation protected function Gets the string translation service.
StringTranslationTrait::setStringTranslation public function Sets the string translation service to use. 2
StringTranslationTrait::t protected function Translates a string to the current language or to a given language.