Support for Drupal 7 is ending on 5 January 2025—it’s time to migrate to Drupal 10! Learn about the many benefits of Drupal 10 and find migration tools in our resource center.
By 2dareis2do on
As suggested by paraview, I am trying to to replace drupal_add_css(), which works well within hook_page_build(), with #attached but seem to be having issues getting the form working. So I currently have:
/**
* Implements hook_page_build().
*/
function css_reset_page_build() {
$path = libraries_get_path('css_reset');
// Needs to be in the CSS_SYSTEM group rather than the CSS_DEFAULT group.
$options = array(
'group' => CSS_SYSTEM,
'every_page' => TRUE,
'media' => 'all',
'preprocess' => TRUE,
'weight' => '-1001',
);
// trying to replace
// drupal_add_css($path . '/reset.css', $options);
/* use attached instead */
$page['#attached']['css'][$path . '/reset.css'] = $options;
}
However the attached stylesheet is not rendering. What am I doing wrong?
Comments
OK I think I have figured it
OK I think I have figured it out:
I need to add
so
You're missing the $page
You're missing the $page argument in your function call. It should be this:
You don't need to return anything.
Ok but If I do it that way
Ok but If I do it that way the stylesheet does not appear to be returned?
https://www.drupal.org/node/2508285
$page is passed by reference,
$page is passed by reference, so you don't need to return anything.
You may need to add #attached to one of the sub elements.
Personally, I do this in hook_page_alter():