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.
- Go to the Extend menu.
- Search for Views and ensure it is enabled. If not, check the box and click Install.
Step 2: Create a New View
- Navigate to Structure > Views > Add new view.
- Provide a name for your view.
- Select the type of content you want to display (e.g., Content, Users, etc.).
- Choose the format for displaying your content (e.g., Table, Grid, List).
- Click Save and edit.
Step 3: Add Fields to the View
- In the Fields section, click Add.
- Select the fields you want to display in your view and click Add and configure fields.
- Configure the fields as needed and click Apply.
Step 4: Add Contextual Filters
- In the Advanced section on the right side, locate the Contextual filters area.
- Click Add.
- 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.
- 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).
- Click Apply to save the contextual filter configuration.
Step 5: Test Your Contextual Filter
- Save your view.
- 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:
- Create the View: Follow steps 1 and 2 to create a view displaying content.
- Add Fields: Add fields such as Title, Body, and Author.
- 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).
- 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
- Adjust additional settings as needed, such as sorting, filters, and pager options.
- Save your view after making all necessary changes.
Step 7: Display the View
- Use the Block layout or Page settings to place your view on the site.
- 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