1

I am currently creating a custom module. I am trying to alter a node's body before it is displayed to the user. So I used hook_nodeapi();

I did something like

function hook_nodeapi(&$node, $op, $a3 = NULL, $a4 = NULL){
  if($node->type=='page' && $op =='load'){
    dsm($op);
    //the rest of my code goes here
  }
}

When ran, this displays 8 "load"s when viewing a page.

Why is that? I think it is because of the other modules I have that may be calling node_load, but I am not really sure.

Can anyone shed a light here? And how do I make it possible so that my code is not ran multiple times (in this case, 8 times)?

Thanks.

UPDATE: I forgot to mention a couple of key things.

I am using the revisioning module. The page I am viewing is an unpublished revision. I tried this:

function hook_nodeapi(&$node, $op, $a3 = NULL, $a4 = NULL){
  if($node->type=='page' && $op =='load'){
    dsm($node->nid);
   dsm($node->vid);
   //the rest of my code goes here
  }
}

I get 8 similar nids, and then 7 similar vids (the unpublished revision), and then 1 vid (the currently published revision)

916 4544

916 3740

916 4544

916 4544

.. and so on

0

hook_node_api will be invoked for each every node available in the page or in one execution.

If you want to alter, particular node's content then add a condition with nid or title

0

You need to use view $op

function hook_nodeapi(&$node, $op, $teaser, $page){
  if($node->type=='page' && $op =='view'){
    if ($teaser) {
      drupal_set_message("I am viewing page content type teaser");
      //the rest of my code goes here
    }
    if ($page) {
      drupal_set_message("I am viewing page content type full node view");
      //the rest of my code goes here
    }

  }
}

view: The node content is being assembled before rendering. The module may add elements $node->content prior to rendering. This hook will be called after hook_view(). The format of $node->content is the same as used by Forms API.

And you need to check for teaser view or full node view using $a3 or $a4 respectively.

Check how many nodes you are rendering in a page. For example if you are in a view where view is displaying 10 nodes then 10 times message will appear.

Hope it answers your issue !

  • yeah you need to consider this one also. – Mathankumar Sep 27 '12 at 12:42

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.