[development] programmatic creation of node

Larry Garfield larry at garfieldtech.com
Sat Mar 8 18:01:13 UTC 2008


Using drupal_execute() or drupal_execute_macro() is a nasty, evil, slow, and 
ugly way to programmatically create users and nodes.  It's also usually the 
only way that will reliably work. :-)  I have, however, run into cases where 
it does not.  For those, I have to fall back to creating the $node object 
myself and saving it.  Fortunately I've only had to do that for one-off 
imports, so I could hard-code defaults.  

There is no real good answer here, not until we get a real Data API in core.  
We're working on it. :-)

On Saturday 08 March 2008, Moshe Weitzman wrote:
> In drupal5, you should call hook_node_submit() and
> hook_nodeapi('submit') before node_save(). that will assure that any
> group info gets saved (for example).
>
> In d6 this is not needed since those submit hooks became
> hook_nodeapi(pre_save) and that happens from within node_save().
>
> drupal_execute is also a valid way. the node_load() is a little
> expensive but this is a very clean way to proceed. if it is working
> for you, i would proceed like this.
>
> On Sat, Mar 8, 2008 at 10:53 AM, Novák Áron <aron at novaak.net> wrote:
> > Hi!
> >
> >  While the development of FeedAPI, i faced with a problem. FeedAPI has to
> >  create nodes programmatically, i believed that this is a really easy
> > thing to do, feedapi now works like this:
> >  $node->data1 = "foo";
> >  $node->data2 = "bar";
> >  node_object_prepare($node); // this is about the default values
> >  node_save($node);
> >
> >  But I got a report that this is not a good way to do:
> >  http://drupal.org/node/196273
> >  Summary:
> >  "node_object_prepare() and node_prepare() functions are meant to
> > simulate the demonstration of a node"
> >  And some users, who use FeedAPI + 3rd party modules together, really
> >  experience bugs around node creation / handling:
> >  http://drupal.org/node/195105 (summary: the core forum module uses
> > form_alter to pass taxonomy-like data. And this data is lost now.)
> >
> >  Can you suggest me a perfect way to handle this problem?
> >  mustafau (http://drupal.org/user/207559) suggested to use
> >  drupal_execute($form_id, $form_values), but in this case, i had another
> >  problem: drupal_execute has no useful return value and $node structure
> >  remains unaltered, so i had to do a node_load after this, which is quite
> >  expensive.
> >
> >  Thanks,
> >
> >  Aron Novak


-- 
Larry Garfield			AIM: LOLG42
larry at garfieldtech.com		ICQ: 6817012

"If nature has made any one thing less susceptible than all others of 
exclusive property, it is the action of the thinking power called an idea, 
which an individual may exclusively possess as long as he keeps it to 
himself; but the moment it is divulged, it forces itself into the possession 
of every one, and the receiver cannot dispossess himself of it."  -- Thomas 
Jefferson


More information about the development mailing list