abstract class QueryTypeRangeBase in Facets 8
A base class for query type plugins adding range.
Hierarchy
- class \Drupal\Component\Plugin\PluginBase implements DerivativeInspectionInterface, PluginInspectionInterface
- class \Drupal\Core\Plugin\PluginBase uses DependencySerializationTrait, MessengerTrait, StringTranslationTrait
- class \Drupal\facets\QueryType\QueryTypePluginBase implements ConfigurableInterface, DependentPluginInterface, QueryTypeInterface uses DependencyTrait
- class \Drupal\facets\QueryType\QueryTypeRangeBase
- class \Drupal\facets\QueryType\QueryTypePluginBase implements ConfigurableInterface, DependentPluginInterface, QueryTypeInterface uses DependencyTrait
- class \Drupal\Core\Plugin\PluginBase uses DependencySerializationTrait, MessengerTrait, StringTranslationTrait
Expanded class hierarchy of QueryTypeRangeBase
2 files declare their use of QueryTypeRangeBase
- SearchApiDate.php in src/
Plugin/ facets/ query_type/ SearchApiDate.php - SearchApiGranular.php in src/
Plugin/ facets/ query_type/ SearchApiGranular.php
File
- src/
QueryType/ QueryTypeRangeBase.php, line 10
Namespace
Drupal\facets\QueryTypeView source
abstract class QueryTypeRangeBase extends QueryTypePluginBase {
/**
* {@inheritdoc}
*/
public function execute() {
$query = $this->query;
// Alter the query here.
if (!empty($query)) {
$options =& $query
->getOptions();
$operator = $this->facet
->getQueryOperator();
$field_identifier = $this->facet
->getFieldIdentifier();
$exclude = $this->facet
->getExclude();
$options['search_api_facets'][$field_identifier] = $this
->getFacetOptions();
// Add the filter to the query if there are active values.
$active_items = $this->facet
->getActiveItems();
$filter = $query
->createConditionGroup($operator, [
'facet:' . $field_identifier,
]);
if (count($active_items)) {
foreach ($active_items as $value) {
$range = $this
->calculateRange($value);
$conjunction = $exclude ? 'OR' : 'AND';
$item_filter = $query
->createConditionGroup($conjunction, [
'facet:' . $field_identifier,
]);
$item_filter
->addCondition($this->facet
->getFieldIdentifier(), $range['start'], $exclude ? '<' : '>=');
$item_filter
->addCondition($this->facet
->getFieldIdentifier(), $range['stop'], $exclude ? '>' : '<=');
$filter
->addConditionGroup($item_filter);
}
$query
->addConditionGroup($filter);
}
}
}
/**
* Calculate the range for a given facet filter value.
*
* Used when adding active items in self::execute() to $this->query to include
* the range conditions for the value.
*
* @param string $value
* The raw value for the facet filter.
*
* @return array
* Keyed with 'start' and 'stop' values.
*/
public abstract function calculateRange($value);
/**
* {@inheritdoc}
*/
public function build() {
// If there were no results or no query object, we can't do anything.
if (empty($this->results)) {
return $this->facet;
}
$query_operator = $this->facet
->getQueryOperator();
$facet_results = [];
foreach ($this->results as $result) {
// Go through the results and add facet results grouped by filters
// defined by self::calculateResultFilter().
if ($result['count'] || $query_operator == 'or') {
$count = $result['count'];
if ($result_filter = $this
->calculateResultFilter(trim($result['filter'], '"'))) {
if (isset($facet_results[$result_filter['raw']])) {
$facet_results[$result_filter['raw']]
->setCount($facet_results[$result_filter['raw']]
->getCount() + $count);
}
else {
$facet_results[$result_filter['raw']] = new Result($this->facet, $result_filter['raw'], $result_filter['display'], $count);
}
}
}
}
$this->facet
->setResults($facet_results);
return $this->facet;
}
/**
* Calculate the grouped facet filter for a given value.
*
* @param string $value
* The raw value for the facet before grouping.
*
* @return array
* Keyed by 'display' value to be shown to the user, and 'raw' to be used
* for the url.
*/
public abstract function calculateResultFilter($value);
}
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. | |
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. | |
QueryTypePluginBase:: |
protected | property | The facet that needs the query type. | |
QueryTypePluginBase:: |
protected | property | The injected link generator. | |
QueryTypePluginBase:: |
protected | property | The backend native query object. | |
QueryTypePluginBase:: |
protected | property | The results for the facet. | |
QueryTypePluginBase:: |
public | function |
Calculates dependencies for the configured plugin. Overrides DependentPluginInterface:: |
|
QueryTypePluginBase:: |
public | function |
Gets default configuration for this plugin. Overrides ConfigurableInterface:: |
|
QueryTypePluginBase:: |
public | function |
Gets this plugin's configuration. Overrides ConfigurableInterface:: |
|
QueryTypePluginBase:: |
protected | function | Builds facet options that will be send to the backend. | 1 |
QueryTypePluginBase:: |
public | function |
Sets the configuration for this plugin instance. Overrides ConfigurableInterface:: |
|
QueryTypePluginBase:: |
public | function |
Constructs a \Drupal\Component\Plugin\PluginBase object. Overrides PluginBase:: |
1 |
QueryTypeRangeBase:: |
public | function |
Builds the facet information, so it can be rendered. Overrides QueryTypeInterface:: |
1 |
QueryTypeRangeBase:: |
abstract public | function | Calculate the range for a given facet filter value. | 2 |
QueryTypeRangeBase:: |
abstract public | function | Calculate the grouped facet filter for a given value. | 2 |
QueryTypeRangeBase:: |
public | function |
Adds facet info to the query using the backend native query object. Overrides QueryTypeInterface:: |
|
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. |