8

Whenever I view a node page, I want to call a function but I'm not sure how to do that.

I was thinking I should use hook_menu() and declare the path as node/%, and attach a callback function to that path. Is that the right way, or is there a correct/better way?

3
  • is this very simple to answer?
    – harshal
    Aug 23, 2013 at 10:15
  • 1
    Yes the answer's very simple, but I'm not sure why that makes this a bad question. The downvote seems unnecessary IMO. I edited to clarify and make it obvious that you've though about this yourself, hopefully that'll be enough to appease the nay-sayers :)
    – Clive
    Aug 23, 2013 at 10:18
  • I must admit Clive's edit significantly improved readability of this question :) +1 from me. Questions simple to answer when you know the answer are not always bad, it's sometimes hard to search them on the net or drupal.og docs.
    – Mołot
    Aug 23, 2013 at 11:06

2 Answers 2

6

You can implement hook_node_view()

Act on a node that is being assembled before rendering.

e.g.

function MYMODULE_node_view($node, $view_mode, $langcode) {
  if ($node->type == 'the_type' && $view_mode == 'full') {
    // Call your function here.
    my_function($node);
  }
}
2

Rules module can also react to URL access. You can specify an action supposed to happen on event if condition is true.

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.