1

I want to include my custom style.css in admin pages in Custom Admin theme I have been create.
I am including CSS in .info file as follows

name = Example Module
description = Custom Example Module
core = 7.x
configure = admin/config/example-module/settings
permissions = admin/people/permissions#module-example-module
stylesheets[all][] = css/style.css

and I used mymodule_preprocess_page(&vars) , but I didn't see any result.

3
  • 1
    why you try to load css from .info in custom module ??!!!!
    – Yuseferi
    Aug 28, 2015 at 7:49
  • @zhilevan Per drupal.org/node/542202, stylesheets is a valid directive in module.info files. However, the practice isn't recommended. CSS, JS, and libraries should use #attached in render arrays instead.
    – mpdonadio
    Aug 28, 2015 at 10:59
  • @MPD I know that but better way to add global css( that want be exist anywhere) is adding it in themename.info file
    – Yuseferi
    Aug 28, 2015 at 11:44

4 Answers 4

2

You can add yourstyl.css in admin theme's .info file. Just goto your active admin theme, find and open its .info file and place a simple line here; stylesheets[ ] = css/yourstyle.css . It works like a charm.

6
  • 1
    But this is not a good idea if it's a core theme.
    – leymannx
    Aug 28, 2015 at 7:33
  • By using this method you are adding additional functionality to core theme. It's doesn't mean you are hacking core theme.
    – WaQaR Ali
    Aug 28, 2015 at 9:43
  • 1
    Well, it may get overwritten with the next update.
    – leymannx
    Aug 28, 2015 at 9:45
  • Just backup that file and add again after update.
    – WaQaR Ali
    Aug 28, 2015 at 11:05
  • 1
    It's getting complicated ;)
    – leymannx
    Aug 28, 2015 at 11:06
1

When I look at your .info file I see it is Custom Module instead Custom Theme

name = Example Module
description = Custom Example Module
core = 7.x
configure = admin/config/example-module/settings
permissions = admin/people/permissions#module-example-module
stylesheets[all][] = css/style.css

Instead adding Style to module info file add it to theme info file in your themename.info add stylesheets[all][] = css/style.css

0

I misunderstood your question. It's not about a core admin theme. It's about a custom admin theme you created. Then of course you add that CSS from your custom admin theme's .info file.

Otherwise, if it's a core admin theme you'ld write a simple custom module or add the following to an existing custom module. All you need to implement is hook_page_build() and then add your own CSS file with drupal_add_css(). The important thing there is to add 'group' => CSS_THEME as an option to prevent the styles from getting overwritten.

/**
 * Implements hook_page_build().
 */
function MYMODULE_page_build(&$page) {
  // check current path
  if (path_is_admin(current_path())) {
    // add CSS
    drupal_add_css(drupal_get_path('module', 'MYMODULE') . '/css/custom_admin.css', array('group' => CSS_THEME));
  }
}
3
  • 1
    Your solution is good when he hasn't custom admin theme, when he created admin and has global CSS file , Do you thing is better to load it from themename.info file instead with another way( like preprocess page or page build etc ) ?
    – Yuseferi
    Aug 28, 2015 at 7:48
  • Ah I see, @zhilevan. You are right. In this case you'ld do it from your .info file. Otherwise I think it's better to use a hook and drupal_add_css() since you have a lot of options available you don't have when you load it from a module's .info file
    – leymannx
    Aug 28, 2015 at 7:56
  • @leymanx I am agree with you ;)
    – Yuseferi
    Aug 28, 2015 at 8:18
-2

Try drupal_add_css() function in your template file.

5
  • There is no template file. I am simply creating the form in admin pages and need to apply css for the same. But unfortunately I am not able to do that.
    – Ajay Reddy
    Jul 24, 2015 at 5:06
  • How do you create this form?
    – Andrew
    Jul 24, 2015 at 5:13
  • 1
    Using hook_form in .module. And mymodule is useful for different project, so cant edit theme template file manually. I need to apply css to form programatically when i access my admin page.
    – Ajay Reddy
    Jul 24, 2015 at 5:18
  • 2
    This is not solution, he had mistake in his code, instead add style in theme.info file added in module.info file
    – Yuseferi
    Aug 28, 2015 at 6:42

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.