Remove all CSS files except current theme files

Function is placed in template.php.


/**
 * Implements hook_css_alter().
 */
function MYTHEME_css_alter(&$css) {
 
  dpm('-- Before unset --');
  dpm($css); 
 
  foreach ($css as $key => $value) {
    if ($value['group'] != CSS_THEME) {
     unset($css[$key]);
    }
  }

  dpm('-- After unset --');
  dpm($css);
   
}

CSS_THEME is a constant defined in common.inc and refers to the group of theme CSS files


/**
 * The default group for theme CSS files added to the page.
 */
define('CSS_THEME', 100);

Output when using dpm: (devel module)

cleaned_up_css.png

However, it is good practice to also include the CSS files defined in the Drupal 7 core system module. The PHP function in_array can be used to keep both groups.


/**
 * Implements hook_css_alter().
 */
function subtheme_css_alter(&$css) {
 
  foreach ($css as $key => $value) {
    if (!in_array($value['group'] ,array(CSS_THEME, CSS_SYSTEM))) {
     unset($css[$key]);
    }
  }
   
}

Add new comment

Plain text

  • No HTML tags allowed.
  • Web page addresses and e-mail addresses turn into links automatically.
  • Lines and paragraphs break automatically.
By submitting this form, you accept the Mollom privacy policy.