The node_save() function is far easier to use and much more reliable. For node_save() you only need to know how to construct the node and every field is more or less the same: $node->FIELD_NAME[0]['value'], and 'value' is the value that is stored in the database (the actual column names vary from one field to another, so you still need to know what they are). When you use drupal_execute you have to construct the node as it has been re-organized by all the field widgets, which requires understanding exactly what each widget looks like and what values it expects where. Some widgets (like optionwidgets and date widgets) are extremely complex and the data used on the node form looks totally different than the data that ultimately gets stored in the database. So you have to pre-process your data into whatever the widget expects.<br>
<br>The biggest problem is a long-running core bug (that may or may not have ever gotten fixed) that kept validation hooks from firing if you do drupal_execute() on multiple nodes in a single pass, as when you are migrating or importing data. That means that any massaging that might be done by the widgets during validation also does not get done and those types of fields are totally broken. Date fields and nodereference fields are two that have created lots of problems.<br>
<br>The only thing you lose by using node_save() is that it does not do validation, but the bug means it doesn't get done reliably anyway. It would be far easier to do your own data validation before you import and then do a simple node_save() to store it.<br>
<br>Karen<br><br><div class="gmail_quote">On Fri, Jul 29, 2011 at 7:57 PM, Emma Irwin <span dir="ltr"><<a href="mailto:emma.irwin@gmail.com">emma.irwin@gmail.com</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex;">
I've used node_save without having to worry about defaults for irrelevant fields. Worked fine for CCK for me<br>
Personally, I think that your alternate solution & function goes too far into a zone of complexity that you'll have more bugs as a result. I would also feel for any developer that comes after you and has to maintain that code :/<br>
<br>
Sent from my iPhone<br>
<div><div></div><div class="h5"><br>
On Jul 29, 2011, at 12:21 PM, "David Cohen" <<a href="mailto:drupal@dave-cohen.com">drupal@dave-cohen.com</a>> wrote:<br>
<br>
> I'm trying import some data from a user's Facebook profile into Drupal.<br>
> The site in question has a lot of moving parts: Content Profile, CCK,<br>
> Drupal for Facebook.<br>
><br>
> I'd like to query facebook for info about the user, then create or<br>
> update a node with the data. The node contains a lot of fields, and I'm<br>
> only interested in updating 1 or 2 (or 3 or 4) of them.<br>
><br>
> My preference is to use drupal_execute() to create/update the profile<br>
> node. To do this, I have to fill out a $form_state['values'] array.<br>
> I've tested this and noticed that if I leave out the values for any<br>
> field in the node, that field will be reset to blank. In other words my<br>
> code needs to fill out values for all fields. But to do so essentially<br>
> prevents anyone from adding more fields via CCK. The code would need to<br>
> be updated for every change to the content type, which is too onerous.<br>
><br>
> There are posts on <a href="http://drupal.org" target="_blank">drupal.org</a> describing this same problem, and<br>
> suggesting using node_save() instead of drupal_execute() for this<br>
> purpose. What do the experts on this list recommend, drupal_execute()<br>
> or node_save()?<br>
><br>
> I had the thought that during hook_form_alter(), I could scan the form<br>
> for all #default_values, and use them to populate the<br>
> $form_state['values']. A function able to do that would, it seems to<br>
> me, make drupal_execute() much easier to use. That code would have to<br>
> navigate the entire $form tree (at least all element children). And<br>
> would have to be smart about where #tree is true in order to get all the<br>
> #default_values into the right $form_state['values']. Has anyone<br>
> attempted such a thing?<br>
><br>
> Thanks in advance, -Dave<br>
</div></div></blockquote></div><br>