Contextual Filters

Contextual Filters work the same way that Filter Criteria do, except that Contextual Filters create the filters dynamically. For example, if we were to add a filter criteria for author uid, we'd have to select one uid. If we chose "uid-1", the view would always show content by user 1 regardless of who is viewing the page. If we want to add a filter like "content created by the currently logged in user", we'll have to add a contextual filter. Let's go ahead and do just that.

I'll create a new page display and add a contextual filter that displays all nodes created by the logged in user.

Add another display

  • Click "+ Add" on the display section
  • Choose "Page"

Modify Page display

  • Display name
    • Click "Page"
    • Name: User Page
  • (Apply)

Title

  • Title
    • Click "Shop"
    • For: This page (override)
    • My Products
  • (Apply(this display))

Next, let us define the URL for this page then add a menu item.

Page Settings

  • Click "No path is set"
    • myproducts
  • (Apply)
  • Menu
    • Click "No menu"
    • [x] Normal menu entry
    • Title: My Products
    • Menu: User menu (This will display the view in the user menu)
  • (Apply)

Now, let’s remove the header and footer information, as they’re not as applicable to this view.

Header

  • Click "Global: Text area"
    • For: This page (override)
  • (Remove)

Footer

  • Click "Global: Text area"
    • For: This page (override)
  • (Remove)

Alright, now let’s add the contextual filter.

Add contextual filters:

  • Click "Add" next to Contextual Filters
    • For: This page (override)
    • [x] Content: Author uid
  • (Apply(this display))
  • When the filter value is not in the URL
    • [x] Provide default value
      • Type: User ID from logged in user
  • When the filter value is in the URL or a default is provided
    • [x] Specify validation criteria (This will gives you more options to validate the specified criteria)
  • Validator: - Basic validation - (This simply verifies that the criteria exists.)
  • Action to take if filter value does not validate: Show "Page not found" (If for some reason the filter criteria doesn’t validate, the view will display "Page not found". This should be impossible, but it doesn’t hurt.)
  • (Apply(this display))
  • (Save) (Save the view)

To check out our new display, I’ll go home, and there we’ll see the "My Products" link in the user menu.. Now, let's switch to another user to check our contextual filter.

I’ll create a user named Chelsea, and grant authenticated users the following permissions
* Product: Create new content
* Product: Edit own content
* Product: Delete own content
* Use the administration toolbar

Add a new product

Logged in as Chelsea in Safari, I’ll create a new product.

  • Click "Add content"
  • Product name: Product #7
  • Click "Choose File"
    • Image #7
    • (Upload)
  • Price: 40.00
  • Availability: [x] Available
  • Description: (Lorem Ipsum)
  • Department: Men’s
  • (Save)

Now, let’s take a look at the My Products view.:

  • Click "My Products" tab

You see that it only display products that are created by Chelsea.

Let's switch back to Admin account and continue to modify the Advanced Settings of our Shop View.