2

views_preprocess_node() contains the following code.

  if (!empty($vars['node']->view) && !empty($vars['node']->view->name)) {
    $vars['view'] = $vars['node']->view;
    $vars['theme_hook_suggestions'][] = 'node__view__' . $vars['node']->view->name;
    if (!empty($vars['node']->view->current_display)) {
      $vars['theme_hook_suggestions'][] = 'node__view__' . $vars['node']->view->name . '__' . $vars['node']->view->current_display;

      // If a node is being rendered in a view, and the view does not have a path,
      // prevent drupal from accidentally setting the $page variable:
      if ($vars['page'] && $vars['view_mode'] == 'full' && !$vars['view']->display_handler->has_path()) {
        $vars['page'] = FALSE;
      }
    }
  }

The part to which I am interested is the line that initializes a variable using the value of $vars['node']->view. I looked at the code executed from the Views module for a node, but I was able to find where $node->view is initialized.

Which of the node hooks (e.g. hook_node_view(), hook_node_load(), hook_node_presave()) would get a valid value for $node->view?

1 Answer 1

1

A quick grep of the views module points to the views_plugin_row_node_view class, whose render() function contains the following lines:

$node->view = $this->view;
$build = node_view($node, $this->options['view_mode']);

return drupal_render($build);

So when a node is rendered using the node row plugin, the view is attached to the node object immediately prior to it being passed through node_view(), which is why you see it in the theme preprocess functions.

To actually answer the question (which I missed before), hook_node_view() would get a valid view object, while the others you mentioned wouldn't (I don't think).

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.