1

So we define the function with this hook...

/**
 * Implements hook_preprocess_bean().
 */
function MODULE_preprocess_bean(&$variables) {

  // Attach css...

  return $variables;
}

What can this be attached to please?

$variables['SOMETHING']['#attached']['css'][] = drupal_get_path('module', 'MODULE') . '/MODULE.css';
1

It can be attached to whatever you like - the important thing is that you render it.

So you could quite simply use:

$foo['#attached']['css'][] = drupal_get_path('module', 'MODULE') . '/MODULE.css';
drupal_render($foo);

Or stick it in a var like you're already doing:

$variables['SOMETHING']['#attached']['css'][] = drupal_get_path('module', 'MODULE') . '/MODULE.css';

And in the template:

drupal_render($SOMETHING);

It works because drupal_render() unconditionally calls drupal_process_attached() on any element it finds with an #attached property, regardless of what you're doing with the result of the render (or if there's even anything to render).

| improve this answer | |
  • 1
    Sorry only just got back, great explanation @Clive, thanks for this, especially about drupal_render() where it calls drupal_process_attached(), didnt know this! – Alex Gill Mar 10 '15 at 14:50
0

You can attach CSS files using the dedicated function drupal_add_css :

 drupal_add_css(drupal_get_path('module', 'MODULE') . '/MODULE.css');

By the way, you don't have to return your $variables since it's passed by reference.

I hope that help :)

| improve this answer | |
  • 3
    The #attached method has been preferred for quite a while - this will work, but isn't so good for caching, and won't future-proof the code for Drupal 8 – Clive Mar 10 '15 at 13:18

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.