0

I have a content type with a number of fields including a checkbox which allows you to select one or more taxonomy terms. If I create a new version of a node, all the previous taxonomy terms are automatically included in the new node version. I can see this in the database table "field_revision_field_catagories" (catagories is my checkbox field).

If I update the checkbox field with fewer terms checked, Drupal will not remove the existing ones. For example:

Node Version 1 has taxonomy terms 1,3,5 7 checked and 4 corresponding records in the database table.

  • I use node_load() to get Version one from the database.
  • I update the node using a Form API form and change the checkbox to select 2 (uncheck 1,3,5,7)
  • save the node with node_save().

The database now has Version 2 with taxonomy records 2,3,5,7. It updated the first record, changing it from 1 to 2. The other 3 records are still there, however and need to be removed for my logic to work.

I guess I can use SQL to delete the records (or just delete them all after node_load()), but will that create other problems? Is there a Drupal function I can use instead of SQL?

Thanks.

0

I figured it out. unset() is the answer. Here is my code:

        $loop = 0;  

    $termarr = $form_state['values']['catagories'];
    // drupal_set_message('Termarray '. print_r($termarr,1) .'');
    foreach ($termarr as $term) {
      if ($term > 0) {
        $node->field_catagories['und'][$loop] = array('tid' => $term);
      } else {
        unset($node->field_catagories['und'][$loop]);
      }
      $loop++;
    }

This took way longer than it should have. I wish Drupal had better documentation although trial and error for days does have some benefits... :-(

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.