1

I am trying to add a body class to my site in template.php based on whether or not a node is published or not. I had a look around here and on drupal.org and pieced together some code but I am getting an error. Here is what I have tried so far:

function responsive_process_html(&$variables) { 

 if ($node->status == 1){
        //$variables['#attributes']['class'][] = 'pub';
        $variables['classes_array'][] = 'published';
    }

    else {
        $variables['classes_array'][] = 'not-published';
    }
}

... but I am getting this error:

Notice: Undefined variable: node in responsive_process_html() (line 37 of /sites/all/themes/responsive/template.php).
Notice: Trying to get property of non-object in responsive_process_html() (line 37 of /sites/all/themes/responsive/template.php).

I also tried $variables['#attributes']['class'][] = 'published'; instead of $variables['classes_array'][] = 'published'; but it made no difference.

4

in your theme's template.php:

function mytemplate_preprocess_html(&$vars) {
  $node = menu_get_object();
  if ($node && isset($node->nid)) {
    if ($node->status) {
      $vars['classes_array'][] = 'published';
    } else {
      $vars['classes_array'][] = 'unpublished';
    }
  }
}
2

I ended up finding a sweet little function from the Zen Theme (7.x-5.1). In template.php add this preprocess node function:

function MY_THEME_preprocess_node(&$variables, $hook) {
        // Add $unpublished variable.
        $variables\['unpublished'\] = (!$variables\['status'\]) ? TRUE : FALSE;
    }

Then in node.tpl.php or your node--custom.tpl.php grab the unpublished variable and add something like this:

 <?php if ($unpublished): ?>
    <!-- whatever code you want here using the variable-->
    <p class="unpublished"><?php print t('Unpublished'); ?></p>
 <?php endif; ?>

You can then theme this any way you want and it's a great visual indicator to the user that the node is unpublished. (see screen capture).

enter image description here

1

That $node variable isn't defined, but may be passed along inside...

$node=$variables['node'];

if (!empty($node)) { // we are displaying a node

  if ($node->status) {
    // we are published
  } else {
    // we aren't published
  }

} else { // we are in a view or another none node type page
  // do whatever here then
}

... if you are displaying a full node page, so I'd check to see if it's in there. It would be in preprocess_page() so may be available to preprocess_html() as well.

ADDITION:

For example, see if this works....

function YOURTHEME_OR_MODULE_preprocess_html(&$variables) {

  $node=$variables['node'];

  if (!empty($node)) {

    if ($node->status) {
      $variables['classes'].=' published';
    } else {
      $variables['classes'].=' not-published';
    }

  }

}

...but I am just looking at the code as I don't have a D7 testbed to actually run this on.

  • I tried this and with preprocess_page and though I no longer get any errors, the code does not seem to do anything. I don't see a body class added to a node either way. – Danny Englander Aug 15 '12 at 15:43
  • what I was saying is $node is certainly not declared, but rather may be passed within the $variables array argument. In D7, preprocess_html() is definitely where you want to be trying this. I just do not know precisely what variable you need to modify. A quick look at html.tpl.php in the system module's directory suggests $variables['classes'] is a string of classes that you might be able to append to IF the node object is also passed along to this. – Jimajamma Aug 15 '12 at 15:55
  • I tried the new code and variations of it but still no joy. I did find a nice alternate solution that works below based on code from the newest version of the Zen Theme. – Danny Englander Aug 15 '12 at 20:18

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.