Currently using Views + search_api + facet_api to build a classified ads system. This works great except for the fact that a fulltext search forgets about all the set facets.

On the main page of the system the user can browse by selecting a specific category, for example 'Cars'. On the resulting search results page all ads within this category are shown including several facets.

Now if you enter a search term (exposed filter in Views) and hit 'Search' the selected facets (category = cars) is completely forgotten and a new search is started for your keyword. One would expect that the fulltext search is added to the filters/facets that are already active.

What would be the best way to solve this? Our current quick hack is adding a hidden field to the Views form to add all existing URL query parameters but that's hardly a longtime solution.

Comments

alexverb’s picture

Status: Active » Closed (duplicate)

At the moment there's no solution to remember facet settings. There is a feature request that is moved to Facet API Extra at http://drupal.org/node/1380716.

I think the best approach to remember your facets is the same way views remembers exposed filters. By storing it in $_SESSION['views'][$this->view->name][$display_id]. Maybe this way you can even combine the facets and exposed filters to be remembered.

Ps: I'm also working on a classified ads system, looks like this is a "must have" for classifieds systems. I'm willing to pitch in on a project if someone wants to take on the feature request. Even though I'm pretty scared of views code :).

I suggest we take this discussion to the feature request since it's not really a bug.

jtbayly’s picture

Component: Views integration » User interface
Category: bug » feature
Status: Closed (duplicate) » Active

According to this comment on the above linked issue, this feature should not be implemented in facet api.

It seems to me, given what I've read, that that means it should be implemented here. Marking as active feature request, and moving to the UI component, although I'm not sure that's where it belongs.

-Joseph

jtbayly’s picture

At one point (maybe still, I have no idea) this was supported in the apachesolr module with a simple checkbox next to the search box that said "Keep current filters" or something like that. Seems pretty simple to implement to me... but I can't at the moment.

Is anybody else interested?

-Joseph

luo8’s picture

Hello,
please..did you anybody solve it?
Thanks.

jtbayly’s picture

Still waiting. Seems pretty basic to me. Can't work on it myself.
-Joseph

Ravenight’s picture

I would love to see this too. Since it is not ready I did what BartVB did by adding a hidden field to the Views form to add all existing URL query parameters.

Since I had to futz with it for more than a few minutes to get it to work, I thought I would post back what I did for reference in case someone else needs this before a more permanent fix can be implemented.

I added this to the template file:

/**
* Implements hook_FORM_ID_alter()
*/
function HOOK_form_views_exposed_form_alter(&$form, $form_state, $form_id) {
  $view = $form_state['view'];
  if( isset($_GET['f']) && $view->name == 'THE_NAME_OF_YOUR_VIEW') {
    $values = $_GET['f'];
    $i=0;
    foreach($values as $value){
      $form['f['.$i.']'] = array(
        '#value' => $value,
        '#type' => 'hidden',
        '#weight' => '50',
      );
      $i++;
    }
  }
}

Not the best workaround, but it does work.

luo8’s picture

Dear all,
problem was solve for "Search api" module need to be installed module "Facetapi_pretty_paths" (http://drupal.org/project/facetapi_pretty_paths),check http://drupal.org/node/1826576 .

merilainen’s picture

Issue summary: View changes

#7 is not a solution per se, it's an alternate solution. In our case we need to stay in same /search path and use parameters, so Pretty paths is not an option because it will spread the search to sub paths like /search/parameter1/value1/parameter2/value2 etc.

drunken monkey’s picture

Component: User interface » Views integration

I think this issue would be better suited for the "Views" module's issue queue, as this is a problem of / feature request for Views in general, not just the Search API. But please search in the issue queue there first, maybe there's already something about this.

mpp’s picture

Version: 7.x-1.x-dev » 8.x-1.x-dev

A similar solution would work for Drupal 8:


/**
 * Implements hook_form_FORM_ID_alter().
 */
function mymodule_form_views_exposed_form_alter(&$form, \Drupal\Core\Form\FormStateInterface $form_state, $form_id) {
  /**
   * SITUATION: Search forms with facet filters and keywords search.
   * PROBLEM: When searching for a keyword after using a facet filter, the
   * facet filter is ignored by Views.
   * SOLUTION: Add facet filters as hidden form fields to Views exposed forms.
   */

  // Only apply for search view.
  if ($form_state->get('view')->id() !== 'search') {
    return;
  }

  // Only apply if any facet query parameters are provided.
  $facet_query = \Drupal::request()->query->filter('f');
  if (empty($facet_query)) {
    return;
  }

  // Iterate through facet query parameters.
  foreach ($facet_query as $key => $value) {
    // Add hidden form field for facet parameter.
    $form['f[' . $key . ']'] = array(
      '#type' => 'hidden',
      '#value' => $value,
      '#weight' => -1,
    );
  }
}
mark_fullmer’s picture

I can confirm that the D8 solution in #10 works for me. Do note, though, that if your facet configuration defines a different string than "f" for the query parameter key, you'll need to adjust that in your code. The below provides a solution where you can act on one or search_api views + facets, supplying the different facet query parameters:

use \Drupal\Core\Form\FormStateInterface;

/**
 * Implements hook_form_FORM_ID_alter().
 */
function mymodule_form_views_exposed_form_alter(&$form, FormStateInterface $form_state, $form_id) {

  // SITUATION: Search forms with facet filters and keywords search.
  // PROBLEM: When searching for a keyword after using a facet filter, the
  // facet filter is ignored by Views.
  // SOLUTION: Add facet filters as hidden form fields to Views exposed forms.
  $views = [
    'FORM ID HERE' => 'FACET QUERY KEY HERE',
    'FORM ID HERE' => 'FACET QUERY KEY HERE',
  ];
  $id = $form_state->get('view')->id();
  // Only apply for search views.
  if (!in_array($id, array_keys($views))) {
    return;
  }
  $parameter = $views[$id];
  // Only apply if any facet query parameters are provided.
  $facet_query = \Drupal::request()->query->filter($parameter);
  if (empty($facet_query)) {
    return;
  }

  // Iterate through facet query parameters.
  foreach ($facet_query as $key => $value) {
    // Add hidden form field for facet parameter.
    $form[$parameter . '[' . $key . ']'] = array(
      '#type' => 'hidden',
      '#value' => $value,
      '#weight' => -1,
    );
  }
}
watergate’s picture

Thanks for the code snippet!

idebr’s picture

idebr’s picture

In this Drupal Core issue #2823541: Table clicksort is lost when using views exposed filter & Pager exposed '#items', the Views exposed form maintains existing query parameters when searching. This would fix the issue reported here.

mkalkbrenner’s picture

I wonder if this is not already solved by #2378945: Facets are lost by Views on keyword search within Search API.

drunken monkey’s picture

Status: Active » Closed (duplicate)

Seems like it, yes. Thanks!