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
- class \Drupal\Component\Plugin\PluginBase implements DerivativeInspectionInterface, PluginInspectionInterface
- class \Drupal\Core\Plugin\PluginBase uses DependencySerializationTrait, MessengerTrait, StringTranslationTrait
- class \Drupal\facets\FacetSource\FacetSourcePluginBase implements ContainerFactoryPluginInterface, FacetSourcePluginInterface
- class \Drupal\facets\Plugin\facets\facet_source\SearchApiBaseFacetSource implements SearchApiFacetSourceInterface
- class \Drupal\jsonapi_search_api_facets\Plugin\facets\facet_source\JsonApiFacets uses PluginDependencyTrait
- class \Drupal\facets\Plugin\facets\facet_source\SearchApiBaseFacetSource implements SearchApiFacetSourceInterface
- class \Drupal\facets\FacetSource\FacetSourcePluginBase implements ContainerFactoryPluginInterface, FacetSourcePluginInterface
- class \Drupal\Core\Plugin\PluginBase uses DependencySerializationTrait, MessengerTrait, StringTranslationTrait
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_sourceView 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
Name | Modifiers | Type | Description | Overrides |
---|---|---|---|---|
DependencySerializationTrait:: |
protected | property | An array of entity type IDs keyed by the property name of their storages. | |
DependencySerializationTrait:: |
protected | property | An array of service IDs keyed by property name used for serialization. | |
DependencySerializationTrait:: |
public | function | 1 | |
DependencySerializationTrait:: |
public | function | 2 | |
DependencyTrait:: |
protected | property | The object's dependencies. | |
DependencyTrait:: |
protected | function | Adds multiple dependencies. | |
DependencyTrait:: |
protected | function | Adds a dependency. | |
FacetSourcePluginBase:: |
protected | property | The facet we're editing for. | |
FacetSourcePluginBase:: |
protected | property | The search keys, or query text, submitted by the user. | |
FacetSourcePluginBase:: |
protected | property | The plugin manager. | |
FacetSourcePluginBase:: |
public | function |
Builds and returns an extra renderable array for this facet block plugin. Overrides FacetSourcePluginInterface:: |
1 |
FacetSourcePluginBase:: |
public | function |
Returns the number of results that were found for this search. Overrides FacetSourcePluginInterface:: |
1 |
FacetSourcePluginBase:: |
public | function |
Returns the search keys, or query text, submitted by the user. Overrides FacetSourcePluginInterface:: |
|
FacetSourcePluginBase:: |
public | function |
Sets the search keys, or query text, submitted by the user. Overrides FacetSourcePluginInterface:: |
|
FacetSourcePluginBase:: |
public | function |
Form submission handler. Overrides PluginFormInterface:: |
|
FacetSourcePluginBase:: |
public | function |
Form validation handler. Overrides PluginFormInterface:: |
|
JsonApiFacets:: |
protected | property |
The search index. Overrides SearchApiBaseFacetSource:: |
|
JsonApiFacets:: |
public | function |
Calculates dependencies for the configured plugin. Overrides DependentPluginInterface:: |
|
JsonApiFacets:: |
public static | function |
Creates an instance of the plugin. Overrides SearchApiBaseFacetSource:: |
|
JsonApiFacets:: |
public | function |
Fills the facet entities with results from the facet source. Overrides FacetSourcePluginInterface:: |
|
JsonApiFacets:: |
public | function |
Returns a single field's data definition from the facet source. Overrides FacetSourcePluginInterface:: |
|
JsonApiFacets:: |
public | function |
Retrieves the Search API index for this facet source. Overrides SearchApiBaseFacetSource:: |
|
JsonApiFacets:: |
public | function |
Returns the path of the facet source, used to build the facet url. Overrides FacetSourcePluginInterface:: |
|
JsonApiFacets:: |
public | function |
Returns true if the Facet source is being rendered in the current request. Overrides FacetSourcePluginBase:: |
|
JsonApiFacets:: |
public | function |
Constructs a SearchApiBaseFacetSource object. Overrides SearchApiBaseFacetSource:: |
|
MessengerTrait:: |
protected | property | The messenger. | 29 |
MessengerTrait:: |
public | function | Gets the messenger. | 29 |
MessengerTrait:: |
public | function | Sets the messenger. | |
PluginBase:: |
protected | property | Configuration information passed into the plugin. | 1 |
PluginBase:: |
protected | property | The plugin implementation definition. | 1 |
PluginBase:: |
protected | property | The plugin_id. | |
PluginBase:: |
constant | A string which is used to separate base plugin IDs from the derivative ID. | ||
PluginBase:: |
public | function |
Gets the base_plugin_id of the plugin instance. Overrides DerivativeInspectionInterface:: |
|
PluginBase:: |
public | function |
Gets the derivative_id of the plugin instance. Overrides DerivativeInspectionInterface:: |
|
PluginBase:: |
public | function |
Gets the definition of the plugin implementation. Overrides PluginInspectionInterface:: |
3 |
PluginBase:: |
public | function |
Gets the plugin_id of the plugin instance. Overrides PluginInspectionInterface:: |
|
PluginBase:: |
public | function | Determines if the plugin is configurable. | |
PluginDependencyTrait:: |
protected | function | Calculates and adds dependencies of a specific plugin instance. | 1 |
PluginDependencyTrait:: |
protected | function | Calculates and returns dependencies of a specific plugin instance. | |
PluginDependencyTrait:: |
protected | function | Wraps the module handler. | 1 |
PluginDependencyTrait:: |
protected | function | Wraps the theme handler. | 1 |
SearchApiBaseFacetSource:: |
protected | property | The search result cache. | |
SearchApiBaseFacetSource:: |
public | function |
Form constructor. Overrides PluginFormInterface:: |
|
SearchApiBaseFacetSource:: |
public | function |
Retrieves the Search API display plugin associated with this facet source. Overrides SearchApiFacetSourceInterface:: |
|
SearchApiBaseFacetSource:: |
public | function |
Returns an array of fields that are defined on the facet source. Overrides FacetSourcePluginBase:: |
|
SearchApiBaseFacetSource:: |
public | function | Retrieves the query types for a specified data type. | |
SearchApiBaseFacetSource:: |
public | function |
Returns the allowed query types for a given facet for the facet source. Overrides FacetSourcePluginBase:: |
|
SearchApiBaseFacetSource:: |
public | function |
Retrieves the Views entity with the correct display set. Overrides SearchApiFacetSourceInterface:: |
|
StringTranslationTrait:: |
protected | property | The string translation service. | 1 |
StringTranslationTrait:: |
protected | function | Formats a string containing a count of items. | |
StringTranslationTrait:: |
protected | function | Returns the number of plurals supported by a given language. | |
StringTranslationTrait:: |
protected | function | Gets the string translation service. | |
StringTranslationTrait:: |
public | function | Sets the string translation service to use. | 2 |
StringTranslationTrait:: |
protected | function | Translates a string to the current language or to a given language. |