3

I need to check inside node template if its contents are being rendered through view or not. What is the best approach to do it?

2 Answers 2

0

I am unaware of a good way to do this. However, the proper Drupal way is to not have one part of a template rely on where it is being used. If something needs to be different, then render it out in a different manner.

For nodes in views, you can accomplish this with custom node view modes. Essentially, you implement hook_entity_info_alter, and create new view modes (ones inaddition to Full and Teaser). You can then use the Manage Display tab on the content type page to set up how you want the new mode to work, and then set up your view to use Node style: Row with your new display mode. Also look at @Andy's answer on How to add additional display(view mode) for node? for some options that make this easier to do just from the UI.

1
  • Thanks, first link was very useful. I've implemented a new custom display type for node and crated template for it.
    – joshas
    Jul 20, 2012 at 10:39
4

You can check to see if $view is set and is an object in the template file; if it does, it means the node is being rendered in a View. From there you can even check that the View is the correct one if you need to.

if (isset($view) && is_object($view)) :
  // Do something
endif;

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.