Module Description
Provides a "real_content" twig filter and test to determine if a certain twig variable (typically a region) has meaningful content is empty. Read the super long core issue for details.
Core issue #953034: [meta] Themes improperly check renderable arrays when determining visibility Once the core issue is solved (which we guess will not be soon, if ever, due to logical problems), this module may become obsolete.
TL;DR If you need to check if a region is empty in twig, for example to not output the wrappers at all, this module is what you want, and the core issue describes the reasons why this is so complicated.
Functionality & how to use As Twig filter:
Adds a |real_content twig filter to run on rendered variables, which filters out all wrappers and trims the code to check if there is real content.
{% set sidebar_first_rendered = page.sidebar_first|render %} {% if sidebar_first_rendered|real_content is not empty %} <div class="sidebar sidebar--first"> {{ sidebar_first_rendered }} </div> {% endif %}
Without this module, it would be something like this:
{% set sidebar_first_rendered = page.sidebar_first|render %} {% if sidebar_first_rendered|striptags('<drupal-render-placeholder><img><video><[...]>')|trim is not empty %} <div class="sidebar sidebar--first"> {{ sidebar_first_rendered }} </div> {% endif %} As Twig Test:
Adds an is real_content twig test to run on rendered variables, which filters out all wrappers and trims the code to check if there is real content. Returns TRUE if there is real content, else FALSE.
{% set sidebar_first_rendered = page.sidebar_first|render %} {% if sidebar_first_rendered is real_content %} <div class="sidebar sidebar--first"> {{ sidebar_first_rendered }} </div> {% endif %} With Twig Blocks:
Inherited Twig blocks are already rendered, so you can do:
{% extend 'paragraphs.html.twig' %} {% block paragraph %} {% if parent() is real_content %} {{ parent() }} {% endif %} {% endblock paragraph %} Unstripped DOM elements The following HTML elements are not being stripped, as they typically have no own DOM text content. If they are present, they are treated as existing content.
[ '<drupal-render-placeholder>', '<embed>', '<hr>', '<iframe>', '<img>', '<input>', '<link>', '<object>', '<script>', '<source>', '<style>', '<video>', ]; Please create an issue, if further exclusions are required. We're also discussing to put this into config here: #3293420: Make allowed tags configurable
Render arrays and performance As rendering a render array multiple times has negative impact on performance, we decided to disallow running this on (unrendered) render arrays directly. Perhaps someone is interested to implement this: #3293416: Allow on render arrays and combine with isEmpty()
Related Related modules
* Twig Capture
* Twig Tweak (also see #3268793: Add Empty Check Helpers)
* Twig Field Value
Related discussions & blog posts
* #953034: [meta] Themes improperly check renderable arrays when determining visibility
* https://drupal.stackexchange.com/questions/175389/how-do-i-properly-dete...
* https://www.drupal.org/forum/support/module-development-and-code-questio...
* https://www.previousnext.com.au/blog/right-way-check-empty-content-twig
* http://www.longlivethemonkey.com/syntaxnotes/?p=351
* https://drupal.stackexchange.com/questions/198570/verify-a-field-is-not-...
Development proudly sponsored by German Drupal Friends & Companies: webks: websolutions kept simple (https://www.webks.de) and DROWL: Drupalbasierte Lösungen aus Ostwestfalen-Lippe (OWL), Germany (https://www.drowl.de)
Core issue #953034: [meta] Themes improperly check renderable arrays when determining visibility Once the core issue is solved (which we guess will not be soon, if ever, due to logical problems), this module may become obsolete.
TL;DR If you need to check if a region is empty in twig, for example to not output the wrappers at all, this module is what you want, and the core issue describes the reasons why this is so complicated.
Functionality & how to use As Twig filter:
Adds a |real_content twig filter to run on rendered variables, which filters out all wrappers and trims the code to check if there is real content.
{% set sidebar_first_rendered = page.sidebar_first|render %} {% if sidebar_first_rendered|real_content is not empty %} <div class="sidebar sidebar--first"> {{ sidebar_first_rendered }} </div> {% endif %}
Without this module, it would be something like this:
{% set sidebar_first_rendered = page.sidebar_first|render %} {% if sidebar_first_rendered|striptags('<drupal-render-placeholder><img><video><[...]>')|trim is not empty %} <div class="sidebar sidebar--first"> {{ sidebar_first_rendered }} </div> {% endif %} As Twig Test:
Adds an is real_content twig test to run on rendered variables, which filters out all wrappers and trims the code to check if there is real content. Returns TRUE if there is real content, else FALSE.
{% set sidebar_first_rendered = page.sidebar_first|render %} {% if sidebar_first_rendered is real_content %} <div class="sidebar sidebar--first"> {{ sidebar_first_rendered }} </div> {% endif %} With Twig Blocks:
Inherited Twig blocks are already rendered, so you can do:
{% extend 'paragraphs.html.twig' %} {% block paragraph %} {% if parent() is real_content %} {{ parent() }} {% endif %} {% endblock paragraph %} Unstripped DOM elements The following HTML elements are not being stripped, as they typically have no own DOM text content. If they are present, they are treated as existing content.
[ '<drupal-render-placeholder>', '<embed>', '<hr>', '<iframe>', '<img>', '<input>', '<link>', '<object>', '<script>', '<source>', '<style>', '<video>', ]; Please create an issue, if further exclusions are required. We're also discussing to put this into config here: #3293420: Make allowed tags configurable
Render arrays and performance As rendering a render array multiple times has negative impact on performance, we decided to disallow running this on (unrendered) render arrays directly. Perhaps someone is interested to implement this: #3293416: Allow on render arrays and combine with isEmpty()
Related Related modules
* Twig Capture
* Twig Tweak (also see #3268793: Add Empty Check Helpers)
* Twig Field Value
Related discussions & blog posts
* #953034: [meta] Themes improperly check renderable arrays when determining visibility
* https://drupal.stackexchange.com/questions/175389/how-do-i-properly-dete...
* https://www.drupal.org/forum/support/module-development-and-code-questio...
* https://www.previousnext.com.au/blog/right-way-check-empty-content-twig
* http://www.longlivethemonkey.com/syntaxnotes/?p=351
* https://drupal.stackexchange.com/questions/198570/verify-a-field-is-not-...
Development proudly sponsored by German Drupal Friends & Companies: webks: websolutions kept simple (https://www.webks.de) and DROWL: Drupalbasierte Lösungen aus Ostwestfalen-Lippe (OWL), Germany (https://www.drowl.de)
Module Link
Project Usage
883
Security Covered
Covered By Security Advisory
Version Available
Production
Module Summary
This module provides a Twig filter and test to check if a region has meaningful content in order to avoid outputting unnecessary wrappers in Twig templates.
Data Name
twig_real_content