Empty Region Displaying In Drupal 8

PROBLEM
This is a strange one but one that’s been talked about many, many times in Drupal 8. In Drupal 8, if you check to see if a region is empty, it isn’t empty. You need to do a few things to adequately determine if a region is truly empty.

See these:

https://www.drupal.org/node/953034
https://www.drupal.org/forum/support/module-development-and-code-questions/2016-04-07/drupal-8-regions-with-and-empty
https://drupal.stackexchange.com/questions/175389/how-do-i-properly-detect-if-region-is-empty

So, I would like to determine if a block is empty before I display it so I can put a Bootstrap wrapper around it. However, just using:

{% if page.left_sidebar %}
	<div class="left_sidebar">{{ page.left_sidebar }}</div>
{% endif %}

won’t work. To give some background, the region on most pages has a secondary menu (a left navigation). However, on pages where this doesn’t display, it would display an empty region even though there was no menu there.

SOLUTION
At first I did:

{% if page.left_sidebar|render|striptags|trim %}
	<div class="left_sidebar">{{ page.left_sidebar }}</div>
{% endif %}

and this seemed to work! However, today, when I got to putting a webform in a region, it didn’t work. Striptags for some reason took out everything. Come to find out Webforms use some sort of “lazyBuilding” or something I don’t give a shit about. #stupidShit

Anyway, now my code above doesn’t work. But buried deep in one of those links above is this gem:

{% if page.left_sidebar|render|striptags("<drupal-render-placeholder>")|trim %}
	<div class="left_sidebar">{{ page.left_sidebar }}</div>
{% endif %}

This seems to work for the moment. If this turns out to not work later (which it might not for other needs… or in like 10 minutes when I find another issue), I will re-evaluate.

Get Link Attributes In Twig Of Menu In Drupal 8

PROBLEM
We’re using Pattern Lab with our Drupal 8 installation so I will need to get the link attributes of menu items so I can pass them to the link pattern. I am using the Link Attributes Widget for giving the option of opening links in new windows and adding classes to links.

However, I need to be able to get these attributes from the Drupal twig template to pass them into the Pattern Lab template.

I tried to do this months ago but couldn’t figure it out. Months later, I’m a lot better at twig than before. I went through looping through the items in item and getting the keys. Nothing was working. The only thing I could do was to output the link using the link function in Drupal. That would properly output the link.

But I wanted to use the link pattern in Pattern Lab!


SOLUTION
After a while at this, I stumbled upon a post on Drupal of someone trying to do the same thing (just get the attributes of a damn menu link in twig)!

Use this code to get the target and the classes. (This is looping through the items array… i.e. for item in items) Note that class is in an array so you might want to add |join(” “) afterward.

item.url.getOption("attributes").target
item.url.getOption("attributes").class

Now I can move forward!

Extra… But by the way, normally to get the target and classes of these for regular field links, use this:

field_link[0].options.attributes.target
field_link[0].options.attributes.class|join(" ")