Extra Block Types (EBT) - New Layout Builder experience❗

Extra Block Types (EBT) - styled, customizable block types: Slideshows, Tabs, Cards, Accordions and many others. Built-in settings for background, DOM Box, javascript plugins. Experience the future of layout building today.

Demo EBT modules Download EBT modules

❗Extra Paragraph Types (EPT) - New Paragraphs experience

Extra Paragraph Types (EPT) - analogical paragraph based set of modules.

Demo EPT modules Download EPT modules

Scroll

Views contextual filters, relationships - arguments and relationships (connections)

17/04/2025, by Ivan

In this lesson, I will focus especially on working with arguments in Views. Arguments refer to parts of the URL, for example:

node/15
The first argument is node, the second is 15.
taxonomy/term/10
The first argument is taxonomy, the second is term, the third is 10.
user/67
The first argument is user, the second is 67

I believe these examples are clear enough and don’t require additional explanation.

You can get arguments using the arg() function:

<?php
print arg(0) . '<br />';
print arg(1);
?>

If the URL was node/15, the output would be:

node
15

Argument indexing in the arg function starts at 0. Now let’s see what built-in solutions are available in the Views module.

Displaying User Nodes

Let’s say we are on the page user/1 and want to display a block of nodes created by the user with uid = 1. For this, we’ll use a URL argument.

Create a new view. Output a block, and in the fields section specify the node title. Of course, we only display published nodes and sort by post date.

Title
Title: user_nodes
Format
Format: Unformatted list | Settings
Show: Fields | Settings
Fields
Content: Title
Filter criteria
Content: Published (Yes)
Sort criteria
Content: Post date (desc)
Pager
Use pager: Display a specified number of items | 15 items
More link: No

Now click Add in Contextual filters. Choose:
Content: Author uid

Then select

When the filter value is NOT available
  Provide default value
    User ID from URL
      Provide default value

Views contextual filters

Save the view. Place the block in the desired region and visit, for example, user/1. The block we created will now display all nodes authored by user with uid = 1.

Displaying Related Nodes

Now let's complicate the example. Suppose we want to link several nodes to one. For this, we use node reference fields. To use these relationship fields, we need the References module and its submodule Node References.

http://drupal.org/project/references

Relationship fields in Drupal 7 allow linking several users and several nodes to a single node simultaneously. This is implemented via Node Reference fields. Since in Drupal 7 fields can be added to any entity, you can now link any type of entity.

I have 4 news articles on the site, and I want to link the first and third articles to the second one, as they cover similar topics.

Go to the content type management for "News" and add a Node Reference field with autocomplete enabled. I’ll name the field "Related News". Then choose which content types can be referenced, and set the number of referenceable nodes. I selected only the News type. Click Save Field. Now edit the second news node and use the new Node Reference field to select related news.

Drupal reference field

Of course, these related nodes will already display as a list attached to the current node. But our goal is to configure a View.

Create a new View in block format.

Title:
similar_news
Format:
Unformatted list | Settings
Show: Fields | Settings
Fields
Content: Title
Filter criteria
Content: Published (Yes)
Content: Type (= News)
Sort criteria
Content: Post date (desc)
Pager
Use pager: Display a specified number of items | 5 items

Add a contextual filter: Content: Nid. This time choose:

When the filter value is NOT available
  Provide default value
    Content ID from URL

Also, in Relationships add a new relationship: Content: Related News (field_similar). (If your field is named differently, choose accordingly). Note that the same field might appear with a "reverse" label—this means it defines a reverse relationship, so if you’re on News 3, News 2 will display as related (even if only News 2 defines the relationship).

The result should include:

Contextual filters
Content: Nid
Relationships
Content: Related News

Now in the content fields, assign the relationship. Instead of "Do not use a relationship", select the name of the relationship.
Fields
(field_similar) Content: Title

Save the view and place the block in the desired region.

Now, on the page for News 2, the related news (News 1 and News 3) will display in a separate block.