Drupal Views Contextual Filters: Build a Related-Content Block from the URL
A Drupal View can list published articles, but a page-specific “More from this topic” block needs one extra piece of context: which topic belongs to the page currently being viewed. A contextual filter lets the View use a value supplied by the URL or another part of the page request, instead of showing the same list everywhere.
This walkthrough is for Drupal site builders who already have a content type and taxonomy vocabulary. It uses a taxonomy term ID in the URL as the example. The exact field labels in the Views UI can differ by Drupal version and installed modules, so test the display on a staging site before placing it on a live page.
Editorial illustration: a URL supplies a term context, and the View returns matching content.
Summary
- A regular filter is fixed in the View; a contextual filter receives a value at request time.
- Start with a block View of published content and add a contextual filter for the field you want to match.
- Define what the View should do when the value is missing, and validate values when they come from a URL.
- Test valid, missing, malformed, and unauthorized contexts before exposing the block.
What the example will do
Suppose your site has articles tagged with a vocabulary called Topics. You want /topic/42 to show a block of published articles tagged with term ID 42. The View’s contextual filter reads the ID from the URL and matches the article’s topic reference field.
Drupal documentation describes contextual filters as filters whose values come from context such as the current URL, date, or another value the View can detect. Unlike a regular filter, the value can change for each request without creating a separate View for every topic.
Build the View
- Go to Structure > Views and create a new View using the content entity type that contains the items you want to list.
- Select the relevant content type, such as Article, and add a display of type Block. Limit results to published content unless your use case explicitly requires another access-controlled state.
- Add fields such as title and image, and add a normal filter for Published = Yes. Add a sort order, for example newest first.
- Under Advanced > Contextual filters, select the taxonomy reference field used by your content. If you do not see the expected field, check that the field exists on the View’s base entity and that the correct relationship is selected.
- Configure the contextual filter’s missing-value behavior and validation, then save the View.
- Place the block in a region or layout where the relevant page route can supply the expected value.
The exact URL pattern depends on your route and layout. A contextual filter does not automatically make /topic/42 a Drupal page; it only consumes context when the View is rendered. You may need a route, a page display, a block placement, or a contextual value passed by another component.
Choose a safe missing-value behavior
The filter’s When the filter value is NOT available setting is easy to overlook. Drupal offers choices such as displaying all results, providing a default value, hiding the View, showing a summary, or displaying a no-results message. For a related-content block, “display all results” can be a surprising fallback: a page with no topic context may suddenly show unrelated site-wide content.
For this example, choose a behavior that fails closed for the display: hide the View or show a concise no-results message when no term context exists. Use a default only when it is a genuine editorial rule, not as a workaround for a broken route.
Validate URL values
A URL parameter is input. Configure validation for the expected value type or entity, and decide what should happen if the value does not resolve to a real term. When available, validate it as a taxonomy term ID for the intended vocabulary rather than accepting arbitrary text.
Then test four cases:
- Known term with matching content: expected articles appear.
- Known term with no published content: the View shows the chosen empty state.
- Missing or malformed value: the block does not expose a broad list by accident.
- Term or content the visitor cannot access: access checks still apply, and private or unpublished items stay hidden.
If the filter relies on a field through a relationship, add that relationship explicitly and test it. Relationships change which entity data a View can reach; they do not automatically grant permission to display the referenced content.
Troubleshoot an empty or incorrect block
- Check the source value first. Confirm the route really supplies a numeric term ID where the View expects one.
- Check the field and relationship. The content reference field may not be on the base table selected by the View.
- Check publication and access. A correct match can still be excluded because the content is unpublished or restricted.
- Check the display. A contextual filter configured on a page display may not be configured on the block display you placed.
- Clear caches after configuration changes. Then test as both an administrator and an anonymous visitor; cached output can hide a permission or context mistake.
For more on Drupal’s Views building blocks, read the a Drupal Views empty-results troubleshooting guide. If the block should appear only on certain pages, compare the filter with Drupal’s block visibility conditions guide.
Mistakes to avoid
- Treating the filter as authorization. A contextual filter selects rows; permissions and entity access still matter.
- Showing all rows when context is missing. That can create a confusing block and may expose content you did not intend to list.
- Using a raw URL value without validation. Validate the type and expected entity.
- Testing only as an administrator. Admin access can conceal problems that anonymous users will encounter.
My Take
Contextual filters are powerful because one reusable View adapts to many pages. That flexibility makes the missing-value setting part of the design, not a minor option. Decide what “no context” should mean before placing the block.
Conclusion
Create a block View, add a contextual filter for the content reference you need, validate the incoming value, and choose a deliberate empty-state behavior. Test the route with real, missing, invalid, and restricted values. A related-content block should adapt to the page without turning an absent context into an unrestricted listing.