1

I have a custom module in Drupal 7 that creates nodes. I want a custom css stylesheet (customstyle.css) applied when one of the nodes created by my custom module is rendered, but only when one of the nodes created by my custom module is rendered.

The simple solution (adding stylesheets[all][] = customstyle.css to my custom module's .info-file) applies the style sheet to all pages, including pages containing nodes not created by my custom module. I do not want that.

2
  • 1
    You will need to create a preprocess function in your theme folder to add the CSS required depending on the node type. You will have to implement template_preprocess_html. There is a good answer by Clive covering a similar functionality here: stackoverflow.com/a/7354562/1027644 Commented Dec 10, 2012 at 23:26
  • Also, this is assuming that your node creates a custom node type, that will allow you to differentiate between nodes created by that module and nodes not created by that module Commented Dec 11, 2012 at 1:19

2 Answers 2

5

I assume that "nodes created by my custom module" means that they have some special node type. If so, you can implement hook_node_view() in your module and then call drupal_add_css() based on the node type or any other property:

function mymodule_node_view($node, $view_mode) {
  if ($view_mode != 'rss') {
    if ($node->type == 'mynodetype') {
      drupal_add_css(drupal_get_path('module', 'mymodule') . '/mynodetype.css');
    }
  }
}
Sign up to request clarification or add additional context in comments.

Comments

0

I would do it the site builder way. Use Context to target that node type, then assign the reaction to load your CSS file, just use .context-[node type] to target specific elements in those pages.

For example if your node type is apple recipes, you can use .context-apple-recipes

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.