You are here

public function SearchApiFacetapiTerm::build in Search API 7

Initializes the facet's build array.

Return value

array The initialized render array.

1 method overrides SearchApiFacetapiTerm::build()
SearchApiFacetapiDate::build in contrib/search_api_facetapi/plugins/facetapi/query_type_date.inc
Initializes the facet's build array.

File

contrib/search_api_facetapi/plugins/facetapi/query_type_term.inc, line 195
Term query type plugin for the Apache Solr adapter.

Class

SearchApiFacetapiTerm
Plugin for "term" query types.

Code

public function build() {
  $facet = $this->adapter
    ->getFacet($this->facet);

  // The current search per facet is stored in a static variable (during
  // initActiveFilters) so that we can retrieve it here and get the correct
  // current search for this facet.
  $search_ids = drupal_static('search_api_facetapi_active_facets', array());
  $facet_key = $facet['name'] . '@' . $this->adapter
    ->getSearcher();
  if (empty($search_ids[$facet_key]) || !search_api_current_search($search_ids[$facet_key])) {
    return array();
  }
  $search_id = $search_ids[$facet_key];
  list(, $results) = search_api_current_search($search_id);
  $build = array();

  // Always include the active facet items.
  foreach ($this->adapter
    ->getActiveItems($this->facet) as $filter) {
    $build[$filter['value']]['#count'] = 0;
  }

  // Then, add the facets returned by the server.
  if (isset($results['search_api_facets']) && isset($results['search_api_facets'][$this->facet['name']])) {
    $values = $results['search_api_facets'][$this->facet['name']];
    foreach ($values as $value) {
      $filter = $value['filter'];

      // As Facet API isn't really suited for our native facet filter
      // representations, convert the format here. (The missing facet can
      // stay the same.)
      if ($filter[0] == '"') {
        $filter = substr($filter, 1, -1);
      }
      elseif ($filter != '!') {

        // This is a range filter.
        $filter = substr($filter, 1, -1);
        $pos = strpos($filter, ' ');
        if ($pos !== FALSE) {
          $filter = '[' . substr($filter, 0, $pos) . ' TO ' . substr($filter, $pos + 1) . ']';
        }
      }
      $build[$filter] = array(
        '#count' => $value['count'],
      );
    }
  }
  return $build;
}