twig_real_content
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>',
'<svg>',
'<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
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-...
Supporting this module
Support DROWL's ♥ FOSS work on this module on OpenCollective!
Drupal and this module are FOSS. However, it takes dedicated people to develop and maintain. And they need YOU to give back!
We're committed to building and maintaining Drupal modules that benefit the entire community.
Supporting us on OpenCollective helps us continue to improve, innovate and contribute to Drupal's future. Every pledge makes a difference!
If this module has helped you, we would be very grateful for your donation to support its further development and maintenance.
Support our FOSS development ♥️
You can also speed up the development of features or bugfixes you'd love to see, by sponsoring and giving back!
Let's make Drupal even better, together!
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)