1

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->computed_file_extension[$node->language][0] = $term->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->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->field_file['und'][0]['fid'])) {
      $file = file_load($node->field_file['und'][0]['fid']);

// Split the filename and grab the last part to get the file extension.
      $extension = array_pop(explode('.', $file->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'  => $vocabulary->vid,
          'name' => $extension,
        );

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

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

    }
    else {

// If the file has been removed, remove the tag.
      unset($node->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;
  }
}
1

You're one level off; this:

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

Needs to be this:

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

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

  • Thanks a lot! Apparently I had been staring myself blind on this. Any idea why I cannot untag it with the unset function, though? – funkylaundry Nov 9 '15 at 10:50

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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