6

I want to remove default Drupal CSS files if it is a front page in Drupal 7 theme.

Following is the code (got from http://www.grahamgilchrist.com/blog/25) that I am trying to put in the template.php for barktik1 theme. How do I check if rendering page is a front page or not? Only $css variable is passed as an argument to the theme_css_alter() function while for other process functions $variables is passed from which it is easy to determine if it is a front page or not.

function bartik1_css_alter(&$css) { 

//do something to check if it is a front page
if ($is_front){
    //make a list of module css to remove 
    $css_to_remove = array(); 
    $css_to_remove[] = drupal_get_path('module','system').'/system.base.css'; 
    $css_to_remove[] = drupal_get_path('module','system').'/system.menus.css'; 

    // now we can remove the contribs from the array 
    foreach ($css_to_remove as $index => $css_file) { 
      unset($css[$css_file]); 
    } 
    }
} 

Thanks!

3 Answers 3

6

The function you're looking for is drupal_is_front_page().

0

In both Drupal 7 and 8 you have hook_css_alter for altering CSS files before output on the page.

If you want to remove some default css file from your Drupal 7 home page you can go with:

function YOUR_THEME_css_alter(&$css) {
  if (drupal_is_front_page() == TRUE  )  {
    unset($css['/'.drupal_get_path('theme', 'YOUR_THEME').'/css/removeme.css'] );
  }
}
0

To remove CSS files from a module and theme on a Drupal 8 page, you can follow these steps:

Remove single file

function YOUR_THEME_css_alter(&$css) {
    // Remove CSS files from modules.

  // Example: Remove 'defaults.css' from the 'system' module using the service method.
     $system_module_path = \Drupal::service('extension.list.module')->getPath('system') . '/defaults.css';
     unset($css[$system_module_path]);

// Example: Remove custom module CSS file.
    unset($css['modules/custom/ms_ajax_form_example/css/ms_ajax_form_example.css']);

// Example: Remove contributed module CSS file.
    unset($css['modules/contrib/module-machine-name/css/example.theme.css']);

// Remove CSS files from themes.

// Example: Remove 'align.module.css' from the 'stable' theme using the service method.
    $theme_path = \Drupal::service('extension.list.theme')->getPath('stable') . '/css/system/components/align.module.css';
    unset($css[$theme_path]);

// Example: Remove a custom theme's CSS file.
    unset($css['themes/custom/theme-machine-name/assets/css/jquery-ui.min.css']);
}

Remove all CSS files that belong to the stable theme

function YOUR_THEME_css_alter(&$css) {
    // Get the path to the 'stable' theme.
    $theme_path = \Drupal::service('extension.list.theme')->getPath('stable');

    // Iterate through the CSS array and remove any files that belong to the 'stable' theme.
    foreach ($css as $path => $value) {
        if (strpos($path, $theme_path) === 0) {
            unset($css[$path]);
        }
    }
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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