1

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;
 }
}
1

$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->nodes[$row->{$this->field_alias}])) {
    $node = $this->nodes[$row->{$this->field_alias}];
    $node->view = $this->view;
    $build = node_view($node, $this->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.

| improve this answer | |

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.