In D7, how do I programmatically add/modify fields to taxonomy terms? They are clearly fieldable and in UI I can add and edit fields in them just like I do in nodes but it proved to be no easy task in the code. Say I have a vocabulary named Fruit with terms like Apples and Bananas. I've added a field to it named main_article_id
, so all terms have Name, Description and Main Article Id. When I create a node named Apples that has a reference to the Fruit vocabulary (field_fruit
) and Apples term is selected, the code should update Apples term and set its main_article_id
field to $node->nid
. Here is my code:
//hook_node_insert fires after new node has been inserted into db
function MYMODULE_node_insert($node)
{
$taxonomy_term_id = $node->field_FRUIT[LANGUAGE_NONE][0]['value'];
$taxonomy_term = taxonomy_term_load($value['tid']);
if($taxonomy_term != FALSE)
{
//doesn't work
$taxonomy_term->field_main_article_id[LANGUAGE_NONE][0]['value'] = $node->nid;
taxonomy_term_save($taxonomy_term);
}
}