I am trying to convert some of my rules to a custom module for more flexibility and better configuration.

However, I am stuck; I want my module to read some fields on node creation (or update). In rules there is the event property where I can choose 'on creation of node'.

I suppose I have to use a hook of some sort, but hooks like hook_node_insert() (and some others) that respond on a node being created (or saved) are depending on a node parameter; hook_node_insert($node).

So how do I get this node property?

  • I think you're slightly misunderstanding hooks, have a look at drupal.stackexchange.com/questions/26290/… it should clear things up. – Clive Sep 11 '12 at 20:15
  • In the example they say hook_node_delete can be used to do thing if a certain node gets deleted. Basically I want to do the same on node creation. So I do assume hooks is the way to go. But all hooks that deal with node creation, insertion, etc. are depending on a $node parameter. Is this an object? a nid? or something I can define myself (eg. specifying types, revisions, dates, etc.) – Neograph734 Sep 11 '12 at 20:24
  • 4
    You implement a hook and some other part of the system will call it. The $node object is passed to your implementation by the invoking function, it's not something you set up yourself – Clive Sep 11 '12 at 20:32
  • Oke, that is clear now. But then how do I make my module respond on such calls? Like hook on to it? – Neograph734 Sep 11 '12 at 20:43
  • You need to write a custom module, simply write function MYMODULE_node_insert($node) { // function code }. Once Drupal's caches have been cleared the code you've written inside that function will be called whenever a node is inserted – Clive Sep 11 '12 at 21:15

All credits to Clive, but here is the answer.

You need to write a custom module: Simply write a MYMODULE_node_insert($node) function containing the code you want to execute. Drupal will call that function every time a node is created (when the module is enabled).

If you are changing the code of a module that is already enabled, you should clear Drupal caches, as otherwise Drupal will not notice the module contains a new hook implementation. This can be achieved by disabling the module before editing it, and re-enabling it after the code has been changed.

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.