Drupal 7: Views: Multiple Contextual Filters with Multiple Relationships

Drupal 7: Views: Multiple Contextual Filters with Multiple Relationships

In this example we'll be building a view in Drupal 7 that filters for different recipes based on liquor brand (a content type) and product (another content type).

This will be a Page View. The path will be

/recipes/%/%

1. Create the view as normal
2. Add a “Relationship” for the entity reference for “Brand”

Drupal Developers, Drupal Agency, NYC

3. Add a “Contextual filter” for Field: title

Drupal Developers, Drupal Agency, NYC

4. Click to open the Contextual filter that was just created.

Select from the dropdown the relationship to match the “Brand” relationship that was created in step 2.

Add a custom PHP snippet in the “Specify validation criteria” area:

$handler->argument = str_replace("-"," ",$argument); return true;

This code will allow for Brands with a space in the title field to render in the URL as a dash.

You can test this in the “Preview with contextual filters” of the View.

Drupal Developers, Drupal Agency, NYC

5. Repeat 2, 3 and 4 to create a Relationship for the Product entity reference field. The order of the items in “Contextual filters” is important.

6. The URL scheme for the recipe landing pages will be:


recipes/BRAND
recipes/BRAND/PRODUCT-NAME

For example:


recipes/absolut
recipes/absolut/absolut-elyx

Drupal Developers, Drupal Agency, NYC

7. Feel free to reach out to us if you have any questions! Use the contact form here.

Location

New York
108 Wooster St, New York 10012
(949) 254-0339