Drupal 7: Ensure HOOK_node_view is not called by search indexing

When using HOOK_node_view() you should take care because when the search (Search API especially) is indexing, this hook is also being called. You can not rely on a user displaying the node display currently. Here's a way to ensure this...

  1. function MYMODULE_node_view($node, $view_mode, $langcode) {
  2.   if ($view_mode != 'full' || arg(0) != 'node') {
  3.     // Skip if this is not in full view mode or the node is not displayed as is (but by search indexing for example):
  4.     return;
  5.   }

Otherwise this may lead to malfunction for example if you're using drupal_goto in this hook (which should not be done by general, but perhaps there's no other way sometimes...)

Comments

Post new comment

The content of this field is kept private and will not be shown publicly.

More information about formatting options