0

I am trying to use a views block with a contextual filter for taxonomy terms to create an index that can filtered based on terms in the URL.

This works fine with a views page: so if the URL is /my-index then I see all the sub pages. And if the URL is /my-index/apples I see all the sub pages tagged with apples.

Now I when I make a block the same as the page view described, I understand that the block does not have a source for the contextual filter, so I have to provide a filter value. I have tried all the settings, but the problem seems to be that when using a views block and the URL is /my-index/apples I get a file not found error. With /my-index I get the full list as expected.

The filter works in preview mode, and if say I set the Default Value to Fixed Value=apples, is works at URL /my-index (filtered for apples as expected), but when URL is /my-index/apples then again I get file not found error. Then if I change the filter value to 'Display all results for specified field' I get the same result i.e. /my-index shows all and /my-index/apples gets file not found.

It seems to me that when the URL is /my-index/my-term it is not getting to the /my-index page to run the views script in the block. So I cannot connect the page URL to the block because I am not getting to the page at all when I add my-term to the URL.

I have set up a new alias for the page and tried new terms in case there is some sort of clash somewhere but to no avail.

The answers provided at Contextual filter with a block view did not seem to help. I tried every setting in every combination several times.

1

I think you are just a bit confused about the structure of the page drupal creates so hopefully this might help to clarify it all.

When you create a page display in Views with a contextual filter, you basically are creating a menu item / path on your website. In this case, it's probably something along the lines of my-index/%, with the % being the contextual filter.

You then define that filter to be a tax term. So, you can then type in www.yourhost.com/my-index/foo and www.yourhost.com/my-index/bar and www.yourhost.com/my-index/baz and get the $content of those pages filled with the fields of the view you created filtered by foo, bar, or baz.

Or any other term a user puts in in place of the %.

This way you aren't creating umpteen individual views, eg, you don't have to create my-index/foo, my-index/bar, my-index/baz, etc etc page displays for each and every possible tax term.

Plus if you say show me everything if the contextual filter is missing, then you can also go to www.yourhost.com/my-index and get a list of everything.

Now to blocks. These aren't the main $content of your page, but rather little boxes of additional information off to the side. They can be placed pretty much on any page you want through the block admin pages, you just go in there and tell it what region you want and pages and off you go.

So, in this case, let's say you created a page display called my-index/% like the above. In this example, let's also say that this view lists the content on your system sorted by Title. So, in the main content area of this page you'd have that listing of your content tagged with that tax term sorted by title.

But let's say you wanted a block off to the side listing the very latest content tagged with that same tax term.

Here, you would create a block display of content sorted by creation date in descending order and limited to 1 item. Here you would also add a contextual filter, but in the supply the default argument, you would tell it to take it from the url and specify where it is, in this case the 2nd argument.

Then you'd save everything and go to the block admin page and down in the list of blocks available you'd see this View Block. Assign it to a region in your theme and then in the pages, restrict it to my-index/* and then it will show up on that page along with your main content.

  • Jimajamma, a big thank you for your rapid, detailed answer. I don't doubt that I am confused :). However I did understand everything you say, I think. I'm trying to use a block as I needed two parts on a page - one with a carousel, and underneath a list of the page links similarly filtered - I couldnt seem to get two sets of data in a single page view. So I was hoping to pull the tax terms from the url and use them in both blocks. Based on what you said I added a view block to a view page rather than a normal node and it does just what I want now. – Jack McKenzie Aug 31 '12 at 6:58
  • I didn't mean the confused part as an result, more of 'you are almost there.' In any case, glad I could help out! – Jimajamma Aug 31 '12 at 12:45

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.