Posted by DayShallCome on April 29, 2007 at 9:17pm
I am trying to programmatically insert nodes into my DB, and I have a few questions/issues.
1) What's the difference between using drupal_execute() and node_save()? It seems like they do the same thing.
2) I can't seem to insert taxonomy term/category for nodes that I'm saving. It seems like node_save and drupal_execute simply don't do anything with the taxonomy information that I'm passing to them. Is there some reason for this? Is there a special way to programmatically categorize nodes?
Thanks,
DSC
Comments
Considering that
Considering that drupal_execute() takes a form as input and node_save() takes a node, you'd think there'd be quite a bit of difference, but ultimately the former calls the latter if what you are submitting is a node_form (as opposed to the general, run of the mill, form).
I didn't see a way to put the taxonomy info in the form submission for drupal_execute. Instead, I used the following to get the taxonomy set on the node I just created/saved:
$term = taxonomy_get_term_by_name($category);
taxonomy_node_save($nid, $term);
It may be possible to stuff it into the node object first and let the normal insert/update handle it. You might try setting the $node->taxonomy variable to your category object ($term in my example above) before your call to node_save() - I never tried that, but it may work.
bjvetter, Thanks for the
bjvetter,
Thanks for the response, that mostly answers my question. I took a look through the save_node() code, but it doesn't seem to deal with taxonomy.
Using your method, what is $category? As data, I have the tid of the term I am trying to associate the new node with. Is $category something different than that?
Thanks.
...
It does
node_invoke_nodeapi($node, 'insert'), which invokes code in the taxonomy module -- code that does record into DB the terms attached to the node.Example:
...$node->taxonomy = array(45, 78, 12);
...
node_save($node);
Note that the taxonomy module allows for various formats for the $node->taxonomy member. I used a plain array to hold the TIDs, but other variations are possible.
Using
drupal_execute()is possible too.TIP:
Install the 'devel' module and the accompanying 'macro' module. You now have a 'Macro engine' that produces PHP code (which uses 'drupal_execute()') for submission of every form in the system -- including taxonomized nodes.
Yes, use the devel and macro module...
Hope this will help others.
It took me a couple hours to figure out how to add taxonomy terms.
Best advice is to use the 'devel' module and the accompanying 'macro' module.
But first, clear out anything you may have in the macro module, and then just add (or edit)
with your node add (edit) form. I foolishly just opened up the macro engine
export pane and found 5,000 lines of incomprehensible verbiage. But clearing it
out and adding (editing) a node left nice simple code for me to work with.
Here's a "real" example that may help. I'm just playing with the taxonomy terms,
so there's duplication and what not. I'm experimenting with both free tagging
('tags') and the older 'fixed' tags.
$form_values = array(
'title' => $locationInfoObj->City . ", " . $locationInfoObj->State . " " . $locationInfoObj->Zip5,
'body' => 'This BODY node was submitted programmatically.',
'path' => 'you-can-also-add-url-paths' . $locationInfoObj->City,
'taxonomy' =>
array (
'tags' =>
array (
2 => $locationInfoObj->City, // 2 (or 6) is the form key visible with devel module
6 => $locationInfoObj->City, // eg. Cities (key=2, weight=0):
1 => 'MA',
),
4 => '0', // "State import"; 0 implies "nothing"
5 => '20', // "StatesHier"
),
); // form_values
$node = array('type' => 'mymoduletype');
drupal_execute($form_id, $form_values, $node); // this inserts node
Social Security credit cards
income taxes debt relief