There seems to be much debate regarding when to use node_save versus drupal_execute. Personally, I like the idea of drupal_execute, since it's just like the regular form executing--it handles validation for you, for example. This is an example of where I had to use node_save, but still needed validation.
There are some cases where you can't use drupal_execute, though. Recently I wrote an Ubiquity command to create nodes (time tracking) on our internal Redfin support site. Ubiquity collects the information for my node and makes it into JSON, which I post over to Services's JSON Server. Since Ubiquity is in the browser, I am just using cookies to authenticate the user on the Drupal side, rather than mess with Services's authentication with hashing and timestamps and whatnot.
So I was using services's built-in node.save, which calls drupal_execute. Unfortunately, KarenS's Date Module doesn't play nice with drupal_execute. KarenS recommends just using node_save, so I had to write a custom service to handle all that.
To get around validation issues, here's what I did in my custom service:
// validate this bad larry
$old_messages = $_SESSION['messages'];
$_SESSION['messages'] = array();
node_validate($incoming);
$new_messages = array();
$new_messages = $_SESSION['messages'];
if (!empty($new_messages['error'])) {
// we're about to bail, so reset messages so they're not displayed to the user again when they visit the site
unset($_SESSION['messages']);
// we did not validate, we need to bail.
return 'Could not create - '.strip_tags(implode(' | ', $new_messages['error']));
}
// restore any previous messages
$_SESSION['messages'] = $old_messages;
Post new comment