I'm using Views Facets, and I have a current search block resetting filters in my facet blocks. I have a search block that is a Views exposed filter on my index (for title). How can I get my reset filters link to also clear this Views exposed block?

Or, is there a text search functionality that I can use instead of a Views exposed filter?

Support from Acquia helps fund testing for Drupal Acquia logo

Comments

azinck’s picture

Status: Active » Needs review
FileSize
3.09 KB

This is tricky since I can't see a way to reset the views exposed filters in an intelligent fashion without wiping out all querystrings. For my use-cases, however, this is ok, so this patch adds an option to do a "complete reset" which will wipe out all querystrings.

azinck’s picture

ptmkenny’s picture

Category: Support request » Feature request

Another "quick and dirty" way to do this if you're using Facet API Pretty Paths is simply to add a text link back to the root path of the view and call it "reset filters." Going back to the root path will wipe out all the Views exposed filters.

PQ’s picture

If you know exactly what fields you want to unset and don't want to clobber everything on the query string, and are willing and able to put some code in a custom module then you can do the following:

  • Duplicate plugins/facetapi/current_search_reset_filters_link.inc from the facetapi_bonus module folder into your own module folder
  • Rename it as something likecurrent_search_reset_filters_and_search_link.inc (my use case involved a search terms exposed filter so my terminoligy will reflect that)
  • Edit the file and change the class name to something like MyModuleCurrentSearchResetFiltersAndSearchLink
  • Add the following before $variables = array(...:
    unset($query['search']);
    

    ...where 'search' is the key that appears in the query string when you use the exposed filter. Add multiple lines if you have multiple exposed filters.

    The whole file should look something like this:

    
    /**
     * @file
     * Current Search plugin to provide Reset filters and search link.
     */
    
    class MyModuleCurrentSearchResetFiltersAndSearchLink extends CurrentSearchItemText {
      /**
       * Implements CurrentSearchItem::execute().
       */
      public function execute(FacetapiAdapter $adapter) {
        $data = array('facetapi_adapter' => $adapter);
    
        // Determines plurality of string.
        if ($this->settings['plural']) {
          $condition = '[' . $this->settings['plural_condition'] . ']';
          $count = (int) token_replace($condition, $data);
          $raw_text = ($count != 1) ? $this->settings['text_plural'] : $this->settings['text'];
        }
        else {
          $raw_text = $this->settings['text'];
        }
    
        // Translates text, returns themed output.
        $translated_text = $this->translate('text', $raw_text);
    
        // Makes sure facet builds are initialized.
        $adapter->processFacets();
    
        $query = array();
        $path = '';
    
        // Build intesection of all filters. This will remove all filters.
        foreach ($adapter->getAllActiveItems() as $item) {
          if (empty($query)) {
            $query = $this->getQueryString($item, $adapter);
          }
          else {
            $facet_query = $this->getQueryString($item, $adapter);
            if (isset($query['f'])) {
              $query['f'] = array_intersect($query['f'], $facet_query['f']);
            }
          }
          // Save the path.
          if (empty($path)) {
            $path = $this->getFacetPath($item, $adapter);
          }
        }
    
        // If no facets were selected.
        if (empty($path)) {
          return;
        }
    
        // The following line is the only deviation from
        // FacetapiBonusCurrentSearchResetFiltersLink.
        // Unset the search key from the query string.
        unset($query['search']);
    
        $variables = array(
          'text' => l(token_replace($translated_text, $data), $this->getFacetPath($item, $adapter), array('query' => $query)),
          'wrapper' => $this->settings['wrapper'],
          'element' => $this->settings['element'],
          'css' => $this->settings['css'],
          'class' => current_search_get_classes($this->settings['classes'], $data),
          'options' => array('html' => TRUE),
        );
        return array('#markup' => theme('current_search_text', $variables));
      }
    }
    
  • Add the following to your module's .info file:

    files[] = current_search_reset_filters_and_search_link.inc

    (Make sure the filename is the same as the one you made earlier.

  • Add the following to your module's .module file:
    /**
     * Implements hook_current_search_items().
     */
    function MY_MODULE_current_search_items() {
      return array(
        'reset_filters_and_search' => array(
          'handler' => array(
            'label' => t('Reset filters and search link'),
            'class' => 'MyModuleCurrentSearchResetFiltersAndSearchLink',
          ),
        ),
      );
    }
    

    Change "MY_MODULE" to your module name.

  • Flush caches
  • Edit the current search block, delete the "Reset filter link" item and add a "Reset filters and search" item and set it's options up.

This should then work exactly the same as before but also strip out the query from the exposed filter(s) when clicked.