Remove special cased title_attributes and content_attributes for Attribute creation

Created on 10 October 2013, almost 12 years ago
Updated 23 July 2025, 1 day ago

Follow-up from #1982024: Lazy-load Attribute objects later in the rendering process only if needed .

Problem/Motivation

We have 3 special attribute variables in preprocess: 'attributes', 'title_attributes', and 'content_attributes'.
When built in preprocess they are created as arrays while all other $variables keys for attributes need to be instantiated as new Attribute(...)

This presents a DX issue where you need to know something about the internals to understand how it works.

There are two things that this does 'for' us that I'm aware:

  1. It makes those un-Attribute()'ed variables easier to merge as arrays (RDF module I believe does this)
  2. Provides a 5.652ms performance gain #1982024-7: Lazy-load Attribute objects later in the rendering process only if needed for wall time. Which could be solved with this #2048637: Add #type => 'attributes' and wrap in Attribute class with drupal_pre_render_attributes

With lazy loading certain $variables keys ('attributes', 'content_attributes', 'title_attributes')


// 
function template_preprocess_something(&$variables) {
  // Special variable keys.
  $variables['attributes'] = array('class' => array('i', 'am', 'lazy', 'and', 'special'));
  $variables['title_attributes'] = array('class' => array('me', 'two'));
  $variables['content_attributes'] = array('title' => "Don't forget me too");
  // The rest of the gang.
  $variables['nested']['attributes'] = new Attribute(array('src' => '/not/special/either.gif'));
  $variables['whatever_attributes'] = new Attribute(array('alt' => 'Un fair:-(');
}

Without lazy loading

function template_preprocess_something(&$variables) {
  // Playing fair and consistant.
  $variables['attributes'] = new Attribute(array('class' => array('i', 'am', 'a', 'work', 'horse')));
  $variables['title_attributes'] = new Attribute(array('class' => array('me', 'two')));
  $variables['content_attributes'] = new Attribute(array('title' => "not as fast but fair is fair."));
  // The rest of the gang.
  $variables['nested']['attributes'] = new Attribute(array('src' => '/img/same.gif'));
  $variables['whatever_attributes'] = new Attribute(array('alt' => 'just like funky and the bunch');
}

Proposed resolution

Remove this chunk of code from the bloated theme() function


  if (!isset($default_attributes)) {
      $default_attributes = new Attribute();
    }
    foreach (array('attributes', 'title_attributes', 'content_attributes') as $key) {
      if (isset($variables[$key]) && !($variables[$key] instanceof Attribute)) {
        if ($variables[$key]) {
          $variables[$key] = new Attribute($variables[$key]);
        }
        else {
          // Create empty attributes.
          $variables[$key] = clone $default_attributes;
        }
      }
    }

And instantiate those attributes when they are needed/used in the templates and not before.

Update

Removing all 3 was challenging in one patch. This patch is now dedicated to only remove title_attributes and content_attributes. A follow-up will be opened to deal with the attributes key after this one is committed.

Remaining tasks

  • Create follow-up issue to finish the job with $variables['attributes']

User interface changes

N/A

API changes

N/A

Related Issues

📌 Task
Status

Postponed: needs info

Version

11.0 🔥

Component

theme system

Created by

🇨🇦Canada joelpittet Vancouver

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

    A change record needs to be drafted before an issue is committed. Note: Change records used to be called change notifications.

  • 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