Taking the Function code "Override or insert variables into the node templates."
from inside the Template.php of the theme Genesis
as a good start point, I want to add a custom node type css class.

To be explicit I have a custom content type named "Event"
and want to add a css class like "node-event-day"
for setting the css attribute for the background-color
for this node-type.

  1. 1. What is the right code?
  2. 2. Where do I have to insert the variable
  3. 3. If I want to insert the variable for another theme should I override all the @param $vars or can I just put the insertion only for my custom content node type in the Template.php file?
/**
 * Override or insert variables into the node templates.
 * @param $vars
 *  A sequential array of variables to pass to the theme template.
 * @param $hook
 *   The name of the theme function being called.
 */

function genesis_preprocess_node(&$vars, $hook) {
  global $user;
  
  // Set the node id.
  $vars['node_id'] = 'node-'. $vars['node']->nid;

  // Special classes for nodes, emulate Drupal 7.
  $classes = array();
  $classes[] = 'node';
  if ($vars['promote']) {
    $classes[] = 'node-promoted';
  }
  if ($vars['sticky']) {
    $classes[] = 'node-sticky';
  }
  if (!$vars['status']) {
    $classes[] = 'node-unpublished';
  }
  if ($vars['teaser']) {
    // Node is displayed as teaser.
    $classes[] = 'node-teaser';
  }
  if (isset($vars['preview'])) {
    $classes[] = 'node-preview';
  }
  // Class for node type: "node-type-page", "node-type-story", "node-type-my-custom-type"
  $classes[] = 'node-'. $vars['node']->type;
  $vars['classes'] = implode(' ', $classes); // Concatenate with spaces.
  
  // Modify classes for $terms to help out themers.
  $vars['terms'] = theme('links', $vars['taxonomy'], array('class' => 'links tags'));
  $vars['links'] = theme('links', $vars['node']->links, array('class' => 'links'));
  
  // Set messages if node is unpublished.
  if (!$vars['node']->status) {
    if ($vars['page']) {
      drupal_set_message(t('%title is currently unpublished', array('%title' => $vars['node']->title)), 'warning'); 
    }
    else {
      $vars['unpublished'] = '<span class="unpublished">'. t('Unpublished') .'</span>';
    }
  }
}

my example tryout:

function mytheme_preprocess_node(&$vars, $hook) {
  global $user;
  
  // Set the node id.
  $vars['node_id'] = 'node-'. $vars['node']->nid;

  // Special classes for nodes
  $classes = array();
  $classes[] = 'node';

if (isset($vars['event'])) {
    $classes[] = 'node-event-day';
  }
 
 // Class for node type: "node-type-event"
  $classes[] = 'node-'. $vars['node']->type;
  $vars['classes'] = implode(' ', $classes); // Concatenate with spaces.
  
}

I am sure I missed somenthing here. so any help very much appreciate.

Comments

psilv’s picture

I think you need to try...

function mytheme_preprocess_node(&$vars, $hook) {
  global $user;
  
  // Set the node id.
  $vars['node_id'] = 'node-'. $vars['node']->nid;

  // Special classes for nodes
  $classes = array();
  $classes[] = 'node';

  // add class for event node type
  if ($vars['node']->type == 'event') {
   $classes[] = 'node-event-day';
  }

  $vars['classes'] = implode(' ', $classes); // Concatenate with spaces.
}

This should add 'node-event-day' class into the body tag on event nodes only.

marcopanichi’s picture

I've tried this code in order to add a class if a specific field is not empty. Here are my code:

function MYTHEME_preprocess_node(&$vars, $hook) 
{
	// class with-cover
	if( $vars['type']=='production' )
	{
		$field = field_get_items('node', $vars['node'], 'field_cover');
		if($field) 
		{ 
			// echo "test"; <--- I've tried it: it works!
			$vars['classes'] = 'with-cover'; 
		}
	}
}

But the class 'with-cover' is not added to the node with the field cover not empty.

Any suggestion?

marcopanichi’s picture

I've used $vars['classes_array'] (I'm on drupal 7) and everything worked!

Here are the code:

function MYTHEME_preprocess_node(&$vars, $hook) 
{
	if( $vars['type']=='production' )
	{
		$field = field_get_items('node', $vars['node'], 'field_cover');
		if($field) { $vars['classes_array'][] = 'with-cover'; }
	}
}
Jeff Burnz’s picture

1. What is the right code?

You have the right code, in your "example tryout", except you don't need the global $user, since you are not using it.

2. Where do I have to insert the variable

Both varaibles need to be printed in your node.tpl.php file, see Genesis node template, e.g.:

<div id="<?php print $node_id; ?>" class="<?php print $classes; ?>">
 ....
</div>

3. If I want to insert the variable for another theme should I override all the @param $vars or can I just put the insertion only for my custom content node type in the Template.php file?

Depends on what you mean by "another theme", if its a subtheme then you only need the customized bits.

@psilv - not really much point in hard coding the type class when you can get it from the node object, as per the original code:

$classes[] = 'node-'. $vars['node']->type;