You are here

abstract class QueryTypeRangeBase in Facets 8

A base class for query type plugins adding range.

Hierarchy

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\QueryType
View 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

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.
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.
QueryTypePluginBase::$facet protected property The facet that needs the query type.
QueryTypePluginBase::$linkGenerator protected property The injected link generator.
QueryTypePluginBase::$query protected property The backend native query object.
QueryTypePluginBase::$results protected property The results for the facet.
QueryTypePluginBase::calculateDependencies public function Calculates dependencies for the configured plugin. Overrides DependentPluginInterface::calculateDependencies
QueryTypePluginBase::defaultConfiguration public function Gets default configuration for this plugin. Overrides ConfigurableInterface::defaultConfiguration
QueryTypePluginBase::getConfiguration public function Gets this plugin's configuration. Overrides ConfigurableInterface::getConfiguration
QueryTypePluginBase::getFacetOptions protected function Builds facet options that will be send to the backend. 1
QueryTypePluginBase::setConfiguration public function Sets the configuration for this plugin instance. Overrides ConfigurableInterface::setConfiguration
QueryTypePluginBase::__construct public function Constructs a \Drupal\Component\Plugin\PluginBase object. Overrides PluginBase::__construct 1
QueryTypeRangeBase::build public function Builds the facet information, so it can be rendered. Overrides QueryTypeInterface::build 1
QueryTypeRangeBase::calculateRange abstract public function Calculate the range for a given facet filter value. 2
QueryTypeRangeBase::calculateResultFilter abstract public function Calculate the grouped facet filter for a given value. 2
QueryTypeRangeBase::execute public function Adds facet info to the query using the backend native query object. Overrides QueryTypeInterface::execute
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.