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(&$vars, $hook) {
// Add the terms to the body classes for my_node_type'.
if ($node = menu_get_object()) {
if ($node->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->name));
$vocabulary = drupal_clean_css_identifier($term->vocabulary_machine_name);
$vars['classes_array'][] = $class;
}
}
}
}
I added a
<?php dpm($classes); ?>
on my node.tpl.php I'm able to see the particular field being added to the classes array in my <div class="my_field_name">
. 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->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(&$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->name));
$vocabulary = drupal_clean_css_identifier($term->vocabulary_machine_name);
$vars['classes_array'][] = $class;
}
}