Core themes should not throw exceptions when strict_variables is TRUE

Created on 4 March 2015, about 10 years ago
Updated 29 May 2025, 2 days ago

Beta phase evaluation

<!--Uncomment the relevant rows for the issue. -->

Problem/Motivation

https://www.drupal.org/node/1806538 resolved that strict_variables should not be enabled by default in Twig. This decision has allowed us to get sloppy in core themes. Core themes should reflect best practices and allow subthemes to use strict_variables without throwing exceptions.

Our coding standards require strict variables in JavaScript.

More details

In form-element.html.twig, in the documentation says the description_display variable exists:

...
 * - description_display: Description display setting. It can have these values:
 *   - before: The description is output before the element.
 *   - after: The description is output after the element. This is the default
 *     value.
 *   - invisible: The description is output after the element, hidden visually
 *     but available to screen readers.
...

(note: not “optional” like the description variable). However template_preprocess_form_element() does not guarantee description_display will be available in $variables. The result is an exception if you enable any core theme and turn strict_variables on.

Proposed resolution

If a variable is listed as "(optional)" in the template documentation, then the template should verify its existence before using it. Otherwise, the preprocess function should provide the variable – even if NULL – so that templates can be kept as clean as possible.

For example, if the documentation says that either foo or foo.bar is optional, then a template should use:

{{ foo.bar|default }}

or

{% if foo.bar|default is not empty %} 
  <div class="this-is-a-foo">{{ foo.bar }}</div> 
{% endif %}

The more verbose (and potentially more readable to new users) version would be:

{{ foo.bar|default('') }}

or

{% if foo.bar is defined and foo.bar is not empty %} 
  <div class="this-is-a-foo">{{ foo.bar }}</div> 
{% endif %}

But if the documentation says that foo.bar is available, then a template should not need to check for it as the preprocess function should provide, at a minimum, a NULL placeholder.

{{ foo.bar }}

or

{% if foo.bar is not empty %}
  <div class="this-is-a-foo">{{ foo.bar }}</div>
{% endif %}

Remaining tasks

  1. Update preprocess functions and Twig template documentation so that variables are correctly accounted for.
  2. Figure out if it's appropriate to turn on strict_variables on the testbot.

User interface changes

None.

API changes

None.

📌 Task
Status

Postponed: needs info

Version

11.0 🔥

Component

theme system

Created by

🇺🇸United States mikeker

Live updates comments and jobs are added and updated live.
  • stale-issue-cleanup

    To track issues in the developing policy for closing stale issues, [Policy, no patch] closing older issues

Sign in to follow issues

Comments & Activities

Not all content is available!

It's likely this issue predates Contrib.social: some issue and comment data are missing.

  • 🇺🇸United States smustgrave

    Thank you for creating this issue to improve Drupal.

    We are working to decide if this task is still relevant to a currently supported version of Drupal. There hasn't been any discussion here for over 8 years which suggests that this has either been implemented or is no longer relevant. Your thoughts on this will allow a decision to be made.

    Since we need more information to move forward with this issue, the status is now Postponed (maintainer needs more info). If we don't receive additional information to help with the issue, it may be closed after three months.

    Thanks!

Production build 0.71.5 2024