How can I create a view page that takes taxonomy machine name and has depth?

This question is a followup to How to load nodes that are associated with the child of a taxonomy term and refers to this answer in particular, but is standalone, so you need only visit those if you want more background.

I want to create a view that functions like the Taxonomy term view that comes with views with depth turned on in the contextual filter, but taking the term's machine name instead of tid as the contextual argument.

I have pathauto set up to alias /taxonomy/term/tid to /vocabulary/term, and I would like to override the page there with views to make it show nodes associated with the children of the term as well as the parent.

So if I had the following Taxonomy:

Vocabulary 1 (vocab_1)

• Term 1 (term_1)
• Term 2 (term_2)
• Term 3 (term_3)
• Term 4 (term_4)
• Term 5 (term_5)

and nodes as follows:

Node 1 - Term 2, Term 5 Node 2 - Term 5 Node 3 - Term 3

A visitor to /vocab_1/term_1 should get a list that contains nodes 1 and 3 because they are associate with children of Term 1.

I haven't been able to figure out how to do that with the term machine_name as the contextual filter instead of the tid.

• "but taking the term's machine name instead of tid as the contextual argument" FYI: Terms don't have machine names. – Елин Й. Apr 16 '16 at 8:41
• Ok, not machine name, but whatever path auto generates for it. – UltraBob Apr 16 '16 at 13:28

You can use the contextual filter Content: Has taxonomy term ID (with depth) without adding a Relationship. Use this settings for the contextual filter:

When the filter value is NOT available: Provide default value
Type: Taxonomy Term ID from URL
Make sure Load default filter from term page is checked.

Please note: When you visit a pathauto destination path, in this case with a source of /taxonomy/term/% pointing at /vocabulary_name/term_name, drupal passes the source path as a query variable, and your view still uses the source path to gather information. This means your path in the view should not change, it should stay as taxonomy/term/%

• Will that give me depth? All the modes will be associated with child terms of the term in the URL. – UltraBob Apr 16 '16 at 13:25
• I've changed my answer, after reading your question once again. – Елин Й. Apr 16 '16 at 22:16
• As far as I can tell your answer is assuming the url contains a tid, but it doesn't. It creates the term name as adjusted by pathauto. I suppose it is possible that that somehow carries the tid along with it, so I'll look into it. – UltraBob Apr 18 '16 at 1:09
• Reviewing this it seems not to take the name of the term, but instead expects the tid. since I have pathauto set up to alias /vocabulary/termname to the term page this doesn't seem like it will work. – UltraBob Apr 18 '16 at 1:33
• @UltraBob Yes it expects the tid, but it should work with pathauto. – Елин Й. Apr 18 '16 at 5:59

Unfortunately looks like there isn't a term machine name contextual filter option. There is one, but only for vocabulary machine name.

If you really want this, you will have to get your hands dirty.

Create a custom module with a custom page that has a variable url. ex: /taxonomy/% the % being a place holder for the machine name.

Then on page load using using current_path() along with some php function (perhaps str_ireplace() to remove the taxonomy/ part), you grab machine name from the url. Then you query the database, using db_query(), to grab the term id that belongs to that machine name.

Once you have the term id you can render the view on your screenshot using php. So you just pass the term id as the argument.

<?php
print views_embed_view('my_view', 'block_1', $arg,); ?>  so in your case it would be: <?php print views_embed_view('taxonomy_term', 'page',$term_id,);
?>


Update:

After looking at the taxonomy term table (taxonomy_term_data) turns out that there is no machine name column for taxonomy terms (which explains why there is no contextual filter in views).

Therefore, the only alternative would be to use the taxonomy term name. As long as you have unique term names, this will be fine.

First, you need to go to the taxonomy term view (the one in your screenshot) and press on and add a block and save.

Here is a working module I created called term finder.

/sites/all/modules/term_finder


term_finder.info

name = Term Finder
description = Creates variable pages for taxonomy term names and shows a view.
core = 7.x


term_finder.module

<?php

/**
*/
$items = array();$items['taxterm/%'] = array( // this will be our custom page url. The % allows us to put anything after, ex: taxterm/[anything can go in here]
'title' => 'Your Title Goes Here',
'access callback' => TRUE,
'page callback' => 'term_finder_page', // the name of the function that gets runed when we visit our taxterm/[anything] page.
);

return $items; } /** * Page callback for taxterm/%. */ function term_finder_page() {$path = current_path(); // gets the current page url. ex: taxterm/hello-world and saves it on a variable
$path = str_ireplace('taxterm/','',$path); // removes the taxterm/ from our variable, so we are left with hello-world
$path = str_ireplace('-',' ',$path); // replaces the - with a space, so we have hello world
$term_id = db_query("SELECT tid from {taxonomy_term_data} WHERE name = :name LIMIT 1", array(":name" =>$path))->fetchField(); // queries the DB to find the tid that belongs to hello world.
if ($term_id != '') { // we check if grabbed a term id, is not empty.$show_view = views_embed_view('taxonomy_term', 'block_1', $term_id); // creates the view, which passes the tid we got from the DB as an argument. return$show_view;
}
else { // if the term id is empty, then we show "not found".
}
}


Lets assume you have a term called Hello World.

What this does is if you go to /taxterm/hello-world or (in firefox) /taxterm/hello world

It will look up in the DB for Hello World and get the term id and pass it to the view.

PS: In case you are wondering why the mysql query looks the way it does, see Writing secure code and How do I get only one result using db_query()

• Actually I'd love to do that, I just don't know where to get started with creating a module listening for a specific page. I'll try to dig further into the docs to figure that out, but any pointers for the right place to look would be appreciated. Being able to do that would let me avoid loading the views infrastructure until I am actually ready to render a page rather than when I'm still going to do a redirect. – UltraBob Apr 14 '16 at 13:45
• I've been trying to follow the early examples here, but every time I flush cache and load the new path that it seems my hook_menu should have created I get access denied and my function is never called. I'm sure when I discover what I've been doing wrong it will have been something simple, but for tonight it has defeated me. – UltraBob Apr 14 '16 at 14:28
• @UltraBob updated my answer, sorry for the delayed reply. Somehow I missed the comment notification. – No Sssweat Apr 16 '16 at 7:41
• You are my favorite person! I can't figure out how one might pm here, but if you have an amazon wishlist or something I'd love to give something back. This isn't exactly what I need, but it shows me everything I need to get there, and teaches me some extra important information in the meantime. Thank you! I'll go implement my version and come back and accept. – UltraBob Apr 18 '16 at 1:38
• @UltraBob Thank you for the kind words, glad to hear my answer helped you in some way. I help people in here without the expectation of getting something back nor I want something back. I learned Drupal from the Community by watching videos tutorials, reading tutorials and reading Q & A's here. This is the beauty of Drupal, it has a great community. – No Sssweat Apr 18 '16 at 2:10