开发者

How do I filter a block view by taxonomy argument in URL?

I have some block views in my sidebar that show events marked as a highlight happening in certain cities. Nodes are organized into cities using taxonomy.

When I load a node directly I have an URL like www.host.com/events/new-york/name-of-my开发者_StackOverflow社区-nice-event

I have some other page views that show teasers for all events in a certain city: www.host.com/events/new-york

Also I have some static pages that are valid for all cities, e.g. www.host.com/about-us

The sidebar blocks showing the highlights are available throughout the whole website. Now I want to make sure that the blocks in my sidebar only show those nodes for the selected city based on the taxonomy provided in the URL. (except for the static pages as there is no taxonomy in the URL, but those are not that important)

So far I tried to pass my view the taxonomy term as an argument using PHP as standard argument:

if (arg(1)) {
    $term = arg(1);
    return $term;
}

This works fine on the above mentioned page views (e.g. www.host.com/events/new-york). But when I load a node directly www.host.com/events/new-york/name-of-my-nice-event my block only shows the empty text.

I thought that arguments are indexed like this:

           events/new-york/name-of-my-nice-event
           ^0     ^1       ^2

So I don't understand why arg(1) does not return new-york when I am viewing the node detail.


First of all, with path and path auto what you see is not always what you get.

Fx I could setup pathauto for my articles nodes to generate urls like this

content/article/[title]

So if I wanted the title I should use arg(2) right?

No! (arg(2) is actually NULL in this case.)

The reason is that the url that's generated by path auto is a fake url, that gets translated into a Drupal url. In the case above what I get is node/[nid]. So eventhough the node title i in the url, I can't get it by using arg(), but I can get the nid by using arg(1)

I can't guess what your urls map to, it depends how you have set up your site what modules you use etc.

A good advice if you do a lot of these context aware things, is to look into panels. It's made to be able to tell modules like views about the context which it is present. Like fx terms, nodes, etc, and you could use this to pass arguments into views.

Panels can do a lot more and is quite complex, but if you need to do a lot of this stuff, it is probably worth the investment.


Solution to my problem:

if (arg(0) == 'node' && is_numeric(arg(1))) {
    $node = node_load(arg(1));

    if (count($node->taxonomy) > 0)  {
      foreach ($node->taxonomy as $term) {
        $term = $term->name;
      }
    }

    $term = strtolower($term); // for some reason needed in my case
}

else {
    $term = arg(1);
    $term = str_replace('-', ' ', $term); // for some reason needed in my case
}

return $term;


While this was technically possible with Views 2 as described in some of the other answers, Views 3 has this integration built in.

You can create an argument for the taxonomy term id and then choose "Provide default argument". This will then give you an option for "Taxonomy Term ID from URL" and "Load default argument from node page..."

This will allow you to take the taxonomy of a page and pass that as an argument to your view block.

Note: Views 3 is currently in Alpha 3 but in my experience is at a relatively stable state and I am using it on production sites. If it has features like the one above that you find useful please use it, test it and submit bugs/patches if you encounter any issues!

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜