Currently the alias for taxonomy terms is generated with <alias>/<term-name>-<term-id>, leading to ugly ids in pretty paths.

I've created a patch (or prototype) that re-uses the path alias of a term page and thus avoids term ids. The handler requires the default path pattern for taxonomy terms, which is [term:vocabulary]/[term:name]. Then the [term:name] part will be extracted and re-used in the facet alias, leading to [facet:alias]/[term:name].

I've only tested it with Search API fields, no idea if the Apache Solr module works differently ;-)

Furthermore, once the patch has been applied, the setting "Re-use term aliases for taxonomy term fields" needs to be activated in the Facet API Pretty Paths settings and the cache needs to be cleared.

Comments

mh86’s picture

nick_vh’s picture

Status: Needs review » Needs work
+++ b/plugins/coders/facetapi_pretty_paths_coder_taxonomy_pathauto.incundefined
@@ -0,0 +1,88 @@
+    $voc_alias = $this->getVocbularyPathAlias($args['facet']);

Vocbulary? typo ;-)

+++ b/plugins/coders/facetapi_pretty_paths_coder_taxonomy_pathauto.incundefined
@@ -0,0 +1,88 @@
+        // The Search API chains fields with ':', use the original field to

pretty paths should work for both so specific search api logic would undo that.

Apachesolr takes the regular field as 1 facet value. Can you perhaps make this explode optional? Is there a way you can make this work both ways?

mh86’s picture

So the Apache Solr module writes something like:

$facet_info['field'] => 'field_tags',

?

Then the explode function won't do any harm. If the ':' delimiter is not found, the explode function will return the whole string in the first array value.

mh86’s picture

Status: Needs work » Needs review
StatusFileSize
new6.75 KB

Updated patch that just fixes the typo

nick_vh’s picture

Looks good overall, I'd still change the comment so it does not appear as if only search api dependent.

dasjo’s picture

hi, thanks for figuring that out :)

+++ b/facetapi_pretty_paths.moduleundefined
@@ -129,6 +129,14 @@ function facetapi_pretty_paths_admin_form($form, &$form_state) {
+        '#description' => t('If set the term alias from the pathauto settings will be re-used, which avoids term ids in the facet aliases. This setting only works if the default taxonomy path pattern \'[term:vocabulary]/[term:name]\' is used. Changing the value of this checkbox requires a cache clear.'),

i'd say "If set, the term alias..."

can't give it a throughout review atm. would be great if somebody else could provide feedback

mh86’s picture

Just realized that the Search API field name chains are a bit more complex than I thought. A chain might be: field collection -> taxonomy field -> parents, or taxonomy field -> taxonomy field (field on taxonomy term).
So the last taxonomy field in this chain will lead to the correct vocabulary.

Updated patch that:
* fixes the extraction of term fields in Search API field name chains
* adds a comment for Apache Solr
* clears Facet API cache after the pretty path admin settings form has been saved

mh86’s picture

Reworked my last patch that allows you to activate the new taxonomy coder per facet level. Additionally the according vocabulary can be selected, in case it can't be retrieved from the field settings.

With the Search API you can index taxonomy properties that are not Field API fields. For these properties no vocabulary information exists. Furthermore certain fields or properties might be based on multiple vocabularies, so the new coder won't work anyway. That's why I think a facet setting makes more sense than a global setting.

mh86’s picture

I've learned that loading the adapter for retrieving the facet settings within hook_facetapi_facet_info_alter() is evil as it already builds the facet settings and statically caches them, without including the alterations of the Facet API Pretty Paths module (and maybe others). Thus we have to clear the static cache of facetapi_get_enabled_facets() at the end.
Alternatively we could try to directly load the facet settings via the CTools API.

dasjo’s picture

+        // Get the alias for this term and extract the term name alias part.
+        $alias = drupal_lookup_path('alias', 'taxonomy/term/' . $term->tid);
+        if ($alias) {
+          $parts = explode('/', $alias);
+          if (count($parts) == 2) {
+            $args['segment']['value'] = $parts[1];
+          }

would be great to document what exactly is going on here: something like taxonomy/term/id is mapped to [facet alias]/[term-name alias] and thus the check count($parts) == 2

+++ b/plugins/coders/facetapi_pretty_paths_coder_taxonomy_pathauto.incundefined
@@ -0,0 +1,79 @@
+      // Rebuild the term alias, get the source and extract the term id.
+      $source = drupal_lookup_path('source', $voc_alias . '/' . $args['value']);
+      if ($source) {
+        $exploded = explode('/', $source);
+        if (count($exploded) == 3) {
+          $args['value'] = $exploded[2];

vice versa here :)

mh86’s picture

Slightly improved comments according to #6 and #10.

Thanks for the reviews :-)

dasjo’s picture

Status: Needs review » Fixed

Status: Fixed » Closed (fixed)

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

Anonymous’s picture

Issue summary: View changes

Fixed <alias>/<term-name>-<term-id>