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
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@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
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@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@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
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@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@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@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
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@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@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@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@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
Quoting Ashraf Amayreh <mistknight@gmail.com>:
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@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);
http://api.drupal.org/api/function/node_save already calls the module hooks for nodeapi via http://api.drupal.org/api/function/node_invoke_nodeapi before returning. Therefore your module_invoke_all isn't needed. Earnie -- http://for-my-kids.com/ -- http://give-me-an-offer.com/
First, thanks for everyone who replied to this thread! 2008. március 8. dátummal Larry Garfield ezt írta:
There is no real good answer here, not until we get a real Data API in core. We're working on it. :-)
Do you have an estimate when Data API will be appear in core? :) Aron
Quoting Moshe Weitzman <weitzman@tejasa.com>
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.
The documentation to convert hook_submit to drupal 6 says to add $form['#submit'], not to create a nodeapi(presave). I'm a bit confused here. Should I convert my hook_submits to nodeapi(presave) or should I simple add $form['#submit'] to the form? I believe if I change it to nodeapi(presave) then I won't need to call hook_node_submit() before calling node_save() as this would be taken care by the presave call in node_save(). But it seems that even in drupal 6, if I use $form['#submit'] I'll still have to do a call to the hook_node_submit() before calling node_save(). Am I missing something here?
In drupal 4.7 and 5.x I have found that $node = node_submit($node); node_save($node); generally works.
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. So, use this function, and include all the necessary fields to make it work, it is a good test. Larry is right, what is needed is a better solution... saludos, Victor Kane http://awebfactory.com.ar On Sat, Mar 8, 2008 at 4:21 PM, Steve Ringwood <nevets@mailbag.com> wrote:
In drupal 4.7 and 5.x I have found that
$node = node_submit($node); node_save($node);
generally works.
Quoting Victor Kane <victorkane@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.
So, use this function, and include all the necessary fields to make it work, it is a good test.
Larry is right, what is needed is a better solution...
mymodule_nodeapi? Filter on ``presave'' value for $op? Then the main code just does the node_save. Earnie -- http://for-my-kids.com/ -- http://give-me-an-offer.com/
On Mon, Mar 10, 2008 at 10:16 AM, Earnie Boyd <earnie@users.sourceforge.net> wrote:
Quoting Victor Kane <victorkane@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 Victor
So, use this function, and include all the necessary fields to make it work, it is a good test.
Larry is right, what is needed is a better solution...
mymodule_nodeapi? Filter on ``presave'' value for $op? Then the main code just does the node_save.
Earnie -- http://for-my-kids.com/ -- http://give-me-an-offer.com/
Quoting Victor Kane <victorkane@gmail.com>:
On Mon, Mar 10, 2008 at 10:16 AM, Earnie Boyd <earnie@users.sourceforge.net> wrote:
Quoting Victor Kane <victorkane@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/
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@users.sourceforge.net> wrote:
Quoting Victor Kane <victorkane@gmail.com>:
On Mon, Mar 10, 2008 at 10:16 AM, Earnie Boyd <earnie@users.sourceforge.net> wrote:
Quoting Victor Kane <victorkane@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
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@drupal.org [mailto:development-bounces@drupal.org]On Behalf Of Omar Abdel-Wahab Sent: Tuesday, March 11, 2008 11:30 PM To: development@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@users.sourceforge.net> wrote:
Quoting Victor Kane <victorkane@gmail.com>:
On Mon, Mar 10, 2008 at 10:16 AM, Earnie Boyd <earnie@users.sourceforge.net> wrote:
Quoting Victor Kane <victorkane@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
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@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@drupal.org [mailto: development-bounces@drupal.org]On Behalf Of Omar Abdel-Wahab Sent: Tuesday, March 11, 2008 11:30 PM To: development@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@users.sourceforge.net> wrote:
Quoting Victor Kane <victorkane@gmail.com>:
On Mon, Mar 10, 2008 at 10:16 AM, Earnie Boyd < earnie@users.sourceforge.net> wrote:
Quoting Victor Kane <victorkane@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.mod...
/** * 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
Quoting Omar Abdel-Wahab <owahab@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/
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@users.sourceforge.net> wrote:
Quoting Omar Abdel-Wahab <owahab@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/
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@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@users.sourceforge.net> wrote:
Quoting Omar Abdel-Wahab <owahab@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
I created http://drupal.org/project/node_factory for programmatic creating nodes. It is not complete let alone flawless :-/ This thread is now documented in http://drupal.org/node/231922 ... Is it wise to consolidate these findings into this module? We can collect best practive into this module as a documented example and a useful module for the less fortunate programmers. I started this module as a php 'integration' with workflow_ng where simple cck fields are usable. But I got stuck with ie imagefield. Regards, Clemens Op 8 mrt 2008, om 16:53 heeft Novák Áron het volgende geschreven:
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
On Saturday 08 March 2008 23:53, Novák Áron wrote:
i had another problem: drupal_execute has no useful return value
see: http://drupal.org/node/84068 A.
participants (11)
-
Ashraf Amayreh -
Augustin (Beginner) -
Clemens Tolboom -
Earnie Boyd -
Larry Garfield -
Moshe Weitzman -
Nancy Wichmann -
Novák Áron -
Omar Abdel-Wahab -
Steve Ringwood -
Victor Kane