How do I include admin.css
in my theme so that it's loaded in the admin section (and preferably only the admin section)?
Right now when I add admin.css via the x.libraries.yml
file, it's only loaded on the front-end.
Update:
So... based on your comments/answers I've created a module admin-css
. But nothing happens. The file isn't loaded. What am I doing wrong?
Inside admin_css.info.yml
:
name: Admin CSS
description: Alters the Admin CSS for better usability.
package: Custom
type: module
core: 8.x
Inside admin_css.libraries.yml
:
admin-css:
version: 1.x
css:
theme:
css/admin.css: {}
Inside admin_css.module
:
<?php
function admin_css_attachments(array &$attachments) {
$is_admin = \Drupal::service('router.admin_context')->isAdminRoute();
if ($is_admin) {
$attachments['#attached']['library'][] = 'admin_css/admin-css';
return;
}
}
hook_page_attachments
(you missed '_page'). But you can also usehook_preprocess_page(&$variables)
.