I'm trying to submit a node throw my module with this

$edit = array();
$edit['type'] = 'my_content';

global $user;
$uid = $user->uid;
$edit['uid'] = 1;
$edit['promote'] = 0;
$edit['comment'] = 0;
$edit['status'] = 1;
$edit['title'] = 'Author '.$uid;

//cck field
$edit['field_field1'][0]['nid'] = $nid;

node_invoke_nodeapi($edit, 'my_content');

node_validate($edit);
if ($errors = form_get_errors()) {
//print_r($errors);
}

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

The node is generated correctly, the title is set as 'Author 1' (and it's ok cause i'm superadministrator), but the node author is set to anonymous even if I've set it to 1.
What am I doing wrong?
Doesn't node_save submit even author uid?

Comments

pobster’s picture

They're all supposed to be objects as per the api instructions; http://api.drupal.org/api/function/node_submit/6

Pobster

upupax’s picture

I guess there's no error..
but if I have a look to the db, the uid is set to 0 even if I put 1.

pobster’s picture

...Well I could write the exact same thing I wrote above as you've obviously not read it the first time... But instead, I'll just point you to an example.

http://acquia.com/blog/migrating-drupal-way-part-i-creating-node

OBJECT not ARRAY.

If you're properly having problems with understanding what I'm talking about then don't set the uid yourself use node_object_prepare instead (http://api.drupal.org/api/function/node_object_prepare/6).

Pobster

upupax’s picture

This seems really useful...
Thank you!

Ralla’s picture

node_submit(); overrides the uid if you have administrator rights to nodes. So this is what you want:

This would work:

$edit = array();
global $user;
$account = user_load(array('uid' => $user->uid));

$edit->uid = $account->uid;
$edit->type = 'my_content_type';
$edit->promote = 0;
$edit->comment = 0;
$edit->status = 1;
$edit->title = 'Author ' . $account->uid;

//cck fields
$edit->field_field1[0]['value'] = $nid;

// Because we are not using node_submit anymore, we have to set some extra values
$edit->body = 'Some content...'
$edit->teaser = 'A teaser...';
$edit->created = time();
$edit->validated = TRUE;

node_save($node);
paulgutib’s picture

is it ok if i use node_submit and then i just update the uid field and set it to the current user's uid? it really is not much of a hassle but the extra values you put are lines too many compared to just updating the uid. just a thought though. :)

alexmsilva’s picture

Thanks so much! this helped me a lot!!

upupax’s picture

This did the trick...
Thanks a lot!

Ralla’s picture

You're most welcome :)

duckzland’s picture

the saving will fails if the $node has more data to fill in such as language.

as http://drupal.org/node/398110 stated. the solution is to include $node->name before invoking node_submit()

--------------------------------------------------------------------------------------------------------
if you can use drupal why use others?
VicTheme.com