4

I have a grade vocabulary with a 6. Class taxonomy term. The taxonomy page have an URL like www.example.com/foo/6-class/bar and I can't find the taxonomy with the following code.

$grade = arg(2);
$term = taxonomy_get_term_by_name($grade)

It works when I use a string (e.g. 6. Class), but not with arg(2). How can I get the correct taxonomy with this URL alias? Problem might be, that it is a exposed filter and no contextual filter. But I can not change this fact.

Neither I can get the source path with the following code.

drupal_lookup_path('source', $_GET['q']);
drupal_lookup_path('alias', current_path());
1
  • when you try to get arg(2) , what getting returned for you? ( term id or something like 6-class?
    – Yuseferi
    Jul 18 '16 at 8:04
3

I know this is a pretty old question but seems unanswered yet. This Drupal 7 snippet will help you to load a taxonomy term but you need to know the name of the vocabulary it belongs to, lets say it is 'position'. Ex. www.example.com/foo/6-class/bar

$term_path=drupal_get_normal_path('position/'.arg(1));
$tid = basename($term_url);
$term = taxonomy_term_load($tid);

If you know your url is an alias to your taxonomy term, just use

$term_path=drupal_get_normal_path(current_path());
$tid = basename($term_url);
$term = taxonomy_term_load($tid);

The drupal_lookup_path with source and uri approach did not work for you because probably that path is not an alias, or at least not for your taxonomy.

Hope this helps someone.

2

I was working in Drupal 8 and for whatever reason I couldn't get any of the above solutions to work for me. Below is what I ended up using to get the url with the term id. There must better ways of doing this and I'm sure that not all of these options are required, but I hope this helps someone.

$term_url = \Drupal\Core\Url::fromRoute('entity.taxonomy_term.canonical', ['taxonomy_term' => $term->tid->value], ['absolute' => TRUE])->toString()
1

arg(2) will return the term id of the taxonomy term not the name because the path will be taxonomy/term/[tid].

You have added an alias for the term pages which the arg() function doesn't read as it reads the original path not the aliased path.

The function that you are using with take the taxonomy name as the argument. But what you are passing is the term id. So if you use taxonomy_term_load(arg(2)) then you will get details about the term in the returned term object.

Check out the documentation here: https://api.drupal.org/api/drupal/modules!taxonomy!taxonomy.module/function/taxonomy_term_load/7

Hope this helps. :)

5
  • Content of arg(2) is exactly this what I see in url 6-class. And taxonomy_term_load has same result as taxonomy_get_term_by_name -> false.
    – Jurik
    Jun 11 '15 at 12:10
  • So www.example.com/foo/6-class/bar is a custom built path or a path alias of taxonomy terms? Jun 11 '15 at 12:13
  • It is made by an exposed filter.
    – Jurik
    Jun 11 '15 at 12:19
  • Ok. But still then arg(2) will give you bar. Anyways if that is the case then you have to check the value of arg(1) convert it to actual term name. if the pattern of the term names are similar then you can try out this pastebin.com/iaMi9v6Z Jun 11 '15 at 12:32
  • Yeah, thanks - I know this approach. But there are several more issues. Like in we got letters like ö that are in computer language oe. So I would have to ` strreplace term by term and actually I'm looking for another solution.
    – Jurik
    Jun 11 '15 at 12:36
1

The most easy way to get taxonomy term from url is

$term = menu_get_object('taxonomy_term', 2);

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.