0

is a simple question, I whant get the term ID (tid) from standard drupal taxonomy url taxonomy/term/167 ou 167 is my tid and use as argument on views.

I found this code to get the tid:

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

How to get taxonomy by URL alias?

I try also :

$here_is_my_tid = drupal_lookup_path('source', 'taxonomy/term/'.$tid);


$here_is_my_tid = drupal_get_path_alias('taxonomy/term/' . $tid);

But I try many combination without success.

Here is my views with the tid from url I try to pass "$here_is_my_tid"

$view1 = views_embed_view('mavue','page',$here_is_my_tid);

 if(arg(0) == 'taxonomy' && arg(1) == 'term') {
            $tid = (int)arg(2);
            $term = taxonomy_term_load($tid);

      }
    $view1 = views_embed_view('mavue','page',$term);

By advance thanks

4
  • Do you want to get the TID from the standard URL and then pass it to the views as a contextual filter, right? Commented Apr 14, 2019 at 10:23
  • 3
    Possible duplicate of How to get taxonomy by URL alias?
    – GiorgosK
    Commented Apr 14, 2019 at 10:47
  • @usmanjutt84 Yes I want to get the TID from the standard URL and then pass it to the views as a contextual filter
    – comcom
    Commented Apr 14, 2019 at 10:54
  • link above does not help ?
    – GiorgosK
    Commented Apr 14, 2019 at 11:36

1 Answer 1

2

You do not have to write any code to achieve that.

Just go to the views edit page, add a contextual filter named as Has taxonomy term ID and select Provide default value, see screenshot for more options

enter image description here

However, you can also do it pragmatically

function HOOK_views_query_alter($view, $query) {
   $term = menu_get_object('taxonomy_term', 2);
   if ($term && $term->vocabulary_machine_name == 'VOCABULARY_NAME') {
     if ($view->name=='VIEWS_MACHINE_NAME') {
       $view->query->where['0']['conditions']['0']['value']= $term->tid;
     }
   }
}

I hope it helps.

3
  • You can also take help from this tutorial youtu.be/XI0iNYpaJ9o?t=114 for passing TID using Drupal Interface. Commented Apr 14, 2019 at 11:31
  • Hi, I know for "has taxonomy term ID from views UI" but I use an ajax callback so I need to do programatically. I try you're programatically solution who look well I Haven't think about use a views_query_alter, many thanks
    – comcom
    Commented Apr 14, 2019 at 11:31
  • I sent you a message if you can check thanks
    – comcom
    Commented Apr 14, 2019 at 13:59

Your Answer

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

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