When using Facetapi Pretty Paths to rewrite ?f[0], the Facet Blocks don't work with Facetapi Pretty Paths.

Support from Acquia helps fund testing for Drupal Acquia logo

Comments

Anonymous’s picture

Status: Active » Needs review
FileSize
1.47 KB

Attached a patch that rewrites the path. It's working for me, let me know what you think of it.

guillaumev’s picture

FileSize
1.44 KB

New patch that works for me (maybe after a few more releases of search_api).

cossimo’s picture

Issue summary: View changes

I know this is an old thread, but neither of the patches worked for me. 1818572-2 is close but makes the link relative to the page on which the facets block resides, instead relative to the search page it's supposed to reference. Since all my facets blocks are on node-based pages and have a consistent url pattern, it was easy enough to alter $variables['path'] to make it work.

However, while I'm including my solution below for reference, it is ugly, brittle, short-sited and not fit for general consumption. I'm hoping there's someone out there smarter than I am, who might come up with a better solution.

C.

      // Override the $variables['#path'] if facetapi_pretty_paths is enabled
      if (module_exists('facetapi_pretty_paths')) {
          
        // Get the facet by the field_name and readable name of searcher.
        $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'], '"'));
          
          // Re-write Pretty Facet Path, unset ['f']
          // cossimo: now for the ugly part....
          $variables['path2'] = explode('/', $urlProcessor->getFacetPath($facet, $values, FALSE));
          unset($variables['path2'][0]);
          unset($variables['path2'][1]);
	  $variables['path2'] = implode('/',$variables['path2']);					
          $variables['path'] .=  '/' . $variables['path2']; 
          unset($variables['options']['query']['f']);
        }
      }
Sakhmed’s picture

Finally :-) #3 worked for me!

Thank you a lot!

drunken monkey’s picture

Status: Needs review » Needs work

Hm, interesting. Thanks for the input!
Maybe try to ask in the Facet API Pretty Paths issue queue for help regarding how to change the base path used? That seems to be the only missing thing here. Otherwise, you could at least determine from current_path() how many leading path segments to unset() from the path created by the URL processor.

dasjo’s picture

a.dmitriiev’s picture

In addition to patch #2 it would be nice to have alias for taxonomy term:

// Override the $variables['#path'] if facetapi_pretty_paths is enabled
      if (module_exists('facetapi_pretty_paths')) {
          
        // Get the facet by the field_name and readable name of searcher.
        $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);
          $facetapi_pretty_path = $urlProcessor->getFacetPrettyPathsAlias($facet);
					
					$alias = $variables['text'];
					
					$searcher_settings = facetapi_get_searcher_settings('search_api@' . $index->machine_name);
					$facet_settings_key = 'search_api@' . $index->machine_name . '::' . $facet['field'];
			    if (isset($searcher_settings[$facet_settings_key]) && isset($searcher_settings[$facet_settings_key]->settings)) {
			      $facet_settings = $searcher_settings[$facet_settings_key];
			      if ($facet_settings->settings['query_type'] == 'term' && module_exists('pathauto')) {
			      	module_load_include('inc', 'pathauto', 'pathauto');
			        $alias = pathauto_cleanstring($variables['text']) . '-' . $filter;
			      }
			    }					          
          // Re-write Pretty Facet Path, unset ['f']
          $variables['path'] .= '/' . $facetapi_pretty_path . '/' . $alias;
          unset($variables['options']['query']['f']);
        }
      }
lodey’s picture

#2 works well, but we can then also get to the current base path value using the getBasePath() method and then remove it from the pretty path generated by getFacetPath() method using a str_replace.

This patch works well for me.

lodey’s picture

Status: Needs work » Needs review
drunken monkey’s picture

Thanks, looks good. Attached are just a few style improvements/nit-picks.

However, it would be good to get confirmation from some other people that this indeed works as intended and doesn't break anything. I don't completely understand the code and want to make sure that it works as intended for all use cases we currently support.

popstas’s picture

Patch #10 works for me, search_api 7.x-1.19

drunken monkey’s picture

Status: Needs review » Fixed

Good to hear, thanks for testing and reporting back!
Committed.

cossimo’s picture

The patch in #10 worked perfectly for me.

Thanks, and FYI,

C.

Status: Fixed » Closed (fixed)

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