I have bootstrap theme installed in my site. I have two sidebar blocks displaying only in home page. Inner pages just content region displaying. But the issue is though there is no sidebar block the contant region not expanding to full width. I checked the page.html.twig file there the below conditions is given -

{%
set content_classes = [
page.sidebar_first and page.sidebar_second ? 'col-sm-6',
page.sidebar_first and page.sidebar_second is empty ? 'col-sm-9',
page.sidebar_second and page.sidebar_first is empty ? 'col-sm-9',
page.sidebar_first is empty and page.sidebar_second is empty ? 'col-sm-12'
]
%}

and in sidebar section
{# Sidebar Second #}
{% if page.sidebar_second %}
{% block sidebar_second %}

{{ page.sidebar_second }}

{% endblock %}
{% endif %}


When I see the page source I can see the

I expect the to hide.
Can anyone tell what is the issue?
Thanks in advance.

Comments

sudha_B created an issue. See original summary.

markhalliwell’s picture

Assigned: sudha_B » Unassigned
Status: Active » Closed (duplicate)
Related issues: +#953034: [meta] Themes improperly check renderable arrays when determining visibility

This is a core issue. Don't ask me to explain it, it's too complicated. Read all the comments in the related issue (especially closer to the end).

ressa’s picture

I found a post with a proposed solution. I tried the concept out in the Bootstrap page.html.twig file, and it seems to work.

Current Bootstrap page.html.twig file, around line 131:

{# Content #}
{%
  set content_classes = [
    page.sidebar_first and page.sidebar_second ? 'col-sm-6',
    page.sidebar_first and page.sidebar_second is empty ? 'col-sm-9',
    page.sidebar_second and page.sidebar_first is empty ? 'col-sm-9',
    page.sidebar_first is empty and page.sidebar_second is empty ? 'col-sm-12'
  ]
%}

With added checks, to see if the regions are empty or not:

{# Content #}
{%
  set content_classes = [
    page.sidebar_first|render|striptags|trim and page.sidebar_second|render|striptags|trim ? 'col-sm-6',
    page.sidebar_first|render|striptags|trim and page.sidebar_second|render|striptags|trim is empty ? 'col-sm-9',
    page.sidebar_second|render|striptags|trim and page.sidebar_first|render|striptags|trim is empty ? 'col-sm-9',
    page.sidebar_first|render|striptags|trim is empty and page.sidebar_second|render|striptags|trim is empty ? 'col-sm-12'
  ]
%}

It looks clunky, but an empty sidebar_second is no longer printed out.

ressa’s picture

Similarly, if you don't want to show empty blocks, update block.html.twig:

{% if content|render|striptags|trim %}
  <section{{ attributes.addClass(classes) }}>
    {{ title_prefix }}
    {% if label %}
      <h2{{ title_attributes.addClass('block-title') }}>{{ label }}</h2>
    {% endif %}
    {{ title_suffix }}

    {% block content %}
      {{ content }}
    {% endblock %}
  </section>
{% endif %}
markhalliwell’s picture

You cannot use the striptags Twig filter as it would strip out all valid HTML that's in the content as well, if it isn't actually empty.

Like I said, this is a core bug... one that is very complex and complicated.

ressa’s picture

Thanks for commenting. What do you do, to avoid printing out empty regions and blocks?

markhalliwell’s picture

Nothing.... that's why it's a major core bug.

hkirsman’s picture

As @markcarver warned, I used this method and now debugged it for a day to find why my block is not visible:
<drupal-render-placeholder callback="Drupal\block\BlockViewBuilder::lazyBuilder" arguments="0=eventregistrationform&amp;1=full&amp;2" token="afb5cea2"></drupal-render-placeholder>
I was expecting the region to have actual html in it but Drupal switches from html to placeholders according to whatever the internal mechanics are.

marcel tuchek’s picture

3 years ago and that bug still isn't fixed? Or have I missed something?

wotney’s picture

Following

thalemn’s picture

Following

ressa’s picture

Just a tip to @wotney and @thalemn: You can click the "Follow - 10 Followers" link on the right hand side to subscribe to an issue.