#content_attributes and #title_attributes on element do not bubble up to $variables

Created on 30 September 2019, about 5 years ago
Updated 2 August 2023, over 1 year ago

When declaring a new render element, we cannot define default values for #content_attributes or #title_attributes since they will not bubble up to the $variables array. Instead, an empty Attribute object will be created. #attributes DOES bubble up correctly because of how it is handled in the template_preprocess hook:

\web\core\include\theme.inc

/**
 * Adds a default set of helper variables for preprocessors and templates.
 *
 * This function is called for every theme hook. It is the first in the
 * sequence of preprocessing functions called when preparing variables for a
 * template.
 *
 * See the @link themeable Default theme implementations topic @endlink for
 * details.
 */
function template_preprocess(&$variables, $hook, $info) {
  // Merge in variables that don't depend on hook and don't change during a
  // single page request.
  // Use the advanced drupal_static() pattern, since this is called very often.
  static $drupal_static_fast;
  if (!isset($drupal_static_fast)) {
    $drupal_static_fast['default_variables'] = &drupal_static(__FUNCTION__);
  }
  $default_variables = &$drupal_static_fast['default_variables'];
  if (!isset($default_variables)) {
    $default_variables = _template_preprocess_default_variables();
  }
  $variables += $default_variables;

  // When theming a render element, merge its #attributes into
  // $variables['attributes'].
  if (isset($info['render element'])) {
    $key = $info['render element'];
    if (isset($variables[$key]['#attributes'])) {
      $variables['attributes'] = NestedArray::mergeDeep($variables['attributes'], $variables[$key]['#attributes']);
    }
  }
}

`#content_attributes` and `#title_attributes` should receive the same treatment.

🐛 Bug report
Status

Needs work

Version

11.0 🔥

Component
Theme 

Last updated 1 day ago

Created by

🇺🇸United States Etroid

Live updates comments and jobs are added and updated live.
  • Needs tests

    The change is currently missing an automated test that fails when run with the original code, and succeeds when the bug has been fixed.

  • Needs issue summary update

    Issue summaries save everyone time if they are kept up-to-date. See Update issue summary task instructions.

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.

Production build 0.71.5 2024