0

Sorry, the title is a bit of a mouthful but I will attempt to clarify.

I have a question about pathauto tokens to create a URL alias for a nested/hierarchical taxonomy.

Specifically, I have a taxonomy called Category, which is n layers deep; e.g:

Category level 01 > Category level 02 > Category level 03

And so on. The question appears to have been asked many times before and there are a number of good solutions, including the following pattern:

[term:parents:join:/]/[term:name]

This yields the following url alias for the above example:

category-level-01/category-level-02/category-level-03

Pretty straightforward. This would be perfect except in my example the taxonomy terms are in Chinese.

I need English in the path aliases, so I have set up a new field in my taxonomy called 'Name English'. This gives me the following replacement pattern:

[term:parents:join:/]/[term:field_name_english]

Which is almost perfect, but the parents default to using the name field, and there does not seem to be an easy way of overriding this. I cannot use the parent taxonomy terms with delta because it's not dynamic; e.g:

[term:parents-all:1:field_name_english]/[term:field_name_english]

Some taxonomy depths will be deeper than others.


So, in summary, I need to somehow specify an alternate term field to name for the [term:parents] token. To do this I assume I will need to do one of the following:

  1. Create a custom token
  2. Override the current [term:parents] token

Can anyone advise on this? Thanks a million.

1
+50

Option 1

You could copy the token created by the taxonomy module for [term:parents:join:/] and recreate it in your own module, using your field as a substitute for title.

This would require some familiarity with the token system. There is also a module entitled 'Custom Token' that might be worth a look, though I have not used it.

Option 2

One divergent way I could suggest to get around this is to use drupal's title field to store your english language title, and another field to store the chinese title (*field_chinese_title*).

You could then preprocess the node title within your theme/module to replace $variables['title'] with *field_chinese_title*. You might also have to do this for menu entries and search results, and you would also have to remember to use *field_chinese_title* for views fields etc.

YOURTHEME/template.php:

<?php
function YOURTHEME_process_page(&$variables) {
  if (isset($variables['node']->field_chinese_title['und'][0]['value'])) {
    $variables['title'] = $variables['node']->field_chinese_title['und'][0]['value'];
  }
}

function YOURTHEME_preprocess_search_result(&$variables) {
  // use chinese version of title for search results
  if (isset($variables['result']['node']->field_chinese_title)) {
    $variables['title'] = $variables['result']['node']->field_chinese_title['und'][0]['value']);
  }
}

I admit I have little experience of multi language sites, and I do realise that this method might look a little crude, but I have used a variation (with autonodetitle) to differentiate the titles used by menu entries and nodes (so that menu entries can optionally be more concise) and it works beautifully. For my use case, I later found a module for that...

  • Hi there. That's great, thanks for the feedback. I was originally thinking along the lines of option #1 - I am not very familiar with Drupal's internals, but I am proficient enough with PHP to do some overriding, if I can find the code in question. The only problem is that I haven't been able to pinpoint the [term:parents:join:/] code yet. – Darragh Enright Dec 10 '13 at 15:11
  • Option #2 is definitely food for thought - I'm just reading/digesting it now to see if I understand it properly :) – Darragh Enright Dec 10 '13 at 15:12
  • Actually, yes, I am populating the menu with taxonomy terms, using the Taxonomy Menu module. I guess, as you noted, I will have to override the menu hook as well then? A quick google would seem to suggest hook_menu_alter: drupal.stackexchange.com/a/51255/23717 – Darragh Enright Dec 10 '13 at 15:15
  • Thanks a million for the information - I ended up reneging on the nested taxonomy url alias in the end but this will prove useful in future and it was an all-round informative answer so cheers! – Darragh Enright Dec 13 '13 at 17:46
  • no problem, glad to be of some help – autopoietic Dec 15 '13 at 18:14

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.