I want to add a body class for the content type of each node. Drupal already provides this, but it isn't always consistent between the Node view, the Node/Add view and the Node/Edit view. This makes theming difficult.

Here is my code in template.php

function MYTHEME_preprocess_html(&$variables) {

    // Add Content type name to classes
    $content_type = $variables['page']['content']['system_main']['type']['#value'];
    $content_type_class = 'content-type-'.drupal_html_class($content_type);
    $variables['classes_array'][] = $content_type_class;


It works but I get this error message:

Notice: Undefined index: type in wintheme_preprocess_html()

On Stack Exchange, it says the best approach is to use Isset().

If I do this, the error message goes away, but so does the class.

How can I keep my class working without the error message.

2 Answers 2


The problem is you're trying to use the same system_main content type variable that's not always defined. Try this instead:

$node = menu_get_object();
$type = isset($node->type) ? $node->type : 'none';
$type = ($type == 'none' && arg(0) == 'node' && arg(1) == 'add') ? arg(2) : $type;
$content_type_class = 'content-type-'.$type;
$variables['classes_array'][] = $content_type_class;

That should give you a $classes_array var with either "content-type-TYPE" or "content-type-none" for pages that aren't associated with nodes.

  • This works for node/edit, but the for node/add, the content type is listed as "none". (By Node/add I mean when you create a node before saving it).
    – big_smile
    Apr 17, 2015 at 17:48
  • I updated the code with an extra conditional assignment that should cover the scenario of node/add/XXXX path.
    – Shawn Conn
    Apr 17, 2015 at 17:59

Here's the solution for your problem.

@Shawn Conn : menu_get_object is return none on index/home page because menu_get_object work as route page requests to code based on URLs. See : function menu_get_object

So finally:

Put below code line into your template.php file in before closing bracket of wintheme_preprocess_html.

//Get current exist nodes content type and append into array
$nodType = node_type_get_types();
if(is_array($nodType)) {
   foreach ($nodType as $types) {
      $vars['classes_array'][] = "content-type-".strtolower(drupal_clean_css_identifier($types->type));

Hope this would solve your problem. Further, if you have used theme like Bootstrap, Omega, etc. then we need to work out differently.

Let me know if you face any query/concern regarding this.


  • Thanks! I've tried your code, but it always returns the content type as page (E.g. for articles, it is displayed as content-type-page). Shawn Conn's code seems to work, so I have gone with that, but thanks for helping, as I do appreciate it.
    – big_smile
    Apr 21, 2015 at 10:05

Your Answer

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

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