what's the best way to create nodes from code?

View: New views
8 Messages — Rating Filter:   Alert me  

what's the best way to create nodes from code?

by Xell Zhang :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Hi,

I have a batch of nodes need to be created from code, which function  
should i use as an entry?

thanks!

Re: what's the best way to create nodes from code?

by William Smith-7 :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Look into drupal_execute, although I often tend to create my node object explicitly and go via node_save, depending on the task.

On Fri, Oct 30, 2009 at 11:53 AM, Zhang Xiao <xellzhang@...> wrote:
Hi,

I have a batch of nodes need to be created from code, which function should i use as an entry?

thanks!


Re: what's the best way to create nodes from code?

by Alex Barth :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message


If speed is not an issue, use drupal_execute(). You're on the safe  
side with it, you don't need to worry about hook_nodeapi() /  
hook_form_alter() implementations of the moduls in use.

Otherwise, I settled for:

$node = new stdClass();
node_object_prepare($node);
// Add stuff.
$node->title = 'my title';
node_save($node);

On that note, I urge all module developers to not rely on form  
submission in their hook_nodeapi() implementations. Your module's node  
api implementation shouldn't break when a node is being created  
without using the way through the form api.

On Oct 30, 2009, at 11:53 AM, Zhang Xiao wrote:

> Hi,
>
> I have a batch of nodes need to be created from code, which function  
> should i use as an entry?
>
> thanks!

Alex Barth
http://www.developmentseed.org/blog
tel (202) 250-3633





Re: what's the best way to create nodes from code?

by hassafrass :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Isn't there a need for node_submit() as well before node_save(); ?

On Fri, Oct 30, 2009 at 9:52 AM, Alex Barth <alex@...> wrote:

If speed is not an issue, use drupal_execute(). You're on the safe side with it, you don't need to worry about hook_nodeapi() / hook_form_alter() implementations of the moduls in use.

Otherwise, I settled for:

$node = new stdClass();
node_object_prepare($node);
// Add stuff.
$node->title = 'my title';
node_save($node);

On that note, I urge all module developers to not rely on form submission in their hook_nodeapi() implementations. Your module's node api implementation shouldn't break when a node is being created without using the way through the form api.


On Oct 30, 2009, at 11:53 AM, Zhang Xiao wrote:

Hi,

I have a batch of nodes need to be created from code, which function should i use as an entry?

thanks!

Alex Barth
http://www.developmentseed.org/blog
tel (202) 250-3633






Re: what's the best way to create nodes from code?

by andrew morton-3 :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Yeah there's quite a few modules that expect the hook_nodeapi($op =
'submit') to fire.

andrew

On Fri, Oct 30, 2009 at 2:34 PM, Emma Irwin <emma.irwin@...> wrote:

> Isn't there a need for node_submit() as well before node_save(); ?
>
> On Fri, Oct 30, 2009 at 9:52 AM, Alex Barth <alex@...>
> wrote:
>>
>> If speed is not an issue, use drupal_execute(). You're on the safe side
>> with it, you don't need to worry about hook_nodeapi() / hook_form_alter()
>> implementations of the moduls in use.
>>
>> Otherwise, I settled for:
>>
>> $node = new stdClass();
>> node_object_prepare($node);
>> // Add stuff.
>> $node->title = 'my title';
>> node_save($node);
>>
>> On that note, I urge all module developers to not rely on form submission
>> in their hook_nodeapi() implementations. Your module's node api
>> implementation shouldn't break when a node is being created without using
>> the way through the form api.
>>
>> On Oct 30, 2009, at 11:53 AM, Zhang Xiao wrote:
>>
>>> Hi,
>>>
>>> I have a batch of nodes need to be created from code, which function
>>> should i use as an entry?
>>>
>>> thanks!
>>
>> Alex Barth
>> http://www.developmentseed.org/blog
>> tel (202) 250-3633
>>
>>
>>
>>
>
>

Re: what's the best way to create nodes from code?

by Domenic Santangelo :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

On Oct 30, 2009, at 11:34 AM, Emma Irwin wrote:

Isn't there a need for node_submit() as well before node_save(); ?

In D6, yes. Also to clarify Alex's point, a pure node_submit() + node_save() is indeed faster -- because it doesn't hit any hooks. The "proper" way to save a node programmatically is in the comments for drupal_execute():
 
* // Create a new node
 * $form_state = array();
 * module_load_include('inc', 'node', 'node.pages');
 * $node = array('type' => 'story');
 * $form_state['values']['title'] = 'My node';
 * $form_state['values']['body'] = 'This is the body text!';
 * $form_state['values']['name'] = 'robo-user';
 * $form_state['values']['op'] = t('Save');
 * drupal_execute('story_node_form', $form_state, (object)$node);
 */



On Fri, Oct 30, 2009 at 9:52 AM, Alex Barth <alex@...> wrote:

If speed is not an issue, use drupal_execute(). You're on the safe side with it, you don't need to worry about hook_nodeapi() / hook_form_alter() implementations of the moduls in use.

Otherwise, I settled for:

$node = new stdClass();
node_object_prepare($node);
// Add stuff.
$node->title = 'my title';
node_save($node);

On that note, I urge all module developers to not rely on form submission in their hook_nodeapi() implementations. Your module's node api implementation shouldn't break when a node is being created without using the way through the form api.


On Oct 30, 2009, at 11:53 AM, Zhang Xiao wrote:

Hi,

I have a batch of nodes need to be created from code, which function should i use as an entry?

thanks!

Alex Barth
http://www.developmentseed.org/blog
tel (202) 250-3633


--
Domenic Santangelo
WorkHabit, Inc.
[Direct  ]530-902-2576
[Office  ]866-WorkHabit
[Skype   ]dsantangelo
[Freenode]entendu @ #drupal




Re: what's the best way to create nodes from code?

by andrew morton-3 :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

On Fri, Oct 30, 2009 at 2:44 PM, Domenic Santangelo
<domenic@...> wrote:
> On Oct 30, 2009, at 11:34 AM, Emma Irwin wrote:
>
> Isn't there a need for node_submit() as well before node_save(); ?
>
> In D6, yes. Also to clarify Alex's point, a pure node_submit() + node_save()
> is indeed faster -- because it doesn't hit any hooks. The "proper" way to
> save a node programmatically is in the comments for drupal_execute():

Maybe I'm being a bit too literal but saying you're not hitting *any*
hooks is incorrect. You avoid all the form building and altering calls
but you definitely fire off hooks submit, insert/update hooks.

andrew

Re: what's the best way to create nodes from code?

by Domenic Santangelo :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

On Oct 30, 2009, at 11:57 AM, andrew morton wrote:

> On Fri, Oct 30, 2009 at 2:44 PM, Domenic Santangelo
> <domenic@...> wrote:
>> On Oct 30, 2009, at 11:34 AM, Emma Irwin wrote:
>>
>> Isn't there a need for node_submit() as well before node_save(); ?
>>
>> In D6, yes. Also to clarify Alex's point, a pure node_submit() +  
>> node_save()
>> is indeed faster -- because it doesn't hit any hooks. The "proper"  
>> way to
>> save a node programmatically is in the comments for drupal_execute():
>
> Maybe I'm being a bit too literal but saying you're not hitting *any*
> hooks is incorrect. You avoid all the form building and altering calls
> but you definitely fire off hooks submit, insert/update hooks.

No no you're right, I should have been clearer (or not responded when  
on a phone call :).

-D


--
Domenic Santangelo
WorkHabit, Inc.
[Direct  ]530-902-2576
[Office  ]866-WorkHabit
[Skype   ]dsantangelo
[Freenode]entendu @ #drupal