Skip to content

Add library/hook to default theme to enable admin (Gin) theme style changes #885

@NikLP

Description

@NikLP

In a recent project I found myself wanting to add minor css changes to Gin. Chasing this requirement I found a number of ways of achieving this.

  1. Subtheme gin
  2. Use a custom css file with special location in filesystem
  3. Custom code in a module to add a library

Option 1 is not recommended and given the state of Gin at the moment I'm in strong agreement. There is a lot of work to be done to get this theme into core.

Option 2 is sketchy at best. The idea is that you create a css file called gin-custom.css and this has to be placed in the public files directory. Whilst this doesn't sound too offensive, it's subject to issues of confusion and environment specificity - the easiest way to ship this is with the theme and then create a symlink from public: back to the theme, but not all servers will honour a symlink, and failure to copy the file into place (in the event that symlinks aren't an option) when changes are made mean that simply forgetting to do so renders the changes ineffective.

Option 3 is arguably the best (most Drupally) way.

In order to achieve this we need to add a library entry and css file to the theme, and add a hook to an LGD module. @markconroy informs me that such a candidate exists in the hitherto-unknown-to-me localgov_admin_theme_improvements module.

Physical steps to achieve monumental success!

library is simply:

gin_overrides:
  css:
    theme:
      css/gin-overrides.css: {}

module hook code:

function MODULE_page_attachments(array &$attachments) {
  /**
   * Attach a CSS library to overwrite Gin theme styles on admin routes.
   * Assumes the default theme contains the necessary library.
   */
  if (\Drupal::service('router.admin_context')->isAdminRoute()) {

    // Get the default (frontend) theme (ref. ckeditor5.module:587+).
    $default_theme = \Drupal::config('system.theme')->get('default');

    $attachments['#attached']['library'][] = "$default_theme/gin_overrides";
  }
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions