[development] programmatic creation of node

Victor Kane victorkane at gmail.com
Wed Mar 12 11:58:08 UTC 2008


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/
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://lists.drupal.org/pipermail/development/attachments/20080312/efdcb7c6/attachment.htm 


More information about the development mailing list