Blog

Passing Page Arguments to View Panes

Written by Metal Toad Staff | May 3, 2013 12:00:00 AM
Filed under:

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.
  • 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"
  • 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.

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

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.

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