Adding an Inline Style Attribute to an Entity in Drupal 7
The point of this post is to illustrate adding inline styles without creating template files. Normally we add classes via $vars['classes_array'] but sometimes inline styles are required.
This code isn’t too useful as written (make all text red for all entity types, really?), but drop in a conditional to affect specific entity types and replace the styles with something useful like setting a background image URL from a field within the entity and you have a handy snippet.
1 2 3 4 5 6 7 8 9 10 11 12 |
/** * Implements hook_preprocess_HOOK(). */ function mymodule_preprocess_entity(&$vars) { $styles = 'color: #ff0000;'; if (!isset($vars['attributes_array']['style'])) { $vars['attributes_array']['style'] = $styles; } else { $vars['attributes_array']['style'] .= $styles; } } |