Inserting a Block into a View Listing in Drupal 8

Sean Reiser

Inserting a Block into a View Listing in Drupal 8


My blog homepage is basically an activity feed, a combination of Blog Posts, Short Notes, Images and Links, much like a facebook wall or a tumblog.  I decided I wanted to add a link to one of my other sites in the feed, basically a “house ad”.  Blatantly stealing from facebook, I decided that I wanted the second item in the feed. My goal is to create something like this:

In Drupal 7, I would have used preprocess_views_view to insert the ad into the view, but I wanted to do something that was more sustainable and not as hard coded.  Although on my blog I perform all roles (developer, designer, themes, content admin, etc), I wanted to give non-developers the ability to add, change and delete the advertisement.  So I decided to use Drupal's block interface.

I did a little investigation and found that the Twig Tweak module provides a twig function to fetch and drupal regions (and blocks, entities, fields and other Drupalisms). Here's a list of of the functions Twig Tweak provides. The drupal_region twig would do what I need.

Here’s what I did

  1. I added a region to my theme config, “view_inline_ad.”
  2. I added the block with the ad it to the region.
  3. Added the code to render the region to my views template (views-view-unformatted--blog.html.twig)
    1. {% if title %}
    2. <h3>{{ title }}</h3>
    3. {% endif %}
    4. {% for row in rows %}
    5. {%
    6. set row_classes = [
    7. default_row_class ? 'views-row',
    8. loop.first ? 'views-row-first',
    9. loop.last ? 'views-row-last',
    10. ]
    11. %}
    13. {% if loop.index == 2 %}
    14. {{ drupal_region('view_inline_ad') }}
    15. {% endif %}
    17. <div{{ row.attributes.addClass(row_classes) }}>
    18. {{ row.content }}
    19. </div>
    20. {% endfor %}
  4. Cleared Cache, loaded the page and rock and roll.

One problem I ran into, on my site the blog theme is not the default theme. I host multiple sites out of the same Drupal instance and am using the Switch Page Theme module to change theme based on site context. drupal_region() assumes you're using the default theme and not the current theme so it couoldn't find the region. It does accept a second parameter, theme. I popped the theme's machine name in there and it worked like a charm.