Samuli Natri
Dec. 11, 2015
Updated: Sep. 28, 2016
0
By Samuli NatriDec. 11, 2015
(Updated: Sep. 28, 2016)
0

Views #5 : Contextual Filters & Relationships (Drupal 8 Tutorial #7) (Article + Video)

Contextual Filters are similar to the basic Filters, but they are more dynamic. For example you could use them to show only articles that the current user has created. Relationships are mostly used to get extra information from an entity. In this tutorial we use author relationship to get the name of the author and use it in a Display summary to list all the users that has written articles and count their posts.

Table of Contents

Contextual Filters

You can add a default FILTER CRITERIA to show articles from a specific user:

Regular filter

But to specify the user in the URL or use current user you must add a contextual filter.

  • Click Add next to CONTEXTUAL FILTERS and add an Authored By filter.
  • Leave the default settings and hit Apply:

The default filter

Now you will see all articles unless you provide user id in the URL.

  • Add Content: Authored by field to show the author. Select Author as the Formatter:

Authored by

  • Go the view page (/home) and specify some user id in the url:

User id in the url

Now you see articles for a user specified in the url:

Article specific articles

Use the Provide default value to use the current user as a filter:



Now the homepage shows only articles written by the current user:

Relationships

You can use a Contextual Filter to create a Display summary.

Add a relationship to get the author name.

  • Click Add next to RELATIONSHIPS and add a Content author relationship.
  • Add a contextual filter User: Name.
  • Select Display a summary and Number of records:

View so far:



Add new comment