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:
- It makes those un-Attribute()'ed variables easier to merge as arrays (RDF module I believe does this)
- 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