5

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;
    }

}
3
  • are you using the same themes in front-end and backend ?
    – rémy
    Commented Feb 22, 2017 at 11:03
  • No I'm not, I'm using the standard theme for back-end. But I'd like to include an admin.css in my theme, just for a few minor changes to the admin section.
    – Pim
    Commented Feb 22, 2017 at 11:05
  • I think your hook is wrong. I think it should be hook_page_attachments (you missed '_page'). But you can also use hook_preprocess_page(&$variables). Commented Jun 25 at 8:26

3 Answers 3

3

Unless you use the same theme for front and backend, that's not the right approach.

If something transcends the theme layer, e.g. you have functionality that you need to apply to two separate themes, it belongs in a module.

See Adding stylesheets (CSS) and JavaScript (JS) to a Drupal 8 module for instructions to actually accomplish that, specifically the section on Attaching a library to all (or a subset of) pages which contains a contrived code example:

function contextual_page_attachments(array &$attachments) {
  if (!\Drupal::currentUser()->hasPermission('access contextual links')) {
    return;
  }

  $attachments['#attached']['library'][] = 'contextual/drupal.contextual-links';
}
8
  • I'm using the standard theme for backend, but I'd like to include an admin.css in my theme, just for a few minor changes to the admin section. Literally 6 lines of CSS. Is it at all possible?
    – Pim
    Commented Feb 22, 2017 at 11:07
  • 1
    Of course :) See the code example. It will need to be in a module though, for obvious reasons a theme's functionality is only loaded/invoked when it's actively being used. Executing functionality from one theme when a different theme is being used to render the page could be disastrous
    – Clive
    Commented Feb 22, 2017 at 11:08
  • I've updated the question, could you take a look at it? Thanks!
    – Pim
    Commented Feb 22, 2017 at 11:54
  • 1
    You'll need to change the logic in the attachments hook function to match your requirements, that's just a contrived example that uses a different condition. Searching "programatically determine admin page in drupal 8" should give you the answer
    – Clive
    Commented Feb 22, 2017 at 12:12
  • I've done just that (based on stackoverflow.com/questions/4483200/…), but still no luck.
    – Pim
    Commented Feb 22, 2017 at 12:19
3

The hook is called "hook_page_attachments" so you have to rename your function from

function admin_css_attachments(array &$attachments) {

to

function admin_css_page_attachments(array &$attachments) {
0

You can also use the Asset Injector module to add css to your admin theme: https://www.drupal.org/project/asset_injector

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.