1

I am having a very strange problem with drupal. I have a custom module that does a goto if you view a node of a certain type (see code below). The problem is, after a while I get in an infinite loop where the datafile_flow_node_view is called infinitely (happens on every page,also the ones that don't contain the CT).

The most interesting part is (altough I don't use the search anywhere) that the view mode is search_index. After I disable the search module, the problem dissapears. When I reanable the module, the problem is gone for a certain time, then it returns (I don't know when and why).

Can anyone shed some light on why this happens ?

function datafile_flow_node_view($node, $view_mode, $langcode) {
  if ($node->type == 'datafile') {
    drupal_goto('node/' . $node->nid . '/edit');
  }
}

@EDIT apparently this happens during running the cron. Is there a way to avoid the hook_node_views from beeing executed during cron ?

Naftali
  • 144,921
  • 39
  • 244
  • 303
Nealv
  • 6,856
  • 8
  • 58
  • 89

1 Answers1

1

There isn't a way to avoid hook_user_view() is executed during cron tasks, but you can avoid redirecting the users to another URL when hook_node_view() is invoked to build the node content used to populate the search index.

function mymodule_node_view($node, $view_mode, $langcode) {
  if ($view_mode != 'search_result') {
    if ($node->type == 'datafile') {
      drupal_goto('node/' . $node->nid . '/edit');
    }
  }
}

Generally speaking, it is a bad idea to call drupal_goto() inside hook_node_view(); the hook is supposed to change $node->content, not to redirect users.

What happens is that:

  • During cron tasks, the Search module updates its index
  • The Node module updates the search index, and to do this calls node_view(), which invokes any implementation of hook_node_view(), including the one you shown
  • As drupal_goto() is called during cron tasks, Drupal has problems

The assumption that hook_node_view() is invoked when users are viewing a node is not correct. The only moment you are sure users are viewing a node is when the page callback for node/%node is called.

What you can do is implementing code similar to the following one. (I am writing simplified code just to show the correct way to do it.)

function datafile_flow_menu_alter(&$items) {
  if (isset($items['node/%node'])) {
    $items['node/%node']['page callback'] = 'datafile_flow_node_view';
  }
}

function datafile_flow_node_view($node) {
  if ($node->type == 'datafile') {
    drupal_goto('node/' . $node->nid . '/edit');
  }

  return node_page_view($node);
}

The code is simplified because it makes the assumption the page callback is node_page_view() (the default used by Drupal), and no module changed it. It would be possible to write code that don't make any assumption about that, and the arguments used from that page callback. (It is left as exercise to the readers. ;))

References

apaderno
  • 28,547
  • 16
  • 75
  • 90
  • $view_mode != 'search_result' is what I did too, but you gave a very good explenation and options. Thanks for that. – Nealv Dec 19 '12 at 12:26
  • You are welcome. I am always happy to help with Drupal questions. `:)` – apaderno Dec 19 '12 at 12:33
  • @kiamlaluno , I have the similar problem. But with your solution I guess the same hook datafile_flow_node_view will trigger in normal page calls also other than node/%node. Correct me if I'm wrong. – Jyothish Dec 07 '15 at 07:05