[development] programmatic creation of node

Ashraf Amayreh mistknight at gmail.com
Sat Mar 8 18:33:12 UTC 2008


Just wanted to point that this is just dummy code so no one thinks it's
actually anything related to the aggregation module itself. But the new
release will follow a similar pattern, unless I get feedback on a better way
to do it :)

On Sat, Mar 8, 2008 at 8:30 PM, Ashraf Amayreh <mistknight at gmail.com> wrote:

> For better or worse, this is the code I use in the aggregation module and
> it seems to be working flawlessly. This is in the yet to be released release
> though (drupal 5). If there's a better way I'd be glad to know it.
>
> (INSERT)
> $node = new stdClass();
> $node->title = "{$XML->name}";
> $node->body = "{$XML->description}";
> $node->eid = $eid;
>
> node_save($node);
> module_invoke_all('nodeapi', $node, 'insert', NULL, NULL);
>
> --------------
> (UPDATE)
> $node = node_load(array('nid' => 5));
> $node->title = "{$XML->name}";
> $node->body = "{$XML->description}";
> $node->eid = $eid;
>
> node_save($node);
> module_invoke_all('nodeapi', $node, 'update', NULL, NULL);
>
>
>
> On Sat, Mar 8, 2008 at 8:01 PM, Larry Garfield <larry at garfieldtech.com>
> wrote:
>
> > 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
> >
>
>
>
> --
> Ashraf Amayreh
> http://blogs.aamayreh.org




-- 
Ashraf Amayreh
http://blogs.aamayreh.org
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://lists.drupal.org/pipermail/development/attachments/20080308/61f909f0/attachment-0001.htm 


More information about the development mailing list