7

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);
    }
}

1 Answer 1

9

The column name for a field isn't always value; in the case of taxonomy term references it's tid, and in the case of node/entity references it depends on what module you're using.

For a field created using the References module the name is nid, and for one created using the Entity Reference module, it's target_id.

The following code should work a bit better:

//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]['tid'];

    // $value['tid'] isn't defined anywhere in this function scope so I 
    // assume you meant to use $taxonomy_term_id here
    $taxonomy_term = taxonomy_term_load($taxonomy_term_id);
    if($taxonomy_term != FALSE)
    {
        $column_name = 'nid'; // Or 'target_id' if you're using entity reference            

        $taxonomy_term->field_main_article_id[LANGUAGE_NONE][0][$column_name] = $node->nid;
        taxonomy_term_save($taxonomy_term);
    }
}
7
  • You're right about $value['tid'], sorry for mistake in the code. But I don't use any modules to create my field. I just added it manually on the vocabulary Edit Fields page. I tried to look at taxonomy_term when editing it in the UI. Inside hook_taxonomy_term_presave($term) $term it looked like this in print_r(): stdClass Object ( [tid] => 20 [vid] => 6 [name] => Fruit ... [field_main_article_id] => Array ([und] => Array([0] => Array([value] => 14)))
    – Ari Linn
    May 16, 2012 at 15:41
  • Gotcha, that one should definitely be 'value' then. The taxonomy term reference, though, will also definitely be 'tid', so maybe you just need to change that line ($taxonomy_term_id = $node->field_FRUIT[LANGUAGE_NONE][0]['tid'];) and not the other one
    – Clive
    May 16, 2012 at 15:44
  • already done that. :-) Still it doesn't work. By the way I looked at the result taxonomy_term_load($taxonomy_term_id) produces, and it looked like this: stdClass Object([tid] => 1 [vid] => 2 [name] => Apples [description] => [format] => full_html [weight] => 0 [vocabulary_machine_name] => fruit [rdf_mapping] => ...). My preciousss field_main_article_id is nowhere to be found (I can see it in UI perfectly though). Should taxonomy_term_load operate this way?
    – Ari Linn
    May 16, 2012 at 16:01
  • Sorry again, my bad. I was being stupid and looked to the wrong place, that's why I didn't see any changes in my taxonomy terms. Your code works. Thanks.
    – Ari Linn
    May 16, 2012 at 16:27
  • Should taxonomy_term_save also work for updating a term's parents? I'm trying to use it for this purpose, but I'm running into an issue with updating the data. Jul 17, 2017 at 16:38

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.