theme_preprocess_node not working inside of a view I'm trying to add a variable to the $classes array on a node, but it's not adding it while the node is in a view.

function MY_MODULE_preprocess_node(&amp;$vars, $hook) {
  // Add the terms to the body classes for my_node_type'.

  if ($node = menu_get_object()) {
    if ($node-&gt;type == 'my_node_type') {

  // Return an array of taxonomy term ID's.
  $termids = field_get_items('node', $node, 'my_field_name');

  // Load all the terms to get the name and vocab.
  foreach ($termids as $termid) {
    $terms[] = taxonomy_term_load($termid['tid']);
  }

  // Assign the taxonomy values.
  foreach ($terms as $term) {
    $class = strtolower(drupal_clean_css_identifier($term-&gt;name));
    $vocabulary = drupal_clean_css_identifier($term-&gt;vocabulary_machine_name);
    $vars['classes_array'][] = $class;
   }
  }
 }
}


I added a 

&lt;?php dpm($classes); ?&gt;


on my node.tpl.php I'm able to see the particular field being added to the classes array in my &lt;div class="my_field_name"&gt;. But when it's in a view, the class isn't applied which makes me think that the preprocess_node isn't being called properly. Any suggestions for what I should do or anything I'm missing that's preventing the hook_preprocess_node from being called properly? I've tried placing it into my theme's template.php instead of in the custom module I'm using but neither work.

ANSWER NINJA EDIT (can't post one for another 8 hours)

I was able to finally get it to call the classes properly. Appears that these conditions were not being met in the view:

if ($node = menu_get_object()) {
    if ($node-&gt;type == 'my_node_type') {


so I removed that line and instead replaced it with 

$node = $vars['node'];


Removed the code from my custom module and placed it in my custom theme's template.php o the function now looks like this

function MY_THEME_preprocess_node(&amp;$vars, $hook) {
  // Add the terms to the body classes for MY_NODE_TYPE'.
  $node = $vars['node'];

  // Return an array of taxonomy term ID's.
  $termids = field_get_items('node', $node, 'MY_FIELD_NAME');

  // Load all the terms to get the name and vocab.
  foreach ($termids as $termid) {
    $terms[] = taxonomy_term_load($termid['tid']);
  }

  // Assign the taxonomy values.
  foreach ($terms as $term) {
    $class = strtolower(drupal_clean_css_identifier($term-&gt;name));
    $vocabulary = drupal_clean_css_identifier($term-&gt;vocabulary_machine_name);
    $vars['classes_array'][] = $class;
 }
}
$node = menu_get_object() works only when the page you are seeing has a URL that is similar to node/12, or node/12/view. When you are watching a view, its URL is probably different.

That code is also not necessary, as template_preprocess_node() already initializes $variables['node'] with the node object.

$variables['node'] = $variables['elements']['#node'];
$node = $variables['node'];


If you want to check if the node is being viewed in its own page, you can use the following code.

if ($variables['page']) {
  // The node is shown in its own page.
}
else {
  // The node is shown together other nodes.
}


$variable['page'] is a variable set by template_preprocess_node().

As side note, this is the code used by views_plugin_row_node_view::render() to render a node.

function render($row) {
  if (isset($this-&gt;nodes[$row-&gt;{$this-&gt;field_alias}])) {
    $node = $this-&gt;nodes[$row-&gt;{$this-&gt;field_alias}];
    $node-&gt;view = $this-&gt;view;
    $build = node_view($node, $this-&gt;options['view_mode']);

    return drupal_render($build);
  }
}


node_view() is the function that calls the theme_node() theme function, causing preprocess/process functions to be called.