Problem/Motivation

\Drupal\Core\Template\TwigExtension::renderVar() will "render" and empty array or NULL or FALSE to a NULL. Once #3239860: \Drupal\Core\Template\TwigExtension::renderVar() can result in deprecations in PHP 8.1 lands this will change to rendering these values to an empty string.

\Drupal\Core\Template\TwigExtension::renderVar() is very permissive about what it excepts and returns. We have special handling for 0 float and integer values and it can return any scalar type, NULL or an object that implements \Drupal\Component\Render\MarkupInterface. Interestingly, I don't think it will ever return a \Twig\Markup object as per the docs :)

Note: the current behaviour is only depended on in core by Umami and Olivero because they both do things like

  {% if page.pre_header|render|striptags|trim is not empty or
     page.header|render|striptags|trim is not empty %}
    <header class="layout-header" role="banner">
      <div class="container">
        {% if page.pre_header|render|striptags|trim is not empty %}
          {{ page.pre_header }}
        {% endif %}
        {% if page.header|render|striptags|trim is not empty %}
          {{ page.header }}
        {% endif %}
      </div>
    </header>
  {% endif %}

The use of |render|striptags|trim to decide whether to render something causes unexpected side-effects and should NOT be used. See #2937640: Umami theme ignores placeholders and HTML replaced elements when checking for empty regions. It is tempting to deprecate the behaviour so people have to fix their code to not do this.

Steps to reproduce

See test added by #3239860: \Drupal\Core\Template\TwigExtension::renderVar() can result in deprecations in PHP 8.1 - \Drupal\Tests\Core\Template\TwigExtensionTest::testRenderVarEarlyReturn

Proposed resolution

  • Make the method return either a string or a \Drupal\Component\Render\MarkupInterface - i.e. a stringable.
  • Decide how to handle unexpected input values. The docs say it expects String, Object or Render Array. but the code is doing something else.

Remaining tasks

User interface changes

API changes

Data model changes

Release notes snippet

Comments

alexpott created an issue. See original summary.

andypost’s picture

Version: 9.3.x-dev » 9.4.x-dev

Drupal 9.3.0-rc1 was released on November 26, 2021, which means new developments and disruptive changes should now be targeted for the 9.4.x-dev branch. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

johnalbin’s picture

Issue summary: View changes

Version: 9.4.x-dev » 9.5.x-dev

Drupal 9.4.0-alpha1 was released on May 6, 2022, which means new developments and disruptive changes should now be targeted for the 9.5.x-dev branch. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

Version: 9.5.x-dev » 10.1.x-dev

Drupal 9.5.0-beta2 and Drupal 10.0.0-beta2 were released on September 29, 2022, which means new developments and disruptive changes should now be targeted for the 10.1.x-dev branch. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

Version: 10.1.x-dev » 11.x-dev

Drupal core is moving towards using a “main” branch. As an interim step, a new 11.x branch has been opened, as Drupal.org infrastructure cannot currently fully support a branch named main. New developments and disruptive changes should now be targeted for the 11.x branch, which currently accepts only minor-version allowed changes. For more information, see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

Version: 11.x-dev » main

Drupal core is now using the main branch as the primary development branch. New developments and disruptive changes should now be targeted to the main branch.

Read more in the announcement.