Using a custom php to write nodes.
Taxonomy free-tagging associated to nodes.
in 6.x i was using taxonomy_node_save().
Now i have to use only node_save() and fill the items for taxonomy with the tid, In order to know this tid, I started to make my own script for 7.x and it is working.
function taxonomy_tid($tname, $voc, $vid) {
$taxo = taxonomy_get_term_by_name($tname);
if (!$taxo) {
$taxo = new stdClass();
$taxo->vocabulary_machine_name = $voc;
$taxo->name = $tname; print_r($taxo);
$taxo->vid = $vid;
$status = taxonomy_term_save($taxo);
$taxo = taxonomy_get_term_by_name($tname);
}
return $taxo->tid;
}
but it doesn't look very clean isn't it?
Is there a better way?
Comments
Comment #1
Anonymous (not verified) commentedI advise you to save the node properly. Everything is handled by Field Attach API now. This is by design.
Comment #2
egarias commentedThanks bangpound
here is the solution i found, now it works, but my impresion is that is not very clean and surely it is another way to do it, if someone wants to share, here is my code.
Comment #3
jereoms commentedI was looking to do this very same thing and I actually came up with almost the same exact function during development. Would love to know if there is a script provided by drupal core that alleviates the need for the taxonomy_tid function above?
Comment #4
xjmThere's a few issues that include similar proposals:
#1169964: Taxonomy autocomplete: decouple from Field API and factor out a helper so that code is reusable
#143817: API: Get just term ID by name
#1140188: Fatal errors during or after adding default values for autocomplete widgets
Comment #5
drzraf commentedI can't see which one of the above bugs deals with the missing taxonomy_node_save().
And this is an annoying issue. It's really (really) boring to bind terms to the node in the X possibilities
before finding the _right_data_structure_Drupal_expects.
That's the goal of the API to make data structure simpler to use.
here is a hint: http://yoodey.com/how-create-taxonomy-node-programmatically-drupal-7
Can we know why this helper has been removed ?
Comment #6
drzraf commentedrelated, if not the cause: #412518: Convert taxonomy_node_* related code to use field API + upgrade path