Change record status: 
Project: 
Introduced in branch: 
8.x
Description: 





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.

Impacts: 
Module developers
Themers
Updates Done (doc team, etc.)
Online documentation: 
Not done
Theming guide: 
Not done
Module developer documentation: 
Not done
Examples project: 
Not done
Coder Review: 
Not done
Coder Upgrade: 
Not done
Other: 
Other updates done

Comments

AndyThornton’s picture

I think it should be ...

    'container' => array(
      '#attributes' => array('class' => array('bar')),
    ),
Tichris59’s picture

I tested this usage and if you need to add a class to a specific theme wrapper, you should use like this :

array(
  '#theme' => 'image',
  '#attributes' => array('class' => 'foo'),
  '#theme_wrappers' => array(
    'container' => array(
      '#wrapper_attributes' => array('class' => array('bar') ),
    ),
    'custom_container' => array(
      '#wrapper_attributes' => array('class' => array('baz') ),
    ),
  ),
);

#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 :

 $form["my_element"]['#wrapper_attributes'] = ['class' => 'bar'];