You are here

public function Grouping::preprocessSearchQuery in Search API Grouping 8

Preprocesses a search query.

Parameters

\Drupal\search_api\Query\QueryInterface $query: The object representing the query to be executed.

Overrides FieldsProcessorPluginBase::preprocessSearchQuery

File

src/Plugin/search_api/processor/Grouping.php, line 145

Class

Grouping
Processor for grouping up items on behalf of user defined fields.

Namespace

Drupal\search_api_grouping\Plugin\search_api\processor

Code

public function preprocessSearchQuery(QueryInterface $query) {
  $grouping_fields = $this
    ->getGroupingFields();
  if (!empty($grouping_fields)) {

    // We move the options from our options array into where the Solr Service
    // is expecting them.
    $options = [
      'use_grouping' => TRUE,
      'grouping_fields' => $grouping_fields,
      'truncate' => isset($this->configuration['truncate']) ? $this->configuration['truncate'] : TRUE,
      'group_limit' => isset($this->configuration['group_limit']) ? $this->configuration['group_limit'] : NULL,
      'group_sort' => [],
    ];
    if (!empty($this->configuration['group_sort'])) {
      $options['group_sort'][$this->configuration['group_sort']] = isset($this->configuration['group_sort_direction']) ? $this->configuration['group_sort_direction'] : 'asc';
    }
    $query
      ->setOption('search_api_grouping', $options);
  }
}