Problem/Motivation
We need a way to let type's define attributes.
Proposed resolution
- Setup a #type #pre_render hook for attributes: drupal_pre_render_attributes to build attributes for a type.
Before
// With lazy loading 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:-('
);
}After
// Still converted to Attribute object's but later in the process for the twig layer.
function template_preprocess_something(&$variables) {
// Playing fair and consistant.
$variables['attributes'] = array(
'#type' => 'attributes',
'class' => array('i', 'am', 'a', 'work', 'horse'))
);
$variables['title_attributes'] = array(
'#type' => 'attributes',
'class' => array('me', 'two'))
);
$variables['content_attributes'] = array(
'#type' => 'attributes',
'title' => "not as fast but fair is fair.")
);
// The rest of the gang.
$variables['nested']['attributes'] = array(
'#type' => 'attributes',
'src' => '/img/same.gif')
);
$variables['whatever_attributes'] = array(
'#type' => 'attributes',
'alt' => 'just like funky and the bunch',
);
}Remaining tasks
TBD
API changes
TBD
Related Issues
#1757550: [Meta] Convert core theme functions to Twig templates
#2006282: Refactor Attribute classes - Cleanup, Security, and Readability and minor performance
#2108771: Remove special cased title_attributes and content_attributes for Attribute creation
| Comment | File | Size | Author |
|---|---|---|---|
| #4 | 2048637-4-type-attributes.patch | 4.85 KB | joelpittet |
| #4 | interdiff.txt | 2.56 KB | joelpittet |
| #2 | 2048637-type-attributes.patch | 2.23 KB | joelpittet |
| #2 | 2048637-1-type-attributes.patch | 2.43 KB | joelpittet |
Comments
Comment #1
joelpittetChanging title and adding tags
Comment #1.0
joelpittetadded before/after
Comment #2
joelpittetHere's a patch to see if this will work. I'd much rather return an Attribute from drupal_render()... I think. My problem is that I want the variable passed to twig to be of type Attribute... so my test needs to change and probably have to add in a loop inside theme over all the variables... which could be not so efficient...
I attached my first attempt too which will fail just to show what I did.
Comment #3
jenlamptonI like this idea!
Also see my comment here.
We should not have anything named title_attributes or content_attributes. If we can fix that (by removing those and correctly adding only attributes where needed) this code starts to look much nicer too. :)
Comment #3.0
jenlamptonUpdated issue summary.
Comment #4
joelpittetI'm abusing those default keys for the sake of a quick test. I've typed 'attributes', 'content_attributes', 'title_attributes'.
Also, @Fabianx probably has a better way to deal with all the intricacies of late rendering and converting the attributes at time of printing, but I had a hard time dealing with
{{ test.attributes.class }} {{ test.attributes }}when I tried my hand at doing the 'casting' in twig_render_var().If anybody want's a quick test you can use that to see if things are working.
I'll run a performance test on this patch to see how it fairs with that recursion in there.
Comment #5
joelpittetprofiling is slow, so yeah, need a hand getting this type'd array turned into Attribute either in twig parsing or variable rendering, which ever works with making attribute values drillable.
Comment #7
joelpittet"xxx" is an invalid render array keyis my nemesis. Anyways I'm sure all those errors don't help performance either.Comment #7.0
joelpittetUpdated issue summary.
Comment #8
tim.plunkettComment #9
joelpittetI no longer want to do this... so if someone wants to re-open this giver. And ask any questions of me on IRC if you are curious.