Samuli Natri
Nov. 27, 2015
Updated: Sep. 28, 2016
0
By Samuli NatriNov. 27, 2015
(Updated: Sep. 28, 2016)
0

Views #3 : Filters And Sorting (Drupal 8 Tutorial #5) (Article + Video)

In this tutorial we will show only Articles that are promoted to the front page, authored in the last 2 hours and sorted so that the articles with most comments are on top.

Table of Contents

Video has more explanations and examples!

Filters

  • Click Add next to FILTER CRITERIA:

Filter criteria

  • Search for Promoted to front page and select the Promoted to front page status as Yes:

Promoted to front page

Now only the Articles with Promoted to front page checked in the Publishing options will be listed in the front page.

Promoted articles

Example:

You want to show only articles that have been created in the last 2 hours.

  • Add a filter Content: Authored on with these settings:

Authored on

You could for example create multiple View Displays for articles created 2-3 hours ago, 3-4 hours ago etc. Like you see in the news sites.

Sorting

  • Create more articles with comments in Configuration > Development > Generate Content
  • Add another field called Comments Count with a label Comments :
  • Click Add next to SORT CRITERIA and add a Comment count criteria. Select the order as Sort descending:

Sorting

The view so far:

The view so far

In the picture you see that we have the Authored on sorting before the comment sort,
so if you add newer articles, they will show first, regardless of the comments they have. Just remove the Authored on sort if you want to see the most commented always first.

Result

Our view now shows only Articles that are promoted to the front page, authored in the last 2 hours and sorted so that the articles with most comments are on top.

Result

Add new comment