I seem to have read that there is a way of making Rules add a css class to the body tag, but I can't find out how to do it. Any ideas?

  • Why would you need Rules for that ? You can do it in theme level (template.php, individual page template, etc). I'm not aware of such action or a combination of rules to do that either.
    – AKS
    Apr 21 '13 at 21:55
  • To do it conditionally, so that the page's body get a gets a class after an event has occured. Thanks for your comment, I'll edit the title to make it clearer
    – Dalmaz
    Apr 21 '13 at 21:59
  • A Rules event is a one-off temporary firing of an event, if you're adding a body class triggered by that, it will only be added once, after that event is triggered. Keep in mind you can add body classes in hook_preprocess_html in template.php with conditional logic. Apr 22 '13 at 2:51
  • @DavidThomas: That's exactly the kind of behaviour I expect. I want to graphics to change after a product has been added, and the changed graphics should be there until the order is completed or all products have been removed (that'd require another rule, I guess). The suggestion below seems promising
    – Dalmaz
    Apr 22 '13 at 5:34

Yes - the Context module allows you to add CSS classes to the body (Theme HTML condition) based on whatever events you specify. You would then combine this with the Context Rules module which extends Context to allow for context events from Rules.

  • Great, will give that solution a try.
    – Dalmaz
    Apr 22 '13 at 5:35

Assuming you are using the Pathauto module to automatically create semantic paths your content pages based on the menu path you can use the page's path to create the classes you are looking for:

function THEMENAME_preprocess_html(&$vars) {
  $path = drupal_get_path_alias();
  $aliases = explode('/', $path);

  foreach($aliases as $alias) {
    $vars['classes_array'][] = drupal_clean_css_identifier($alias);
  • The OP is expressly talking about the Rule module. Also, it is preferable to tailor the answer to the question being asked; generally answering to a question would probably add a useless answer.
    – apaderno
    Oct 21 '15 at 16:34

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