You are here

public function SearchApiViewsFacetsBlockDisplay::render in Search API 7

Render this display.

Overrides views_plugin_display::render

1 call to SearchApiViewsFacetsBlockDisplay::render()
SearchApiViewsFacetsBlockDisplay::execute in contrib/search_api_views/includes/display_facet_block.inc
The display block handler returns the structure necessary for a block.

File

contrib/search_api_views/includes/display_facet_block.inc, line 182
Display plugin for displaying the search facets in a block.

Class

SearchApiViewsFacetsBlockDisplay
Plugin class for displaying search facets in a block.

Code

public function render() {
  if (substr($this->view->base_table, 0, 17) != 'search_api_index_') {
    form_set_error('', t('The "Facets block" display can only be used with base tables based on Search API indexes.'));
    return NULL;
  }
  $facet_field = $this
    ->get_option('facet_field');
  if (!$facet_field) {
    return NULL;
  }
  $this->view
    ->execute();
  if ($this
    ->get_option('hide_block')) {
    return NULL;
  }
  $results = $this->view->query
    ->getSearchApiResults();
  if (empty($results['search_api_facets']['search_api_views_facets_block'])) {
    return NULL;
  }
  $terms = $results['search_api_facets']['search_api_views_facets_block'];
  $filters = array();
  foreach ($terms as $term) {
    $filter = $term['filter'];
    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) . ']';
      }
    }
    $filters[$term['filter']] = $filter;
  }
  $index = $this->view->query
    ->getIndex();
  $options['field'] = $index->options['fields'][$facet_field];
  $options['field']['key'] = $facet_field;
  $options['index id'] = $index->machine_name;
  $options['value callback'] = '_search_api_facetapi_facet_create_label';
  $map = search_api_facetapi_facet_map_callback($filters, $options);
  $facets = array();
  $prefix = rawurlencode($facet_field) . ':';
  foreach ($terms as $term) {
    $name = $filter = $filters[$term['filter']];
    if (isset($map[$filter])) {
      $name = $map[$filter];
    }
    $query['f'][0] = $prefix . $filter;

    // Initializes variables passed to theme hook.
    $variables = array(
      'text' => $name,
      'path' => $this->view->query
        ->getOption('search_api_base_path'),
      'count' => $term['count'],
      'options' => array(
        'attributes' => array(
          'class' => 'facetapi-inactive',
        ),
        'html' => FALSE,
        'query' => $query,
      ),
    );

    // Override the $variables['#path'] if facetapi_pretty_paths is enabled.
    if (module_exists('facetapi_pretty_paths')) {

      // Get the appropriate facet adapter.
      $adapter = facetapi_adapter_load('search_api@' . $index->machine_name);

      // Get the URL processor and check if it uses pretty paths.
      $urlProcessor = $adapter
        ->getUrlProcessor();
      if ($urlProcessor instanceof FacetapiUrlProcessorPrettyPaths) {

        // Retrieve the pretty path alias from the URL processor.
        $facet = facetapi_facet_load($facet_field, 'search_api@' . $index->machine_name);
        $values = array(
          trim($term['filter'], '"'),
        );

        // Get the pretty path for the facet and remove the current search's
        // base path from it.
        $base_path_current = $urlProcessor
          ->getBasePath();
        $pretty_path = $urlProcessor
          ->getFacetPath($facet, $values, FALSE);
        $pretty_path = str_replace($base_path_current, '', $pretty_path);

        // Set the new, pretty path for the facet and remove the "f" query
        // parameter.
        $variables['path'] = $variables['path'] . $pretty_path;
        unset($variables['options']['query']['f']);
      }
    }

    // Themes the link, adds row to facets.
    $facets[] = array(
      'class' => array(
        'leaf',
      ),
      'data' => theme('facetapi_link_inactive', $variables),
    );
  }
  if (!$facets) {
    return NULL;
  }
  return array(
    'facets' => array(
      '#theme' => 'item_list',
      '#items' => $facets,
    ),
  );
}