Updating a term reference programatically I've written a module that - among other things - automatically categorizes uploaded files based on their file extension. The idea is simple: the module programatically sets up a taxonomy and automatically adds new terms to it whenever it stumbles upon a new file extension (i.e. pdf, .doc, .jpg etc.).

I cannot get it to work, however. The taxonomy terms are succesfully added to the taxonomy and I can go and investigate them in the admin interface, but the term reference between the node and the term is never saved to the database. I use $node-&gt;computed_file_extension[$node-&gt;language][0] = $term-&gt;tidto save the reference and I'm pretty sure that I've done this with sucess in the past.

I'm actually a bit confused about using 'und' or $node-language. I suppose $node-&gt;langugage is the more universal of the two, but it seems it does not always work - in this case I've tried both without success. 

When I inspect my node object I can see that the reference is on the node object, but there are no records added to the table taxonomy_index in the DB when node_save() is triggered.

I'm triggering this function on either node update, node insert or node delete and only if changes have been made to the field where I store the uploaded file (i.e. a file has been added or removed).

function _uvp_fields_update_field_file_extension($node) {

// Load the vocabulary called 'file extensions'.
  $vocabulary = taxonomy_vocabulary_machine_name_load('file_extensions');

  if (!empty($vocabulary)) {
    if (!empty($node-&gt;field_file['und'][0]['fid'])) {
      $file = file_load($node-&gt;field_file['und'][0]['fid']);

// Split the filename and grab the last part to get the file extension.
      $extension = array_pop(explode('.', $file-&gt;filename));

      $term = current(taxonomy_get_term_by_name($extension));

// If there is not already a taxonomy term with the given extension, create it.
      if (empty($term)) {

// Build taxonomy term object.
        $term = (object) array(
          'vid'  =&gt; $vocabulary-&gt;vid,
          'name' =&gt; $extension,
        );

// Save the taxonomy term.
        taxonomy_term_save($term);
      }

// Add taxonomy term reference.
      $node-&gt;computed_file_extension[$node-&gt;language][0] = $term-&gt;tid;
//$node-&gt;computed_file_extension['und'][0] = $term-&gt;tid;

    }
    else {

// If the file has been removed, remove the tag.
      unset($node-&gt;computed_file_extension['und'][0]);
    }

// Save the node.
    node_save($node);

    return TRUE;

  }
  else {

    watchdog('uvp_fields', 'The vocabulary "file_extensions" does not exists.');

    return FALSE;
  }
}
You're one level off; this:

$node-&gt;computed_file_extension[$node-&gt;language][0] = $term-&gt;tid;


Needs to be this:

$node-&gt;computed_file_extension[$node-&gt;language][0]['tid'] = $term-&gt;tid;


Without specifying the field column (tid in this case), Drupal doesn't know which DB field the data should be stored in.