3

For various reasons, I need to change my custom path alias for a taxonomy type as follows...

FROM: /composers/aardvark

TO: composers/a/aardvark

So I'm looking at either creating a custom token, or a computed field to figure out the first letter of the term and feed into the pathauto module...

I'm having trouble figuring out either... Could anyone suggest the correct code to use for either of these? Most of the examples I am finding are node specific, and not taxonomy term specific...

The reason I'm looking to do this, is to provide a contextual filter to a views block... But if anyone could suggest a good way to pass 'a' from 'aardvark' into the block - so that both of these paths passes the same contextual filter, that would be equally good!

/composers/a

/composers/aardvark

Thanks in advance for your help!

0

There are many ways to achieve this, as you said.

The less-coding way I see is to use Custom Token and create for each node a token using the following code :

return substr($node->title, 0);

Then you use this token in your URL alias pattern like [yournewtoken]/[node:title].

If you have a multi-lingual site, the first letter might not be the same in all languages, you may update this code to fit this.

  • Thanks Gregory. I tried your suggestion, but as my entity is a taxonomy term and not a node I couldn't quite get it working (e.g. with $term->name and other permutations)... So then I returned to the Computed Field route and got it working as follows... $name = field_get_items('taxonomy_term', $entity, 'name'); $n = substr($name,0,1); $n = strtolower($n); $entity_field[0]['value'] = $n; – Simon Doyle May 6 '13 at 11:17
  • Good one ! Glad it worked. – Gregory Kapustin May 6 '13 at 12:04
0

I found this page searching for a solution to add the first character (letter) of the node title in the breadcrumb path. The full and correct code should be

$node = node_load(arg(1));
return substr($node->title, 0);

See the screen shot

Screenshot settings

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.