0
function mymodule_node_insert($node){
    if($node->form_id == 'whatever_node_form'){
        // do stuff
    }
}

Returns an error: Notice: Undefined property: stdClass::$form_id in mymodule_node_insert() (line 16 of /home/admin/public_html/website.com/sites/all/modules/mystuff/mystull.module).

I can clearly see the array dump of $node that it is indeed an object and it contains form_id. What am I doing wrong?

closed as too localized by kiamlaluno Feb 8 '12 at 8:59

This question is unlikely to help any future visitors; it is only relevant to a small geographic area, a specific moment in time, or an extraordinarily narrow situation that is not generally applicable to the worldwide audience of the internet. For help making this question more broadly applicable, visit the help center. If this question can be reworded to fit the rules in the help center, please edit the question.

  • Your code looks right and this is what I have in my similar functions -- is it finding the form you want it to and just throwing an error condition because of a high PHP error level? – schnippy Feb 2 '12 at 23:03
  • Hah, no, figured it out... I was creating another node inside this function, and it was calling hook_node_insert a second time, recursively, and leaving out form_id. So what looked like the right array was actually NOT the correct output! Gah. – emc Feb 3 '12 at 0:04
  • If it's not a property added by another module, $node->form_id is not a property added by Drupal. – kiamlaluno Feb 3 '12 at 0:57
  • 1
    This is a localized questions that doesn't help future readers. – kiamlaluno Feb 4 '12 at 22:24
1

This code is actually correct. When checking for an object, it's always a good idea to use isset() to make sure the variable exists!

This code was adjusted to the following:

function mymodule_node_insert($node){
  if (isset($node->form_id) && ($node->form_id == 'whatever_node_form')) {
    // do stuff
  }
}

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