TransWikia.com

How to change path of taxonomy facet

Drupal Answers Asked by rahulkumar.it on January 24, 2021

I am using facet search on my site. The search paths for taxonomy term facets are of the pattern “search/term-name”, but I would like them to be of the pattern “search/term-tid”.

Is this possible? How can I change this?

One Answer

Please try this:

/**
 * Implements hook_facetapi_searcher_info_alter().
 */
function mymodule_facetapi_searcher_info_alter(array &$searcher_info) {
  foreach ($searcher_info as &$info) {
    $info['url processor'] = 'term_facet_url';
  }
}

Create a folder inside your mymodule directory, and name it facetapi.

Next is create an .inc file and name it, url_processor_term_facet_url

So you should have this structure:

mymodule/facetapi/url_processor_term_facet_url.inc

Inside of your url_processor_term_facet_url.inc, add the following code.

<?php

/**
 * @file
 * A custom URL processor.
 */

/**
 * Extension of FacetapiUrlProcessor.
 */
class FacetapiUrlProcessorTermFacetUrl extends FacetapiUrlProcessorStandard {

  public function getQueryString(array $facet, array $values, $active) {
    $params = $this->params;
    unset($params[$this->filterKey]);
    return $params;
  }

  public function getFacetPath(array $facet, array $values, $active) {
    // Change the line below to your desired taxonomy term path, currently it
    // is set to 'taxonomy/term/[tid]'. And also make sure only taxonomy terms
    // are affected. Check other parameters to pull the data you might need.
    $path = 'taxonomy/term/' . $values[0];
    return $path;
  }

}

I hope it helps.

Answered by adriancruz on January 24, 2021

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP