1

I'm trying to do something before my invoice saves. In my invoice module I have:

function invoice_node_presave($node) {
  var_dump($node); die;
  if ($node->type == 'invoice') {
    // If true we are creating a new invoice
    if (intval($node->invoice_number) == 0) {
      // Get new invoice number
      if (intval($node->user_defined_invoice_number) > 0) {
        $node->invoice_number = $node->user_defined_invoice_number;
      }
      else {
        $node->invoice_number = _invoice_get_new_invoice_number();
      }
    }
  }
  ......
}

As you see I have a var_dump($node) & a die; . This works perfect. But then when I want to add the following in my template.php :

function xmed_node_presave($node){
  dump($node); die;
  if (($node->type == 'invoice')) { //node has not been saved
      dpr($node); die;
  }

}

He doesn't die. He just runs... Also the rest of functions in my template.php like function xmed_preprocess_page(&$vars) work.

2 Answers 2

4

If you see below code in node.module node_save() function..

module_invoke_all('node_presave', $node);

It's only invoked if you implement it in modules..

  • Create a folder called 'custom' in sites/all/modules
  • Create a folder called 'mynodeactions' in above custom folder
  • Create a file called mynodeaactions.info inside above folder with

below code

name = My Node Actions
description = A custom module to tackle different node actions
core = 7.x
package = Custom
  • Create a file called mynodeactions.module inside 'mynodeactions' folder with

below code

<?php

function mynodeactions_node_presave($node){

}

?>

For more information See the Creating Drupal 7.x modules if you're not sure

3
  • And how can I do this? Maybe a link to tutorial or something?
    – nielsv
    Apr 25, 2014 at 15:54
  • 1
    You just need to put your xmed_node_presave() function in a module instead of template.php
    – Felix Eve
    Apr 25, 2014 at 15:55
  • See updated detailed answer...
    – Anil Sagar
    Apr 25, 2014 at 16:00
4

hook_node_presave() is only invoked for modules, so you can't implement it in a theme.

It'll need to go in a custom module instead.

From a best practice point of view, that sort of logic definitely shouldn't be at the theme layer, it should be in a module.

6
  • And how can I do this? Maybe a link to tutorial or something?
    – nielsv
    Apr 25, 2014 at 15:56
  • 2
    How can you do what? Implement a hook or write a module? It's funny that you ask, since the code in your question is already doing both :) See the module developer's guide if you're not sure
    – Clive
    Apr 25, 2014 at 15:58
  • The problem is I don't now where to place my code :P Another question, It's bad practice to overwrite the standard invoice module in the code of the invoice module I guess?
    – nielsv
    Apr 25, 2014 at 15:59
  • You guess correctly :) If you can possibly help it, always write a new module to alter existing functionality, rather than editing core/contrib modules directly. It makes life so much easier when you come to updating
    – Clive
    Apr 25, 2014 at 16:01
  • 1
    Anil's added a short example to his answer, that should get you going
    – Clive
    Apr 25, 2014 at 16:03

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.