Passing Page Arguments to View Panes

Recently I was faced with the task of passing off multiple page arguments to a view pane, which seems simple enough after you have done it once, but the first time around I found myself wading through settings for far too long. The scenario: we were working with Page Manager to create pages and within a page we had a View Pane that needed to be filtered based on the context filters being passed to the page as arguments in the URL.

For this example I set up a basic Drupal install with ctools, views, views_ui, panels, pages, page_manager, and views_content installed and enabled. I created a basic taxonomy vocabulary named Category with three terms and then added a term reference field to the Article content type. Then I used the devel module to generate some users and articles. My goal for the example is to have a page with a view pane that can be filtered by a user id and taxonomy term id of a category based on arguments in the URL.

First let's build our view.

  • In the admin under Structure > Views click "Add new view".
  • Name the view and have it show "Content" of type "Article".
  • Un-check create a page and hit the continue & edit button.
  • Click the add button and create a content pane.
  • Setup your fields to display as desired. I set the format to Table and I am showing the Title, User Name (added a relationship to the content author), and Category.
  • Create a contextual filter for the Content: Author uid.
  • Create a contextual filter for the Content: Has taxonomy term ID.our view
  • Click the Edit button next to Argument Input.
  • For your author uid set it to "From panel argument" and set the Panel argument to "First"
  • Do the same your taxonomy term, but set the Panel argument to "Second"view arguments
  • Save your view.

Now let's build the page.

  • In the admin under Structure > Pages click "Add custom page".
  • Name the page and provide a path.
  • Continue on until you hit the Panel Content screen. In your layouts center content area, click the small gear icon to access the context menu and click Add Content.
  • Select View panes in the left side menu. You should see the view that we made above. Select it and click finish.
  • Click Finish then click Save.

Let's take a look at the results. Pull up the page at the URL you specified when assigning a path. For now leave it with no arguments. You should see all articles with no filtering.
all results

Now let's add a single argument to filter by a user id. Now you should only see articles by one user.
user filter

And lastly let's add a second argument to also filter by a category (taxonomy term id). Now you should only see articles by one user assigned to only one category.
user and category filter

As for how you assign these arguments in your URL's... well that's another blog post for another day.

Comments

Another fun one is the user configured pane argument! If you add other contextual filters, and have each display all values when no argument is present, you can treat them as variants. That way, in a user configured setting, one can choose from multiple arguments. Isn't it great for the 'no argument' functionality to actually be usable and not just a place for a hack because blocks don't work with views?! Discovering this capability you here describe was what finally sold me on the panels approach... in most situations!

Thanks Tom, great article. I wanted to ask you if you specified any arguments for the url in Page manager? something like: filtered-articles/%tid/%tid

Cheers

Sorry for the delayed reply, been stomping though Django-land the past few days. Passing arguments around in Drupal seems to be one of those things that has a lot of support that should make it easy, however until you've gained some millage tinkering with it it can be frustrating. Let me know if you have any more questions.

After trying different things it worked out with the arguments. Cheers

Tom,
I am trying to recreate your solution and running into issues. I used a custom content type that had a taxonomy on it. I used just one contextual filter instead of 2. When I try to query using the url/"term id" it brings back all of the content and not just at the one specified in the URL. Any advice?

Sorry Russ, this comment slipped through the cracks back during the holiday-ish time of year and I just saw it today when replying to another comment. I'm assuming by now you've either solved it or moved on, but I'm not entirely sure why that wouldn't be working for you. If you have the contextual filter for the "has taxonomy term id" set up for your vocab, and you got it all squared away in the argument input then it generally should work. I'd probably do some debugging, maybe a dpm in a views hook to see it sheds any light on if the argument is showing up and/or why it's not being applied.

I'm having the same issue as Russ. I'm using the convert taxonomy term to ID in the view and it works just fine in the "Update Preview" but doesn't filter in the page manager. It works fine when I just use Taxonomy ID, but not Convert ID from name. Really strange.

I don't know how to thank you. It took me about 20 working hours to solve this problem until I managed to formulate the right question to Google and found your post. Big, big kiss and thank you :-)

Still I've got one more problem and maybe you've encountered such a thing and can help me. I've got a panel pane with searching form and included panel pane with glossary. The mother panel pane receives two arguments: 1.id of a worker's unit and 2.first letter of surname in the way that you described. The included panel pane with glossary receive only the first parameter - unit id. I don't know how to force the searching form to use the URL without the second parameter, because now the form searches for something like: http://pagename/123/h?surname=jones, and it will not find Jones under letter H. Have you any idea how to control it and neglect the letter argument just for the searching form?

That's an interesting problem that I can't say I've directly tackled before. Off the top of my head, I can't think of any "pretty" ways to pull that off. Forgive me, it's been a while since I was deep into this type of scenario, but it seems like one way or another your task would be to find a way to tell the view to ignore the second argument if that other parameter is passed. That could be where the "not pretty" solution could come into play. If you are familiar with how the arguments are handled in code ( https://drupal.org/node/54455 ) it should be possible to use a hook to intercept the view query before it is ran ( https://api.drupal.org/api/views/views.api.php/function/hook_views_quer… ) and if you see the search argument being passed, then programmatically clear out the condition that it has to start with that second argument. Again, this isn't really a clean way of doing things (and some of my Drupal purist co-workers might slap me on the wrist for even bringing up query alters), but I'm afraid it is the best option I can think of without replicating the issue and doing some hands on tinkering.

Another dev and I did have to do something similar functionality-wise here http://www.dccomics.com/browse but that was being handled by a custom module with custom c-tools plugin as opposed to just what panels and views could offer off the shelf.

I have no idea about what I am doing wrong. I created a page with the URL example.com/product/%tag with a selection rules that verifies that %tag is some tag name of some specfic vocabulary.

On the other side, I created a pane view that shows content with a contextual filter that uses a taxonomy term ID. I set the panel input to "From panel argument" (first) and even though the preview works fine if I enter terms like "data-broker" or "collaborative-platform" which are valid taxomy terms, the result if I load the actual page using an URL like example.com/product/data-broker, it shows every content that is tagged with any term in the specific vocabulary.

Am I missing something here? Can you give me a clue on what might be the problem?
Please? :P

Hi Tom,

Brilliant post. Thanks so much. Solved my problem.

Wondering if you ever posted something on the URL filtering as suggested at the end of this post?
"As for how you assign these arguments in your URL's... well that's another blog post for another day. "

Very keen to see what you wrote.

Thanks,
Lewis

How do you create an exposed filter for 'category', and place it in a separate block or pane, so the user can select the category rather than typing a TID or term name in the URL?

Thank you

Add new comment

Restricted HTML

  • Allowed HTML tags: <a href hreflang> <em> <strong> <cite> <blockquote cite> <code> <ul type> <ol start type> <li> <dl> <dt> <dd> <h2 id> <h3 id> <h4 id> <h5 id> <h6 id>
  • You can enable syntax highlighting of source code with the following tags: <code>, <blockcode>, <cpp>, <java>, <php>. The supported tag styles are: <foo>, [foo].
  • Web page addresses and email addresses turn into links automatically.
  • Lines and paragraphs break automatically.

Ready for transformation?