How to Create a View in Drupal with Contextual Filters

Creating a view in Drupal with contextual filters allows you to dynamically filter your content based on the context, such as URL segments or user profile values. Follow this step-by-step guide to set it up.

Step 1: Install and Enable the Views Module

If not already installed, ensure that the Views module is installed and enabled. It is included in Drupal core starting from version 8.

  1. Go to the Extend menu.
  2. Search for Views and ensure it is enabled. If not, check the box and click Install.

Step 2: Create a New View

  1. Navigate to Structure > Views > Add new view.
  2. Provide a name for your view.
  3. Select the type of content you want to display (e.g., Content, Users, etc.).
  4. Choose the format for displaying your content (e.g., Table, Grid, List).
  5. Click Save and edit.

Step 3: Add Fields to the View

  1. In the Fields section, click Add.
  2. Select the fields you want to display in your view and click Add and configure fields.
  3. Configure the fields as needed and click Apply.

Step 4: Add Contextual Filters

  1. In the Advanced section on the right side, locate the Contextual filters area.
  2. Click Add.
  3. Select the field you want to use as a contextual filter (e.g., Content: ID, Content: Author, etc.) and click Add and configure contextual filters.
  4. Configure the contextual filter settings:
    • When the filter value is NOT available: Choose how the view should behave when the filter value is not provided in the URL. Options include providing a default value, hiding the view, or displaying a summary.
    • When the filter value IS available: Configure how the filter should be used when the value is present.
    • Filter value type: Determine how the filter value should be interpreted (e.g., raw value, a user ID from the URL).
  5. Click Apply to save the contextual filter configuration.

Step 5: Test Your Contextual Filter

  1. Save your view.
  2. Test the contextual filter by visiting the path that includes the contextual value. For example, if you set up a contextual filter based on node ID, visit a URL like /your-view-path/1 to see the filtered results.

Example: Filtering Content by Author

Let's say you want to create a view that displays content authored by a specific user. Here's how you can set it up:

  1. Create the View: Follow steps 1 and 2 to create a view displaying content.
  2. Add Fields: Add fields such as Title, Body, and Author.
  3. Add Contextual Filter:
    • Add the Content: Author UID as a contextual filter.
    • Configure the filter:
      • When the filter value is NOT available: Provide a default value (e.g., Current User ID).
      • When the filter value IS available: Specify how the filter value should be used (e.g., display content authored by the user ID in the URL segment).
  4. Test the View: Visit the view path with a user ID, such as /your-view-path/2, to see content authored by user ID 2.

Step 6: Customize and Refine Your View

  1. Adjust additional settings as needed, such as sorting, filters, and pager options.
  2. Save your view after making all necessary changes.

Step 7: Display the View

  1. Use the Block layout or Page settings to place your view on the site.
  2. Navigate to the appropriate URL to see your view in action.

By following these steps, you can create a view in Drupal with contextual filters that dynamically adjust the displayed content based on the context provided in the URL or other sources.

Published By: Krishanu Jadiya
Updated at: 2024-07-31 01:05:24

Card Image

How to Set Up a Local SSL Certificate on Apache: Step-by-Step Guide

Learn how to set up a local SSL certificate on Apache with this comprehensive step-by-step guide. Secure your local development environment with HTTPS.

Card Image

Latest Features of Coding Technology

Explore the latest features and advancements in coding technology, including new programming languages, frameworks, DevOps tools, AI integration, and more.

Card Image

Understanding Laravel Mix Webpack Configuration: Step-by-Step Guide

Step-by-step explanation of a Laravel Mix Webpack configuration file, including asset management for JavaScript, CSS, and Vue.js support.

Card Image

How Emojis Can Enhance Your Git Commits | Gitmoji Guide

Discover how to enhance your Git commits with emojis. Learn about the best practices for creating informative and visually distinctive commit messages.