Using a current node field value as a contextual filter in a block view

As you probably know, Drupal views can be rendered as a block and this block can be placed into a node page like in the sidebar region or below the content for example. An usual requirement is show related content, or more content of a same taxonomy that is dependent on the current node (the one that we are seeing in default view).

I’m working on a site where we have a content type for ephemeris, this content has a field with the date of the historical event. As per design, the idea in the full view of the ephemeris is to show below the main content a block where we must render a list of ephemeris of the same day and the current event must not be included in that list.

So, my first idea was to implement that with a view block, that is the easiest part. However, I have the need to filter the results of the views using the date field in the current node. I knew that I needed to set in place a contextual filter in order to apply a criteria to return content of the same date, however I couldn’t find any guide on the internet about how to do it, and I couldn’t figure out neither with the core modules. 

Since I wasn’t finding anything useful I tought, there must be a way to set the contextual filter value using tokens, since token is amazing, and it makes sense to be used in that way. Out of the box there wasn’t anything to achieve that.

Googling and gooling I was able to find views extra module, this provides a couple of more options to set the default value of a contextual filter, however didn’t have an option for what I was looking for.

Then I found Views Token Argument, this module allows to use tokens in the contextual filters with the great thing that uses the current node to fill the tokens (not in the same way that view extra, but better), Bingo I said!. 

The only problem that I saw is that the module is not currently in active development and the module version is not allowing Drupal 9. So I have decided to download manually the module, place it as a custom module, and modify the info file to allow Drupal 9 version. After that I went to extend and I was able to enable the module and everything worked.

I’m still testing this module however I’ll send patches to the maintainers to upgrade this great module, I can’t believe that this is not being well maintained and updated.

So, how to use it?, very easy. 

Go and create your view as a block, select all the display options, add all your criterias as a regular view is created. You know that, and there is a lot of documentation available, I wont explained here in this post.

Next step is to add a contextual filter. In my case, the target field is a date field, I need to filter by day and month, so I’ll need to format in some way the field in order to explode the day and month values from the entire date time field. Also I need to exclude the current node for the view result list.

To define more clearly the requirement, I need this filter

current_node.month = view_node.month
current_node.nid != view_node.nid
current_node.day = view_node.day

Where current_node is the node that is being loaded in the page, the one that is shown in the main content. And view node is the target node, form the list of nodes, selected by the view to include (or not) in the result list.

Let’s add the first contextual filter, when we go Contextual Filter > Add in the view edit page, a modal will open.

Here we can select multiple fields to be used as a filter. We can access the entire field, however, I need to use a special case, only the day or month, since Drupal knows that is a date field besides including the entire field value, also adds formatted values, like the day, month, year, time.

The options will depend on the field type always, so always browse what you have for another cases that you may need to do.

We will add the day field first (Content: node.field_date (day)), next we will continue with the configuration. Here we need to select the option “Provide default value” since we need to set this value by default to always filter. Once we select this option will appear a dropdown to select the type, by default drupal has very few options, once we install the two mentioned modules we will have more options for types, here we want to select Token, this is provided by Views token argument module.

In the next input we need to enter the token to get the day value from the date field from the current node. To discover the right token I have used the option “Browse token”, in my case the used token was [node:field_date:date:custom:d] and that’s it, save it and the context filter will be set.

You need to do the same thing for month, select first the field for month and then use the token to format by month [node:field_date:date:custom:m]

Lastly, for add the exclusion of the current node, Add another contextual filter, select content ID field, and in the contextual filter configuration select Content ID from Url, no need for a token here.

That is it, if we test the view with the view debug enabled, we can see the query that will run and you’ll that the criteria is being added.

I hope this guide helps you to write better views that can react against the current node without writing any code. 

Let me know if you have more ideas or better implementations in the comments.

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.