You are here

class JsonApiFacets in JSON:API Search API 8

Provides a facet source for use in JSON:API.

Plugin annotation


@FacetsFacetSource(
  id = "jsonapi_search_api_facets",
  deriver = "Drupal\jsonapi_search_api_facets\Plugin\facets\facet_source\JsonApiFacetsDeriver"
)

Hierarchy

Expanded class hierarchy of JsonApiFacets

1 file declares its use of JsonApiFacets
JsonApiQueryString.php in modules/jsonapi_search_api_facets/src/Plugin/facets/url_processor/JsonApiQueryString.php

File

modules/jsonapi_search_api_facets/src/Plugin/facets/facet_source/JsonApiFacets.php, line 22

Namespace

Drupal\jsonapi_search_api_facets\Plugin\facets\facet_source
View source
class JsonApiFacets extends SearchApiBaseFacetSource {
  use PluginDependencyTrait;

  /**
   * The search index.
   *
   * @var \Drupal\search_api\IndexInterface
   */
  protected $index;

  /**
   * 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\IndexInterface $index
   *   The search index.
   */
  public function __construct(array $configuration, $plugin_id, $plugin_definition, QueryTypePluginManager $query_type_plugin_manager, QueryHelper $search_results_cache, IndexInterface $index) {
    parent::__construct($configuration, $plugin_id, $plugin_definition, $query_type_plugin_manager, $search_results_cache);
    $this->index = $index;
  }

  /**
   * {@inheritdoc}
   */
  public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
    $entity_type_manager = $container
      ->get('entity_type.manager');
    $index = $entity_type_manager
      ->getStorage('search_api_index')
      ->load($plugin_definition['index']);
    return new static($configuration, $plugin_id, $plugin_definition, $container
      ->get('plugin.manager.facets.query_type'), $container
      ->get('search_api.query_helper'), $index);
  }

  /**
   * {@inheritdoc}
   */
  public function calculateDependencies() {
    $this
      ->addDependency('module', 'jsonapi_search_api_facets');
    return $this->dependencies;
  }

  /**
   * Fills the facet entities with results from the facet source.
   *
   * @param \Drupal\facets\FacetInterface[] $facets
   *   The configured facets.
   */
  public function fillFacetsWithResults(array $facets) {

    // Check if the results for this search ID are already populated in the
    // query helper.
    $results = $this->searchApiQueryHelper
      ->getResults(strtr('jsonapi_search_api:!index', [
      '!index' => $this->index
        ->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' => $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 getPath() {
  }

  /**
   * {@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");
  }

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

  /**
   * {@inheritdoc}
   */
  public function isRenderedInCurrentRequest() {
    return TRUE;
  }

}

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
DependencyTrait::$dependencies protected property The object's dependencies.
DependencyTrait::addDependencies protected function Adds multiple dependencies.
DependencyTrait::addDependency protected function Adds a dependency.
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::buildFacet public function Builds and returns an extra renderable array for this facet block plugin. Overrides FacetSourcePluginInterface::buildFacet 1
FacetSourcePluginBase::getCount public function Returns the number of results that were found for this search. Overrides FacetSourcePluginInterface::getCount 1
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
JsonApiFacets::$index protected property The search index. Overrides SearchApiBaseFacetSource::$index
JsonApiFacets::calculateDependencies public function Calculates dependencies for the configured plugin. Overrides DependentPluginInterface::calculateDependencies
JsonApiFacets::create public static function Creates an instance of the plugin. Overrides SearchApiBaseFacetSource::create
JsonApiFacets::fillFacetsWithResults public function Fills the facet entities with results from the facet source. Overrides FacetSourcePluginInterface::fillFacetsWithResults
JsonApiFacets::getDataDefinition public function Returns a single field's data definition from the facet source. Overrides FacetSourcePluginInterface::getDataDefinition
JsonApiFacets::getIndex public function Retrieves the Search API index for this facet source. Overrides SearchApiBaseFacetSource::getIndex
JsonApiFacets::getPath public function Returns the path of the facet source, used to build the facet url. Overrides FacetSourcePluginInterface::getPath
JsonApiFacets::isRenderedInCurrentRequest public function Returns true if the Facet source is being rendered in the current request. Overrides FacetSourcePluginBase::isRenderedInCurrentRequest
JsonApiFacets::__construct public function Constructs a SearchApiBaseFacetSource object. Overrides SearchApiBaseFacetSource::__construct
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.
PluginDependencyTrait::calculatePluginDependencies protected function Calculates and adds dependencies of a specific plugin instance. 1
PluginDependencyTrait::getPluginDependencies protected function Calculates and returns dependencies of a specific plugin instance.
PluginDependencyTrait::moduleHandler protected function Wraps the module handler. 1
PluginDependencyTrait::themeHandler protected function Wraps the theme handler. 1
SearchApiBaseFacetSource::$searchApiQueryHelper protected property The search result cache.
SearchApiBaseFacetSource::buildConfigurationForm public function Form constructor. Overrides PluginFormInterface::buildConfigurationForm
SearchApiBaseFacetSource::getDisplay public function Retrieves the Search API display plugin associated with this facet source. Overrides SearchApiFacetSourceInterface::getDisplay
SearchApiBaseFacetSource::getFields public function Returns an array of fields that are defined on the facet source. Overrides FacetSourcePluginBase::getFields
SearchApiBaseFacetSource::getQueryTypesForDataType public function Retrieves the query types for a specified data type.
SearchApiBaseFacetSource::getQueryTypesForFacet public function Returns the allowed query types for a given facet for the facet source. Overrides FacetSourcePluginBase::getQueryTypesForFacet
SearchApiBaseFacetSource::getViewsDisplay public function Retrieves the Views entity with the correct display set. Overrides SearchApiFacetSourceInterface::getViewsDisplay
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.