0

I have a view that lists a bunch of nodes of a certain content type. I want to style this list of nodes in a way that I can't do easily with the standard node.tpl.php that they're being rendered with. I'm trying to override my themename_preprocess_node function to add a template suggestion depending on what view the nodes were called from. I tried this:

<?php
  function themename_preprocess_node(&$variables) 
  {
    // Add theming based on view name. If the view is named 
    // 'featurefront' one of the template suggestions will be
    // 'node-view--featurefront.tpl.php'.
    if (isset($variables['view']))
    {
       $variables['theme_hook_suggestions'][] = 'node_view__'.
            str_replace('_', '--', $variables['view']->name);
    }
  }
?>

but it doesn't work because $variables['view'] isn't ever set anywhere. Basically my question comes down to this:

What variable can theme_preprocess_node use that will tell it the name of the current view or whether it was called from a view at all?

1 Answer 1

2

You can leverage the views_get_current_view() function to get the current view being rendered. I haven't tested the code below, but it should get you close to what you are trying to do. Note that it could have unintended consequences if nodes are being rendered somewhere else on the page.

function themename_preprocess_node(&$variables)  {
  if (($view = views_get_current_view()) && 'featurefront' == $view->name) {
    $variables['theme_hook_suggestions'][] = 'node_view__' . $view->name;
  }
}

Views doesn't set template variables for nodes, which is why the technique above has to be leveraged.

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.