How to Programatically Populate Taxonomy using drupal_execute in Drupal 6?
vanderlip - June 2, 2009 - 16:39
Using the code below, I am programatically creating nodes using drupal_execute. The code below works well for me, but I have been unable to find the correct way to populate taxonomy terms into an existing single select vocabulary with no-freetagging. I have tried every example that I could find on drupal.org without success. I am quite far down the drupal_execute path, so node_save options are not as attractive.
Any guidance on how to populate taxonomy would be much appreciated.
<?php
// Create a new node
$form_state = array();
module_load_include('inc', 'node', 'node.pages');
$node = array('type' => 'autoevents');
$form_state['values']['title'] = 'This is the node title.';
$form_state['values']['field_principal_person'][0]['nid']['nid'] = '[nid:22]'; // Person - nodereference
$form_state['values']['field_link_to_place'][0]['nid']['nid'] = '[nid:35]'; // Location - nodereference
$form_state['values']['field_city'][0]['nid']['nid'] = '[nid:24]'; // City - nodereference
$form_state['values']['field_link_to_state_0'][0]['nid']['nid'] = '[nid:3736]'; // State - nodereference
$form_state['values']['field_country'][0]['nid']['nid'] = '[nid:14]'; // Country - nodereference
$form_state['values']['body'] = 'This is the body text.';
$form_state['values']['name'] = 'david';
$form_state['values']['op'] = t('Save');
drupal_execute('autoevents_node_form', $form_state, (object)$node);
?>
Try
The node import module before "playing" with drupal execute (I've had nightmares about using it!)http://drupal.org/project/node_import
have u tried taxonomy csv import module?
this is the link http://drupal.org/project/taxonomy_csv
i dont know if this will work for you but it auto-populates terms ( flat or with hierarchy ) as long as you play with the rules.If you find it diffcult in use please install the advance help module.It helped me a lot understanding the use of this module.
Thanks for your ideas!
Thanks ludo and nikitas! Actually, I am not bulk importing or creating many nodes. I am only creating one node at a time. When a URL is hit it creates a single new node based on the parameters above. This may not be mainstream functionality, but works for what I need. So I am really focused on finding a way to populate taxonomy within the construct above.
Any other Ideas?
If I was..
..you, I'd pray that Nevets "is in the building" If you find a solution please let us know.
Good Luck! The only other thing I could suggest is you Dissect the node import module to see how it deals with Taxonomy
sorry but my english are bad . . .
so if i understand this right . . . you could mess with the term_node table from drupal where it saves the node that connects with a vocab. and a term and so on.So if you can get the value for the taxonomy term ( try the devel module for this ) you can just pass it or find the drupal function that pass the value for it.But then again you might looking for this so i'm just spamming :D right now.
and the query could be something like this
query->insert into term_node values $nid,$vid,$tid but with drupal db functions
taxonomy_save_term($form_values)
you can use taxonomy_save_term($form_values)
$form_values = array(
'name' => 'new_term',
'vid' => VOCABULARY_ID,
'op' => 'save'
);
taxonomy_save_term($form_values);
Yep
believe me, taxonomy_save_term() is just fine. Use that.
If you want you use drupal_execute() for whatever reason, you can do that too, by invoking the taxonomy_form_term() form.
<?phpmodule_load_include('inc', 'taxonomy', 'taxonomy.admin');
?>
first though, to be sure it's available.
.dan.
A simple solution to determine the data structure needed ...
For me, the most difficult point was to determine the data structure $form_state, that has to be passed to drupal_execute($type.'_node_form', $form_state, (object)$node).
The simple solution was:
dpm($_POST);(if Module Devel is enabled)Le Duc de Brême - http://www.early-dance.de