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
- I added a region to my theme config, “view_inline_ad.”
- I added the block with the ad it to the region.
- Added the code to render the region to my views template (views-view-unformatted--blog.html.twig)
- 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.