Hello,

I am using Search Api with solr, views and facet api. And everything is working fine :)

What I want is to change the Views title of the search results according to the facet options that have been selected.

If the title of the views is "Articles" and the user selects, from "Categories" facet, the option "Software". The title must change to "Articles in Software".

Right now I change the title using "hook_views_pre_render" by taking the option from the URL. Pretty lame-non-drupal-way, I know....:)

Is there a hook that I can get the term ids of the options selected and being able to change views title?

Thanks!

Comments

drunken monkey’s picture

Category: task » support

You could look directly at the filters set on the Search API query object. You can get that by calling search_api_current_search(), or in hook_search_api_query_alter() (but that probably won't work since you can't really change the view title from there – you could set the page title, though).
Regarding the Facet API, I have to admit I don't really know what hooks it provides – either look yourself in the facetapi.api.php file or ask in the Facet API issue queue. Also, since the Facet API can change the breadcrumbs to reflect selected facets, maybe changing the page title would be possible in a similar way? (However, don't know if that's enough for you, since you write you want to change the view title …)

Hope that helps.

matrixlord’s picture

Thanks drunken monkey for the quick response,

I am using dsm(search_api_current_search()) but I can't get the actual values passed to search_api.

I am looking right now at current_search module that comes with facet_api. Maybe the answer for me is somewhere there.

drunken monkey’s picture

I am using dsm(search_api_current_search()) but I can't get the actual values passed to search_api.

The data you need isn't available through a dsm(), you have to use the appropriate methods on the query object. See the documentation in includes/query.inc, or this example code:

// $search_id is the "search id" option of the search query you are looking for.
// Call the function without parameters to obtain a list of all searches.
list($query, $results) = search_api_current_search($search_id);
list_all_filters($query->getFilter());

function list_all_filters(SearchApiQueryFilterInterface $filter) {
  foreach ($filter->getFilters() as $filter) {
    if (is_array($filter)) {
      list($field, $value, $operator) = $f;
      echo "$field $operator '$value'\n";
    }
    else {
      list_all_filters($f);
    }
  }
}

And somewhere in those filters will be the values passed to the Search API.

matrixlord’s picture

Thanks again drunken monkey,

I found how to get the selected facets. I wouldn't get very easy there by myself, so your help is appreciated!

drunken monkey’s picture

Status: Active » Fixed

Status: Fixed » Closed (fixed)

Automatically closed -- issue fixed for 2 weeks with no activity.

Sam152’s picture

If anyone finds this I solved it like this. I'd also be interested if there were any cleaner solutions.

/**
 * Get the value of a facet from the current search.
 *
 * If the facet is applied multiple times like a term heiarchy, an array of
 * value will be returned.
 *
 * @param array $facet
 *   The name of the facet to search for.
 * @return string
 *   The value to return.
 */
function fs_products_search_get_current_facet_value($facet) {
  $searches = search_api_current_search();
  if (sizeof($searches) < 1) {
    return FALSE;
  }
  $search = array_shift($searches);
  if (!isset($search[0]) || !is_object($search[0])) {
    return FALSE;
  }

  $filter_search = function($filters) use (&$facet) {
    $val = array();
    foreach ($filters->getFilters() as $filter) {
      if (is_array($filter)) {
        list($facet_name, $facet_value) = $filter;
        if ($facet_name == $facet) {
          $val[] = $facet_value;
        }
      } else {
        $val[] = $filter_search($filter);
      }
    }
    return sizeof($val) > 0 ? $val : FALSE;
  };

  return $filter_search($search[0]->getFilter());
}


/**
 * Implements hook_preprocess_page().
 */
function fs_preprocess_page(&$vars) {
  $category = fs_products_search_get_current_facet_value('field_product_category');
  if ($category) {
    $category_name = taxonomy_term_load(end($category))->name;
    drupal_set_title($category_name);
  }
}

cm_is’s picture

would be interested. where to put the code from #7 ?

ndenhild’s picture

Probably in template.php but i doesn't work for me!

ndenhild’s picture

@cm_is I have a work around.
Change youre view from renderd enity to using fields.
Then add the field Indexed node: rendered entity ánd the field product category. Hide this latest field. Place the product category as title trought the available token.

There you have it!

philipz’s picture

Issue summary: View changes

I've done this basing on #7 but little simpler code. Put this in a module. This might need some tweaking if there are facets other than taxonomy terms.

<?php
/**
 * Implements hook_views_pre_render
 */
function MODULE_NAME_views_pre_render(&$view) {
  if($view->name == 'PUT YOUR VIEW NAME HERE') {
    if ($searchers = facetapi_get_active_searchers()) {
      $terms_title = array();
      $searcher = reset($searchers);
      $adapter = facetapi_adapter_load($searcher);
      foreach ($adapter->getAllActiveItems() as $item) {
        $term = taxonomy_term_load($item['value']);
        $terms_title[] = $term->name;
      }
      $view->build_info['title'] = implode($terms_title, ' ');
    }
  }
}
?>
leahtard’s picture

#11 was just what I needed. Thanks!

youlikeit’s picture

Hi #11 worked for me just fine but looking in the logs I get
Notice: Trying to get property of non-object in modulename_views_pre_render() (line 13
and my cache is growing much to fast
pleas advise

ShaneOnABike’s picture

Thanks for this! Just to include an upgrade that includes views exposed filters if you have any ;) I also included a check to make sure that if there are no results just eliminate setting the title.


/**
 * Implements hook_views_pre_render
 */
function YOUR_MODULENAME_views_pre_render(&$view) {
  if($view->name == 'yourview' && $view->current_display = 'page') {
    $search_values = array();

    // Check the facets
    if ($searchers = facetapi_get_active_searchers()) {
      $searcher = reset($searchers);
      $adapter = facetapi_adapter_load($searcher);
      foreach ($adapter->getAllActiveItems() as $item) {
        $term = taxonomy_term_load($item['value']);
        $search_values[] = $term->name;
      }
    }
    // Check the views exposed filters
    if ($filters = array_filter($view->exposed_input)) {
      $search_values = array_merge($search_values, $filters);
    }
    if (!empty($search_values)) {
     $view->build_info['title'] = '<h3 class="search-result">Results for</h3><h2 class="search-values">' .
       implode($search_values, ', ') . '</h2>';
    } else $view->build_info['title'] = '';
  }
}