Build a Blog in Drupal 8: Using Views

This tutorial is part of the “Build a Blog in Drupal 8” series:

  1. Content types and Fields
  2. Adding Comments
  3. Using Views
  4. Managing Blocks
  5. Create and Manage Menus
  6. Custom Contact Forms

Just when you thought Drupal 8 couldn’t get more powerful; I give to you Views in core. Yes, the most installed module in Drupal is now part of core.

No longer will you have to wait for Views to be upgraded to use the latest version of Drupal. Just install Drupal and start building your custom views page or block.

For people who don’t know, Views allows you to list Drupal content. That’s a simple explanation, but it doesn’t give the module the respect it deserves.

For instance, you could create a page (/latest-reviews) which displays all the latest content by content type and sorted by created date. Using the module, you can query the site’s content and display it in a block, table or page just to name a few.

Now that Views is part of core, it’s used throughout the administration section. Take for example the Content (admin/content) and People (admin/people) pages. All of these are powered by Views.

In this tutorial, we’ll continue building our blog site by adding in a few custom views. We’ll create a listing of blog posts for the homepage, a “Recent blog posts” block and we’ll enable Archive view which comes with Drupal but it’s disabled.

If you’re following along make sure you’ve read and setup the Blog content type which was built in part one of the “Build a Blog in Drupal 8” series.

Create Homepage View

With Views in core any type of listing is now controlled by Views. The default homepage is also a view and it’s called Frontpage.

If you want to customize the homepage you have two options: modify the Frontpage view or create your own, let’s go with the latter and create our own.

1. Go to Structure, Views and click on “Add new view”.

2. Fill out the “Add new view” form, using the values defined in Table 1.0 and click on “Save and edit”.

Table 1-0. Create a new view

Option Value
View name Homepage
Machine name homepage
Show Content type of Blog sorted by Newest first
Create a page Checked
Display format Unformatted list of: teasers

Fig 1.0

Once you’re on the Views edit page click on Save.

At this point we have a working page. It lists published blog posts, sorts by created date and it’ll paginate after 10 posts.

Now we need to set this page as the homepage.

4. Go to Configuration, “Site information” and add “/homepage” into “Default front page”. Make sure you add the forward slash “/”.

Fig 1.1

Now if you go to the homepage you should only see blog posts appear.

Editing Views

You may have noticed that the homepage has a title called “Homepage”. Normally it wouldn’t have a title, let’s remove it.

A view can be edited in two ways. First, by going to Structure, Views or by clicking on the “Edit view” link when you hover over the view.

Fig 1.2

1. Hover over the view and click on “Edit view”.

2. Click on the Homepage link in the Title section, remove it from the Title field in the pop-up and click on “Apply (all displays)”. Once complete save the view.

Fig 1.22

Now the homepage shouldn’t have a title.

Enable Archive View

A common component of a blog is an archive view. This is where blog posts are grouped by month. It allows a reader to go back months or years to read older posts.

Drupal 8 comes with a disabled archive view. This consists of a block, which can be added to a sidebar, and a page.

Let’s now enable and configure the view.

1. Go to Structure, Views, scroll down to the Disabled section and enable the Archive view.

Fig 1.3

Once enabled it should appear in the Enabled section.

Now we need to assign the block to a region.

2. Go to Structure and click on “Block layout”.

3. Scroll down to “Sidebar first” and click on “Place block”.

Fig 1.4

4. In the modal pop-up, search for Archive and then click on “Place block”.

Fig 1.4

5. Leave the “Configure block” as is and click on “Save block”.

Once you’re back on the “Block layout” page you should see the block in the region and highlighted green.

Once you’re back on the “Block layout” page you should see the Archive block in the region and it’ll be highlighted green.

Fig 1.6

Scroll to the bottom and click on “Save blocks”.

Now go to the homepage, you should see the “Monthly archive” block in the left sidebar.

Fig 1.7

If you click on any of the months, you’ll be redirected to a page which only displays blog posts for the specific month.

List Categories

On blogs these days you’ll often find a block which lists all the categories with the number of posts within it.

Let’s now create this type of block using Views.

1. Go to Structure, Views and click on “Add new view”.

2. Fill out the “Add new view” form, using the values defined in Table 1.1 and click on “Save and edit”.

Table 1.1. Create a new view

Option Value
View name Popular categories
Machine name popular_categories
Show Taxonomy terms type of Category sorted by Unsorted
Create a page Unchecked
Create a block Checked

Fig 1.8

Once you’re on the Views edit page click on Save.

2. Click on Advanced in the right column and then click on Add in the Relationships section.

3. Select “Content using field_category” and click on “Apply (all displays)”.

Fig 1.9

4. On the next step check “Require this relationship” and click on “Apply (all displays)”.

At this point we’ve created a relationship between the terms and content which uses the Category field we created in an earlier tutorial.

5. Still in the Advanced section, click on “No” next to “Use aggregation” and check Aggregate. Then click on “Apply (all displays)”.

Fig 1.10

In the Preview area you should only see categories which are actually used. Now let’s add a count next to each category.

Fig 1.11

6. Click on Add in the Fields section and select “Content: Node ID”.

Fig 1.23

7. In the next section, select “Count DISTINCT” from “Aggregation type”. Then click on “Apply and continue”.

Fig 1.12

8. On the next step, configure the field if you need to, then click “Apply (all displays)”.

In the preview section, you should see a count value appear below the category.

Fig 1.13

Let’s change how the categories are outputted so the count appears next to the category. For example, “Drupal (3)”.

9. First move the “Content: Node ID” so it appears above “Taxonomy term: Name”. Do this by first clicking on the down arrow next to the Add link in the Fields section.

Fig 1.14

10. In the pop-up, move “Content: Node ID” above “Taxonomy term: Name” and click on “Apply (all displays)”.

11. Click on “Taxonomy term: Name” in the Fields section. Then check “Override the output of this field with custom text” in “Rewrite results”.

Add the following into the Text field:

{{ name }} ({{ nid }})

Fig 1.15

If you look in the preview section, you’ll see the number of posts next to the category.

Fig 1.16

Let’s now hide the count value that appears above the category.

12. Click on “Content: Node ID” field and check “Exclude from display” and click “Apply (all displays)”.

This will hide the field but it won’t remove it.

Finally, let’s add a sort criteria so it’s sorted by number of counts.

13. Click on Add in the “Sort criteria” section.

14. Select “Node ID” and click “Apply (all displays)”.

15. Select “Count DISTINCT” from “Aggregation type” and click on “Apply and continue”.

Fig 1.17

16. In the next section, select “Sort descending” and click on “Apply (all displays)”.

Now save the whole view.

At this point the view is complete, it took a while, but we’re finished. Now we need to add it to the sidebar.

Add View to Sidebar

1. Go to Structure, “Block layout” and click on “Place block” in the “Sidebar first” region.

Fig 1.18

2. Find “Popular categories” and click on “Place block”.

Fig 1.19

3. Reorder the block so it appears above Search.

Fig 1.20

Then scroll to the bottom and click on “Save blocks”.

4. Now go to the homepage and you should see the block in the left sidebar.

Fig 1.21


With Views now in core, standard Drupal 8 is much more useful than any other Drupal release. We were able to create a complex listing with grouping and counts without installing any other module. I’m so happy it’s part of core; it makes perfect sense.

Next, we’ll look at what’s new in the Block system in Drupal 8.

8 thoughts on “Build a Blog in Drupal 8: Using Views”

  1. Hello Ivan,

    Thanks for your tutorial. I’m glad I discovered you and all your contributions to understanding Drupal.
    I have a question about making another archive view. I want to create another archive view instead of the archive view per month.
    As far as I know I can create a complete new view – for example ‘Blog Archive’ – and I can add a new block under displays of the existing Archive view.
    But it is not clear to me when I have to create a complete new view and when adding a new display type to an existing view is sufficient.

    Can you explain that to me?

    Many thanks, Harro

    1. It depends how different the new archive view is.

      If you create another display within the same view, you inherent the other display’s settings. This is good because it saves you a lot of time.

      However, if the view will be drastically different, I recommend you existing Archive view and modify as needed.

  2. Hello Ivan.

    The aggregation feature as explained above was really useful!

    Shall I ask something? I have created a block using Views which displays three pictures. I want to take these pictures and add some code to make them slideshow. So I created a file named block–views-block–my-view-name-block-1.html.twig containing the following:

    {{ content }}

    which of course displays the three images. My question is, how can I address separately each image? For example I would like to achieve something like this:

    {% for item in content %}
    {{ field_image.item }}
    {% endfor %}


    {% for item in content %}
    {{ content.field_image.item }}
    {% endfor %}

    Or what can I write in twig to simply display one specific image? For example sth like {{ content.field_image.1 }} ?

    Thnx a lot in advance.

    1. Hi Rosana,

      This is a very good question, but I don’t know the answer.

      I did a little research and noticed that the templates for views are different in D8 than in D7. Have a look in the templates folder in the views module and see if you can override them.

  3. Is it possible to has sub-headings in the view page? I want content on my view-page to be divided up by content.


  4. suparna debnath

    Hi Ivan,

    First of all thank you so much for this very helpful post. I am also a subscriber of your Youtube channel, those r also very useful. Could you please help me out, who to make the following structure?



Leave a Comment

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

Scroll to Top