[development] programmatic creation of node

Omar Abdel-Wahab owahab at gmail.com
Wed Mar 12 12:54:47 UTC 2008


Guys, I didn't mean the example literally, I was just trying to
explain my point. :)

> If you look at http://api.drupal.org/api/function/node_submit/5 you will see that the parameter is not a reference and that the function returns the node object.
Thanks for the explanation, now things make sense.


On Wed, Mar 12, 2008 at 1:58 PM, Victor Kane <victorkane at gmail.com> wrote:
> Actually...
>
> Very often you want to put in a user name and user uid different from the
> admin who is running a script.
>
> In my case, I am admin / uid 2, but I want the "webmaster" (uid 4) to figure
> as the author, so it would not be the current user.
>
> It would be a parameter of some kind (in the running of the script, say).
>
> Victor
>
>
>
> On Wed, Mar 12, 2008 at 9:55 AM, Earnie Boyd <earnie at users.sourceforge.net>
> wrote:
>
> > Quoting Omar Abdel-Wahab <owahab at gmail.com>:
> >
> > Assuming you might want to allow for possible other users than uid 1:
> >
> >
> > > Any ideas why does this fails to set the node author:
> > global $user;
> > $user = user_load(array('uid'=>$uid); //$uid is an optional function
> > parameter with a default of 1.
> > //If the action is a temporary switch to admin user be sure to save the
> > original //value of $user so you can reset it before exiting.
> >
> > > $node = new stdClass();
> > > $node->title = 'foo';
> > > $node->uid = 1;
> > //change to
> > $node->uid = $uid;
> > $node->name = $user->name;
> > > node_submit($node);
> > //Node submit is usually called after user clicks submit and is called to
> > //prepare the node for the save.  Things like converting the $node from an
> > //array to an object, adding uid to the node, adding the teaser to the
> > node and
> > //invoking modules with _submit and _nodeapi hooks implemented.  If you
> don't
> > //need or want these things then don't call it.
> >
> > > node_save($node);
> > >
> > > While this succeeds:
> > > $node = new stdClass();
> > > $node->title = 'foo';
> > > node_submit($node);
> > > $node->uid = 1;
> > > node_save($node);
> > >
> > //Node submit sets $node->uid to zero if $node->name isn't loaded by
> > user_load.
> >
> >
> >
> >
> >
> > Earnie -- http://for-my-kids.com/
> > -- http://give-me-an-offer.com/
> >
> >
>
>



-- 
Omar Abdel-Wahab


More information about the development mailing list