Summary:
Since render elements can accept multiple theme wrappers, we need a way to pass unique arguments to each one.
In Drupal 7 this was impossible since the '#wrapper_attributes' were passed blindly to all theme_wrappers, and to make matters worse, these attributes would only be used if the theme_wrapper specifically listened for those '#wrapper_attributes' and applied them.
In Drupal 8, we've allowed individual variables to be passed to each theme_wrapper by changing the structure of the theme_wrapper array.
Code example:
If we have a render element, and we need to pass the class 'bar' as an attribute for the theme wrapper for that element:
Before (D7):
array(
'#theme' => 'image',
'#attributes' => array('class' => 'foo'),
'#theme_wrappers' => array('container', 'custom_container'),
'#wrapper_attributes' => array('class' => 'bar'),
);
After (D8):
array(
'#theme' => 'image',
'#attributes' => array('class' => 'foo'),
'#theme_wrappers' => array(
'container' => array(
'#attributes' => array('class' => 'bar'),
),
'custom_container' => array(
'#attributes' => array('class' => 'baz'),
),
),
);
The array syntax is entirely optional and using a string as in D7 will work and just use the same #attributes as the theme hook.
Comments
Small error
I think it should be ...
I tested this usage and if
I tested this usage and if you need to add a class to a specific theme wrapper, you should use like this :
#attributes in the declaration of theme wrappers doesn't work for me, wrapper_attributes works like a charm
instead if you have a single theme_wrapper, you should use this code instead :