I have a View with contextual filters. I filter by Content Has Taxonomy ID. The problem is that the website is multilanguage, and the name of the taxonomy may change, have commas, non-common characters (cyrillic, for example), be too long and could be, in fact, different from the friendly URL.

My idea (which I have used in other projects) is to have the URL in this way:




and from the URL, retrieve the parameters with

$url_arr = explode("/", $_SERVER['REQUEST_URI']);

In this case, $url_arr[2] would contain the taxonomy description(sport-shoes or спортивная-обувь in Russian), which is what I look for. The taxonomy name could be completely different, for example: "Sport shoes, complements, and other wearables".

Then I make a query to the database with db_query() searching by description field ( or better use a EntityFieldQuery), get the ID (the product will be associated to both taxonomy terms and I only need one), and pass the ID to the contextual filter, instead of letting the module Views to guess the ID from the URL (Term name converted to Term ID).

Is this procedure (which works) too convoluted? Is there a simpler and elegant way of doing this?

1 Answer 1


Retrieving the term ID by name has one considerable downside: If term names change, links may become invalid.

A safer option would be the use of aliases. For each of your terms, generate a URL alias per language that points to the internal view path (containing the term IDs). That way your view won't require custom logic to retrieve the term ID.

You could then expand on that approach and use e.g. the Redirect module to keep track of alias changes.

If you still want to stick to your approach, keep in mind that Drupal provides various functions to deal with paths and arguments. In your case, that one-liner can be simplified to:

$url_arr = arg();
  • Thanks for your answer. However, if there are thousand of possible combinations, for example, /[brand]/[product-category]/[store-location], etc., it can be very difficult to manage. This is why I let the complexity at the level of product assignation (database) and don't go further into another table - "url_alias". That way the URLs are "live", the content is created on the fly. I think it is cleaner. However, if Views would let you choose the field to link to the tid, then I could choose "description" instead of "name" and I would not need the extra source code (EntityFieldQuery).
    – Cesar
    Oct 2, 2015 at 14:23
  • You would create and update your aliases in code, not manually. There are several hooks avaiable (e.g. hook_taxonomy_term_insert()/_update()/_delete() or the more general hook_entity_* counterparts), and paths can be created and updated via path_save(). Another option might be the use of Rules to maintain your aliases. Oct 2, 2015 at 14:34
  • But then I am using several other modules or functions, so now it is a matter of choice, I use only 10 lines of code for all the process, I guess with path_save() and what you have in mind it would be more or less the same. It all came from the original data format, which was in separate (non-Drupal) tables, so I could make direct db_query. Later I incorporated the products into nodes, taxonomies, etc., by using Feeds. Now I found my original code somehow clumsy. In any case, even for a simple structure, you still have to use code if you don't want to create manually dozens of alias.
    – Cesar
    Oct 2, 2015 at 14:49
  • @Cesar Updated my answer. Just keep in mind that SEO is an important aspect, and as such links should not change at random without providing a redirect target. Oct 2, 2015 at 14:57
  • Yes, I thought of SEO too. For sitemap.xml, a SQL could create the links. But then you are right that maybe it would be better if those URLs would exist in the system... I will try your approach with path_save(). Thanks also for your suggestion of arg(), I had used it in the code of autocomplete search. Thanks for reminding me about it.
    – Cesar
    Oct 2, 2015 at 15:02

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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