1

This question is about Drupal 7. For a particular node type, let's take three examples:

  • a) book
  • b) article
  • c) customcontenttype

I would like to be able to capture the moment of an update of an existing node of this type.

I would like to know if there is a way to target only that node type directly from the hook and not to have the hook triggered for all nodes and then checking the node type.

I've seen some internet articles talking about

  1. mymodule_node_update - for me this is a general hook for all nodes

  2. mymodule_book_update or mymodule_article_update or mymodule_customcontentype_update

  3. mymodule_form_book_node_form_alter or mymodule_form_article_node_form_alter or mymodule_form_customcontenttype_node_form_alter

Which one of the three above hooks should I use? all of them? if none of them are good then which hook to use?

Many thanks

1

I would like to know if there is a way to target only that node type directly from the hook and not to have the hook triggered for all nodes and then checking the node type.

Node hooks aren't that granular, they're invoked for all nodes and there isn't a system in place to make that selective based on the type. If you're worried about performance, don't be; the difference between the hook being invoked for all nodes, and a system to allow it to be selective would be negligible.

If you want to react to existing nodes being update, hook_node_update() is definitely the right choice.

You just need to test the type in that function:

function MYMODULE_node_update($node) {
  if (in_array($node->type, ['book', 'article', ...])) {
    // Do something.
  }
}
|improve this answer|||||
  • Thank you Clive, Now I understand better. I got lost with regards to the hook_node_type_update, as I thought that node_type can be replaced with the actual node_type, but I guess that actually this is a fixed hook "node_type_update" that is triggered when the node_type is changed meaning via this link example.com/admin/structure/types/manage/book Could you please confirm that, to complete my understanding. – MMT Apr 25 '19 at 5:28
  • hook_node_type_update is for responding to a node type being updated, not an individual node. It’s not relevant to what you’re trying to achieve – Clive Apr 25 '19 at 7:00

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.