Remove specific stylesheets from a given page
Question
Adding a stylesheet to specific pages is easy: implement hook_preprocess_page()
, call drupal_add_css(...)
and set $vars['styles'] = drupal_get_css()
.
But what is the proper way to remove a stylesheet (or in my case, all "media=screen" stylesheets) for a specific page, without interfering with the caching system ? (e.g. not some regex applied to the output)
You can't just do unset($vars['css']['screen'])
because $vars['styles']
has already been generated at this point and there exists no drupal_remove_css()
function.
Solution
This seems to be the cleanest way, in hook_preprocess_page()
:
$css = drupal_add_css();
unset( $css['screen'] );
$vars['styles'] = drupal_get_css($css);
Licensed under: CC-BY-SA with attribution
Not affiliated with drupal.stackexchange