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

2dareis2do’s picture

OK I think I have figured it out:

I need to add

  return drupal_render($page);

so

/**
 * 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;
  return drupal_render($page);
}
Jaypan’s picture

You're missing the $page argument in your function call. It should be this:

function css_reset_page_build(&$page) {

You don't need to return anything.

2dareis2do’s picture

Ok but If I do it that way the stylesheet does not appear to be returned?

https://www.drupal.org/node/2508285

Jaypan’s picture

$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():

function HOOK_page_alter(&$page)
{
  $page['page_bottom'][] = array
  (
    '#attached' => array
    (
      'css' => array
      (
        array
        (
          'type' => 'file',
          'data' => 'path/to/some/file.css',
        ),
      ),
    ),
  );
}