1

When I call node_save to save a node, no record for "a new node has been saved.." is added to watchdog.

Is this normal? If yes, how do I also add a watchdog record? (without calling the watchdog function directly)

1
  • you generally get a message..
    – harshal
    Oct 29, 2013 at 6:15

1 Answer 1

3

The reason that the save operation is not logged during node_save is because it is done during the node form submission process by node_form_submit which internally uses node_save.

Please refer this elaborate and well-written response of saving the node using drupal_form_submit answered by user Francisco Luz. Pasting the code for the sake of completion here for saving an article resulting in both the watchdog logging and the drupal_set_message of the acknowledgement.

global $user;
module_load_include('inc', 'node', 'node.pages');

$node = (object) array(
  'uid' => $user->uid,
  'name' => $user->name,
  'type' => 'article',
  'language' => 'und',
);
node_object_prepare($node);

// Get default values from attached fields.
$fields = field_info_instances('node');
$form_state = array();
foreach($fields['article'] as $field_name => $values) {
  $form_state['values'][$field_name] = array('und' => array());
}

// Add the $form_state field values.
$form_state['values']['title'] = 'my title';
$form_state['values']['body']['und'][0] = array(
  'value' => 'my body',
  'format' => 'full_html',
);
//$form_state['values']['field_MYFIELD']['und'][0]['value'] = 'blah blah';

// Without this line, not quite sure why, it wont work.
$form_state['values']['op'] = t('Save');

drupal_form_submit('article_node_form', $form_state, $node);

// Tell watchdog if any of the fields fail validation.
$errors = form_get_errors();
if (!empty($errors)) {
  foreach ($errors as $field_name => $message) {
    watchdog('MYMODULE', '%field: %message', 
      array('%message'=> $message, '%field' => $field_name));
  }
}

I did run this once with devel php console and it logged the node during the node creation. Hope this helps.

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.