Drupal 10. How to unset theme.css in custom theme?

Created on 18 September 2023, 9 months ago
Updated 18 October 2023, 8 months ago

In Drupal 9 was possible to unset css files from this module by editing mytheme.info.yml

stylesheets-remove:  
  - modules/contrib/jquery_ui/assets/vendor/jquery.ui/themes/base/accordion.css
  - modules/contrib/jquery_ui/assets/vendor/jquery.ui/themes/base/theme.css

How to do that in Drupal 10?

I try

libraries-override:
  jquery_ui/core:
    css:
      component:          
        /modules/contrib/jquery_ui/assets/vendor/jquery.ui/themes/base/theme.css: false
        /modules/contrib/jquery_ui/assets/vendor/jquery.ui/themes/base/accordion.css : false

and

libraries-override:
  jquery_ui/core:
    css:
      component:  
       assets/vendor/jquery.ui/themes/base/theme.css: false
       assets/vendor/jquery.ui/themes/base/accordion.css : false

But it's doesn't work.

📌 Task
Status

Fixed

Version

1.6

Component

Miscellaneous

Created by

🇺🇦Ukraine VasyOK

Live updates comments and jobs are added and updated live.
Sign in to follow issues

Comments & Activities

  • Issue created by @VasyOK
  • Assigned to abhiyanshu_rawat
  • 🇮🇳India abhiyanshu_rawat

    @VasyOK,
    It seems like you've made some indentation issue.
    Proper indentation is crucial for the code to be parsed correctly.

    Please follow-up with the below code, Hope it works.

    Your code should look like this with correct indentation:

    libraries-override:
    jquery_ui/core:
    css:
    component:
    assets/vendor/jquery.ui/themes/base/theme.css: false
    assets/vendor/jquery.ui/themes/base/accordion.css: false

    NOTE :-

    In the above corrected code,
    The component section is properly indented under css.
    The CSS files you want to disable (theme.css and accordion.css) are correctly indented under component.
    With this indentation, Drupal should be able to recognize and apply the libraries-override configuration as intended, disabling the specified CSS files from the jQuery UI library.

  • 🇺🇦Ukraine VasyOK

    Sory abhiyanshu_rawat, it doesn't work.

  • 🇺🇦Ukraine VasyOK

    Solution referensed from https://api.drupal.org/api/drupal/core%21lib%21Drupal%21Core%21Render%21...

    add function to mytheme.theme

    function mytheme_css_alter(&$css, \Drupal\Core\Asset\AttachedAssetsInterface $assets, \Drupal\Core\Language\LanguageInterface $language) {
      unset($css['modules/contrib/jquery_ui/assets/vendor/jquery.ui/themes/base/theme.css']);  
      unset($css['core/assets/vendor/jquery.ui/themes/base/theme.css']);
      unset($css['modules/contrib/jquery_ui/assets/vendor/jquery.ui/themes/base/accordion.css']);
    }
  • @abhiyanshu_rawat opened merge request.
  • 🇮🇳India abhiyanshu_rawat

    Hi @VasyOk,
    However, based on your reference,
    Here's a more flexible and maintenance-friendly approach using the array_filter function to conditionally unset CSS files.

    /**
     * Implements hook_css_alter().
     */
    function mytheme_css_alter(&$css, \Drupal\Core\Asset\AttachedAssetsInterface $assets) {
      // List the CSS files you want to remove.
      $css_to_remove = [
        'modules/contrib/jquery_ui/assets/vendor/jquery.ui/themes/base/theme.css',
        'core/assets/vendor/jquery.ui/themes/base/theme.css',
        'modules/contrib/jquery_ui/assets/vendor/jquery.ui/themes/base/accordion.css',
      ];
    
      // Unset the specified CSS files.
      $css = array_filter($css, function ($key) use ($css_to_remove) {
        return !in_array($key, $css_to_remove);
      }, ARRAY_FILTER_USE_KEY);
    }
    

    It uses an array of file paths to remove and then filters the $css array to keep only the files that are not in the list of files to remove.

    Make sure to clear the Drupal cache after adding or updating this code in your theme's mytheme.theme file.
    The above code allows you to easily add or remove CSS files from the list as needed without altering the structure of the code.

  • 🇺🇦Ukraine VasyOK

    Yes @abhiyanshu_rawat, you right!
    Thanx!

  • 🇮🇳India abhiyanshu_rawat

    @VasyOK,
    I think we can close this issue now !!!

  • Status changed to Fixed 9 months ago
  • 🇺🇦Ukraine VasyOK
  • Automatically closed - issue fixed for 2 weeks with no activity.

  • Status changed to Fixed 8 months ago
Production build 0.69.0 2024