By Mark_L6n on
In a template file, before running the code print render($content['field_image']); I would like to add a CSS class to the image in the $content['field_image'] render array. How can I do this?
In a template file, before running the code print render($content['field_image']); I would like to add a CSS class to the image in the $content['field_image'] render array. How can I do this?
Comments
I think this will
I think this will work.
You would think that'd work,
You would think that'd work, wouldn't you! I did that, and double-checked with debug code that
$content['field_image']['#attributes']['class'][0] = 'your_class'exists. However, 'your_class' doesn't get added to any HTML code from that.What's the field type?
What's the field type?
#field_type = image
#field_type = image
Here's the render array after
Here's the render array after adding the #attributes part:
And here's the HTML that gets output:
Some parts of the render array and HTML don't seem to be correlated: where do the classes come from?
<div class="field field-name-field-image field-type-image field-label-hidden" >They don't appear in the render array.For anyone stumbling across
For anyone stumbling across this for Drupal 7, you will not be able to apply attributes without overriding a theme function. Attributes are not passed from the render array to the theme image functions. I have proposed a fix for the image module here: -
http://drupal.org/node/1451820
I've also written a blog article about how to work around the problem here.
Thread moved over to
Thread moved over to http://drupal.stackexchange.com/questions/10133/how-do-you-add-a-css-cla... . Please post any further comments there. Thanks!
For theme fields, it is
For theme fields, it is necessary to copy a file modules/field/theme/field.tpl.php in a folder templates your theme and to give it a new name according to one of templates:
field - FIELD_TYPE.tpl.php - the template will be applied to all fields of type FIELD_TYPE. For example, that themes all fields of type Image, it is necessary to create a template with a name field - image.php.
field - FIELD_NAME.tpl.php - the template will be applied to a field with name FIELD_NAME. For example, that themes a field with a name field_images, it is necessary to create a template with a name field - field_images.tpl.php.
field - BUNDLE.tpl.php - the template will be applied to all fields added to essence with type BUNDLE. For example, that themes all fields in a type material page, it is necessary to create a template with a name field - page.tpl.php.
field - FIELD_NAME - BUNDLE.tpl.php - the template will be applied only to field FIELD_NAME which is added to essence with type BUNDLE. For example, that themes a field field_images in a type material page, it is necessary to create a template with a name field - field_images - page.tpl.php.
Issue seems fixed in core
This issue seems fixed in core 7.23
In the node tpl add this at the top:
It works
I can confirm that this works as of Drupal 7.24.
Confirm
I can confirm that this works as of Drupal 7.26.
On Drupal 7.43, in theme
On Drupal 7.43, in theme_preprocess_node.
This didn't work:
$variables['content'][FIELD_NAME]['#items'][0]['attributes']['class'][] = 'your-class';but this worked:
$variables['content'][FIELD_NAME][0]['#options']['attributes']['class'][] = 'your-class';