Support for Drupal 7 is ending on 5 January 2025—it’s time to migrate to Drupal 10! Learn about the many benefits of Drupal 10 and find migration tools in our resource center.
Hello,
What would be the best way of adding a small CSS file to the core admin theme? We are simply looking to improve the UX of multi select list boxes we are using for taxonomy selection by making them taller.
Comment | File | Size | Author |
---|
Comments
Comment #2
cilefen CreditAttribution: cilefen commentedAs far as I know a library override would do. But are you looking for a permanent change to core?
Comment #3
Soundvessel CreditAttribution: Soundvessel commentedI posted a separate feature request regarding the UX concerns I found with taxonomy field checkbox and mutliple select list widgets.
So this means I need to go through all the work to sub-theme Seven to add my CSS file with
libraries-extend
so I am not writing over core?Comment #4
cilefen CreditAttribution: cilefen commented"As far as I know" means what it says ;-). What other solution did you have in mind?
Comment #5
star-szrSeven is internal (subject to change) so not recommended to subtheme it. What I would recommend for cases like this is creating a module that loads the CSS you want in the right scenario. This would likely be done via a hook like a form alter or in this case something more specific like a widget alter. Once you get the right hook you can add other assets (libraries) via #attached in a render array.
Comment #6
ABaier CreditAttribution: ABaier commentedJust to understand some things better, because I am trying to achieve this as well.
Are definitions of
library-extend
inmytheme.info.yml
andmytheme.libraries.yml
only assigned to the frontend or my custom theme or is it possible to extend core and core theme libraries (i.e. of Seven) from there as well?Does the following quote from the link in #2 mean that the overrides need to happen inside this base- and subtheme relation?
I tried the following, but unfortunately the script is not loaded on the node edit form. Overriding the core files of Seven worked.
mytheme.info.yml
mytheme.libraries.yml
Comment #7
ABaier CreditAttribution: ABaier commentedI followed the advice in #5 and did the following in a custom module:
Comment #8
star-szrFirst, the code in #7 is along the lines of what I was thinking :)
@toni4i to answer #6, libraries-override in a mytheme.info.yml only works within the active theme or more accurately the stack of active themes (base- and subtheme relation as you described it). So it would only work if you subthemed Seven which as I mentioned is not recommended. However if you're just subtheming to do a libraries-override/extend it's less risky IMO. In the end just doing that should be roughly equivalent to using hook_library_info_alter() in a custom module which would be the more "recommended" way of overriding/extending libraries from a core internal theme (Bartik or Seven). Either way you may need to update your overrides/extends if you update core.
Comment #9
ABaier CreditAttribution: ABaier commentedGood to know! Thanks for your quick reply.
Comment #10
Beezer75 CreditAttribution: Beezer75 commentedHere's a quick fix: Create a new block, in the body field enter in source view your styles along with opening/closing style tags, then assign the block to a region in Seven theme only (the Help region worked perfect for me).
Comment #11
hockey2112 CreditAttribution: hockey2112 commented@Beezer75, great idea! Works like a charm.
Comment #12
pierrick419 CreditAttribution: pierrick419 commentedI agree that for a quick fix / minimum amount of styles overrides, #10 solution works great!
Comment #13
Elvin - Albania Drupal Developer CreditAttribution: Elvin - Albania Drupal Developer commented@Beezer75, thank you! so quick and elegant :)
Comment #14
sirtet#10 works great for me on a D7 Site,
But on a 8.8.1 this
gets output as
Seems like some new escaping has been introduced?
[edit] Solved:
The filter Correct faulty and chopped off HTML is set by default for the full HTML text Format. It was disabled on my D7 install, so nothing new here.
Comment #15
Elvin - Albania Drupal Developer CreditAttribution: Elvin - Albania Drupal Developer commented@sirtet #14
Please check your text format. I am using this on drupal site 8.8.1 and i have the following without a problem:
I am using Full HTML format. Maybe create a new format and remove any of the filters on it.
Comment #16
sirtetWill check.
This was on a vanilla 8.8.1 on simplytest.me also using full Html Format.
Comment #17
sirtetSo i guess you have modified your Full HTML format?
Because what i see is:
Started 8.8.1 on simplytest.me, added a block with fullHTML with your CSS to the content area, and the css gets escaped.
After turning off the input filter "Correct faulty and chopped off HTML" the CSS is output correctly.
Looks to me like a bug of that filter, as i can see no fault in the CSS...
Comment #18
Elvin - Albania Drupal Developer CreditAttribution: Elvin - Albania Drupal Developer commentedi think i have modified it to allow css outputing. i am glad its working for you now as well
Comment #19
vbalsys CreditAttribution: vbalsys commentedIf you want to add styles, which would apply for the main theme and admin theme (e.g. to target admin toolbar), this way worked for me:
1. Define library in your custom module (not theme) e.g. mymodule.libraries.yml:
2. In your module file (mymodule.module) use hook_preprocess_page() to load the library:
3. Clear caches. Now mystyles.css will load in both themes: main and admin.
Comment #22
halthI can confirm #19 works perfectly! Thank you @vbalsys!
Comment #23
djsagar CreditAttribution: djsagar at OpenSense Labs commentedIf you want best practice for adding CSS to admin core theme try this, This is the best way to add css.
Thanks!
Comment #24
djsagar CreditAttribution: djsagar at OpenSense Labs commentedComment #25
rafaelferreir4 CreditAttribution: rafaelferreir4 as a volunteer and commented#23 doesn't really solve the issue. #19 does. Thanks!
Comment #26
Nishat Ahmad CreditAttribution: Nishat Ahmad as a volunteer and at Douce Infotech Pvt. Ltd. commentedUse this one it's helpful.
Use in custom form.
$form['#attached']['library'][] = 'mymodule/mylib';
Comment #27
vikashsoni CreditAttribution: vikashsoni as a volunteer and at Zyxware Technologies commentedThis code will be helpful
Define the function in .module file ------
function mymodule_page_attachments(&$variables) {
$variables['#attached']['library'][] = 'mymodule/custom_css';
}
Define the libraries in mymodule.libraries.yml
custom_css:
css:
theme:
css/custom.css: {}
Clear cache now u can check your css file with view page source
Comment #28
Chi CreditAttribution: Chi commentedAnother possible way to add CSS to admin theme files from module.
Comment #29
crutch CreditAttribution: crutch commented#10 works great and simple, D9.
1. Added Block Type "Code Block"
2. Created block "Admin CSS"
3. Added css including open and close
<style> ..css.. </style>
4. Added block to Help section in Seven theme (using as admin theme)
Make sure to choose a text format where "Limit allowed HTML tags and correct faulty HTML" is not checked in CKEditor.
Comment #30
karldivad CreditAttribution: karldivad as a volunteer commentedI just made a copy of entire
seven
directory fromweb/core/themes/seven
toweb/themes/custom/seven
and it worked good.Also for code sniffer: Add line
core_version_requirement: ^8 || ^9
and removeversion: VERSION
fromweb/themes/custom/seven/seven.info.yml
Comment #34
gaurav-mathur CreditAttribution: gaurav-mathur at Dotsquares Ltd. commentedComment #35
gaurav-mathur CreditAttribution: gaurav-mathur at Dotsquares Ltd. commentedIf you want Best practice for adding CSS to admin core theme,use this method,this way worked for me:
Create/update "yourtheme_name.libraries.yml" file in your theme.
add css files in it.
Comment #36
Vadim Ansari CreditAttribution: Vadim Ansari at Dotsquares Ltd. commentedThe best way I found is using Admin CSS Module
The module has the configuration page to add your custom css
https://www.drupal.org/project/admincss.
It will add admin-style.css
Comment #37
Vadim Ansari CreditAttribution: Vadim Ansari at Dotsquares Ltd. commentedAdmin CSS Editor setting path
/admin/config/development/admincss
Comment #38
himanshu_jhaloya CreditAttribution: himanshu_jhaloya at Valuebound for Valuebound commentedCreate/update the "mytheme.libraries.yml" file in your theme.
add CSS files in it.
Best practice for adding CSS to admin core theme
global-styling:
version: 1.x
css:
theme:
css/layout.css: {}
css/style.css: {}
css/colors.css: {}
css/print.css: { media: print }
Comment #40
smustgrave CreditAttribution: smustgrave at Mobomo commentedThanks everyone for respond. Since there was nothing to review and original poster hasn't asked for additional help in 7 years think this is good to close.
Again thanks!
Comment #41
johnnny83 CreditAttribution: johnnny83 commentedYou can also use the Asset Injector module to add css to your admin theme: https://www.drupal.org/project/asset_injector
Or with this short custom module: https://gorannikolovski.com/snippet/add-css-and-js-admin-pages