7

I have a feature that just contains a few views. How do I properly add a stylesheet to it?

13
  1. You add the css file to the module.
  2. Use drupal_add_css to include the css file in the .module, this could be done in hook_init and would look something like this:

    module_name_init() {
      if (arg(0) == 'views_path') {
        drupal_add_css(drupal_get_path('module', 'module_name') . '/path_to.css');
      }
    }
    
  • That was my first thought as well, but this has the disadvantage that you have to mannualy add this code every time the fearure is exported. – BetaRide Jan 21 '12 at 9:24
  • 5
    That's not true. Features preserve your custom files and code when you export them. Nowadays, I actually prefer writing relevant custom code inside of feature modules, even update hooks in .install files sometimes. – wizonesolutions Jan 21 '12 at 9:31
  • I was getting a blank screen with the above code. This worked however. Is there anything wrong with this way?: module_name_init() { drupal_add_css(drupal_get_path('module', 'module_name') . '/path_to.css'); } – nigel Jan 22 '12 at 4:13
  • @nigel What I posted was example code, you need to edit it to make it work. If you get a blank screen check your web server's log, it will tell what the problem is. – googletorp Jan 24 '12 at 15:43
  • I used hook_views_pre_render() for my recent project. A sample code snippet is here gist.github.com/1675931 – Sivaji Jan 25 '12 at 11:52
3

If you just want to add some CSS style information, add it to your theme. Create a new CSS file, store it in the theme folder and add it to the .info file of the theme.

If you need to tweak the generated HTML as well, install the theme developer module. When activated, you can point to any part of the Drupal site and it tells you which functions are doing the theming and how to name your own ones if you want to override them.

Don't forget to refresh your cashes to make your changes visible.

  • This is usually a good idea for css files, but doesn't work if you want to use portable nature of modules (the same module can be used on many sites). – googletorp Jan 21 '12 at 9:57
2

I used hook_views_pre_render() in my recent project. A sample code snippet is here https://gist.github.com/1675931

/**
 * Implements hook_views_pre_render().
 */
function example_views_pre_render(&$view) {
  $deals_view = array('deals', 'previous_deals');
  if (in_array($view->name, $deals_view)) {
    drupal_add_js(drupal_get_path('module', 'td_deals') . '/td_deals.js');
    $contextual_module_path = drupal_get_path('module', 'contextual');
    drupal_add_js($contextual_module_path . '/contextual.js');
    drupal_add_css($contextual_module_path . '/contextual.css');
  }
}
0

As mentioned, portability is an issue when adding it to your theme's css folder. I would suggest placing the .css file inside a newly created folder (css) at the root of your exported feature, then reference it inside your feature's .info file like so:

stylesheets[all][] = css/styles.css

Your stylesheet will now be included when the module is enabled. What's nice is when regenerating your feature, this addition will be preserved.

  • This will work only until you update feature, because feature creation overrides custom modifications made in it's .info file. – Bobík Nov 30 '17 at 18:09
  • You are mistaken, @Bobík. I haven't experienced the issue in question when recreating a feature. All modifications to the .info and the .module file are preserved. See feature docs – knice Dec 1 '17 at 20:48
  • The only downside I could see is that the CSS will be included on every page. If you are ok with that, the solution is ok. – donquixote Aug 23 '18 at 16:54

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.