[development] programmatic creation of node

Victor Kane victorkane at gmail.com
Wed Mar 12 09:16:50 UTC 2008


Right, for example:

$node = new stdClass();
$node->title = 'foo';
$node->uid = 1;
$node->name = admin;

... (lot of other stuff)

$node = node_submit($node);
node_save($node);

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.

Victor Kane
http://awebfactory.com.ar


On Wed, Mar 12, 2008 at 1:37 AM, Nancy Wichmann <nan_wich at bellsouth.net>
wrote:

> I had a 5.x module where I was creating a node and it wouldn't even save
> the uid unless I also set the username.
>
> Nancy E. Wichmann, PMP
>
>
> -----Original Message-----
> From: development-bounces at drupal.org [mailto:
> development-bounces at drupal.org]On Behalf Of Omar Abdel-Wahab
> Sent: Tuesday, March 11, 2008 11:30 PM
> To: development at drupal.org
> Subject: Re: [development] programmatic creation of node
>
> Any ideas why does this fails to set the node author:
> $node = new stdClass();
> $node->title = 'foo';
> $node->uid = 1;
> node_submit($node);
> node_save($node);
>
> While this succeeds:
> $node = new stdClass();
> $node->title = 'foo';
> node_submit($node);
> $node->uid = 1;
> node_save($node);
>
>
> On Mon, Mar 10, 2008 at 9:17 PM, Earnie Boyd
> <earnie at users.sourceforge.net> wrote:
> > Quoting Victor Kane <victorkane at gmail.com>:
> >
> >  > On Mon, Mar 10, 2008 at 10:16 AM, Earnie Boyd <
> earnie at users.sourceforge.net>
> >  > wrote:
> >  >
> >  >> Quoting Victor Kane <victorkane at gmail.com>:
> >  >>
> >  >> > I do this a lot, so I will ad my two cents.
> >  >> >
> >  >> > The following line is absolutely essential for "good
> housekeeping",
> >  >> > especially if you are using cck fields:
> >  >> >
> >  >> > $node = node_submit($node);
> >  >> >
> >  >> > However, it will NOT work if ... it shouldn't; for example if you
> omit
> >  >> (as
> >  >> > does the above code) initializing the uid attribute.
> >  >> >
> >  >>
> >  >> Which is what http://api.drupal.org/api/function/node_object_prepare
> >  >> does when nid is not set in the node object.
> >  >
> >  >
> >  > OK, great. But I thought I read KarenS stating somewhere that the
> >  > node_submit($node) was good when we are dealing with CCK fields...
> would be
> >  > good to know
> >  >
> >
> >  Maybe because CCK uses the nodeapi ``presave'' operation which
> >  node_save calls first.
> >
> >  <code
> >  source="
> http://cvs.drupal.org/viewvc.py/drupal/contributions/modules/cck/content.module?view=markup
> >
> >  /**
> >  * Nodeapi 'presave' op.
> >  *
> >  */
> >  function content_presave(&$node) {
> >   _content_field_invoke('presave', $node);
> >   _content_field_invoke_default('presave', $node);
> >  }
> >  </code>
> >
> >
> >
> >
> >  Earnie -- http://for-my-kids.com/
> >  -- http://give-me-an-offer.com/
> >
> >
>
>
>
> --
> Omar Abdel-Wahab
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://lists.drupal.org/pipermail/development/attachments/20080312/34aa29d8/attachment.htm 


More information about the development mailing list