If you have a taxonomy that is not required, no entry is selected by default (even if the user selected '' before). If the user submits now without reselecting the '' entry, a PHP warning like "Warning: Duplicate entry '38-50' for key 1 query: INSERT INTO term_node (nid, tid) VALUES (50, 38)" for each selected taxonomy term shows up.

This happens if you have at least two taxonomies on one node and the user selects something in one taxonomy list but leaves the other blank.

Support from Acquia helps fund testing for Drupal Acquia logo

Comments

Zen’s picture

Assigned: Unassigned » Zen
nicklucas’s picture

I attempted to recreate the scenario on my system. I'm running the latest cvs build as of Friday March 10, 2006 10:00AM PST, with PHP 4.4.1 and MySQL 5.0.18. I created 2 vocabularies with multiple terms and then created a page, selecting a term from one category but not the other. The page was created without any issues.

Zen’s picture

FileSize
2.17 KB

There are 2 issues here:

a) Primary issue: Taxonomy forms (or I believe any select form) with the multiple select option cannot have all their elements deselected, once they have already been set a value. i.e. select elements which have their existing selections deselected will revert to their default values. I believe the same issue cropped up with checkboxes as well, and should be related to the following block of code:

    $posted = (isset($_POST['edit']) && ($_POST['edit']['form_id'] == $form_id));
    $edit = $posted ? $_POST['edit'] : array();
    $ref =& $form_values;

    foreach ($form['#parents'] as $parent) {
      $edit = isset($edit[$parent]) ? $edit[$parent] : NULL;
      $ref =& $ref[$parent];
    }

    $form['#ref'] = &$ref;
    if (!isset($form['#value'])) {
      if ($posted) {
        if (isset($edit)) {
          $form['#value'] = $edit; // normal element
          $form['#needs_validation'] = TRUE;
        }
        elseif (isset($form['#return_value'])) {
          $form['#value'] = '0'; // checkbox unchecked
          $form['#needs_validation'] = TRUE;
        }
      }
      if (!isset($form['#value'])) {
        $function = $form['#type'] . '_value';
        if (function_exists($function)) {
          $function($form);
        }
        else {
          $form['#value'] = $form['#default_value'];
        }
      }
    }

b) Immediate issue: Taxonomy forms for each vocabulary are set default values which also include terms from other vocabularies. So, due to the issue in a) when existing values for a vocabulary are deselected (i.e. there are no selections), the vocabulary reverts to its default values. These include values from other vocabularies as well and therefore trigger "duplicate" errors as the same term is saved twice (or more times). Patch attached. I've also added some minor formatting for some illegible (related) code.

-K

Zen’s picture

Status: Active » Needs review
killes@www.drop.org’s picture

looks good, can somebody test it?

moshe weitzman’s picture

i am still seeing the bug. i cannot clear the selections on a multiple select vocab

moshe weitzman’s picture

Status: Needs review » Needs work
kkaefer’s picture

Patch works for me. moshe: You can only clear the the selection if you don't have the required flag set on that vocabulary. However, if you have something selected and then select 'nothing' (not even '', the change is not saved. But if you select '', it works.

Zen’s picture

@Moshe: Yes, the patch only addresses the database error. The "clearing" issue needs to be fixed in form.inc. and is very likely applicable to all multi-select elements i.e. the attached patch only addresses part b.

-K

kkaefer’s picture

FileSize
2.28 KB

Perhaps the '' value should be preselected - at least that's why it's there. I added this to Zen's patch. Three lines of code.

moshe weitzman’s picture

Status: Needs work » Reviewed & tested by the community

works for me

Zen’s picture

Component: forms system » taxonomy.module

I've created a separate issue for the forms bug.

Thanks for the reviews Moshe, timcn.
-K

Dries’s picture

Status: Reviewed & tested by the community » Fixed

Committed to HEAD. Great job guys! :)

killes@www.drop.org’s picture

Status: Fixed » Needs review
FileSize
583 bytes

Zen told me that we don't need the code that timcn added. I propose to remove it.

Zen’s picture

Priority: Critical » Minor

Yep, selecting "none" by default etc. is unnecessary - it is there more to allow a user to "not select anything" than to tell the user that nothing has been selected. i.e. some end users might not be aware of "Ctrl + click" as an option to unselect an item.

I hadn't noticed that a different patch had gone through earlier - sorry about that.

Cheers,
-K

Zen’s picture

Status: Needs review » Reviewed & tested by the community

RTC-ing. Patch still applies albeit with an offset.

-K

killes@www.drop.org’s picture

Status: Reviewed & tested by the community » Fixed

applied

Zen’s picture

Status: Fixed » Closed (fixed)