When I use the "" option, a new item is duly created - but the select list reverts to "". Wouldn't it make more sense for the new item to be selected by default?

Comments

Wim Leers’s picture

Component: Code - Content Taxonomy » Code - Taxonomy
Assigned: Unassigned » Wim Leers

It should be, but it indeed isn't. It was, in D6 and D5. Something must've gone wrong with the port to D7.

Wim Leers’s picture

Status: Active » Needs work

Any people experiencing this issue, please contribute by debugging this. Starting point: _hierarchical_select_process_calculate_selections(), specifically at:

        // Create the new item in the hierarchy and retrieve its value.
        $value = module_invoke($config['module'], 'hierarchical_select_create_item', check_plain($label), $parent, $config['params']);

It seems $value doesn't get set, and hence the following can't work either:

        // Ensure the newly created item will be selected after rendering.
        if ($value) {
          // Pretend there was a select where the "create new item" section
          // was, and assign it the value of the item that was just created.
          $element['#value']['hierarchical_select']['selects'][count($selects)] = $value;
        }
lpedretti’s picture

The hook in hs_taxonomy.module, line 639

  $status = taxonomy_term_save($term);

  if ($status == SAVED_NEW) {
    // Reset the cached tree.
    _hs_taxonomy_hierarchical_select_get_tree($params['vid'], 0, -1, 1, TRUE);

    // Retrieve the tid.
    $children = _hs_taxonomy_hierarchical_select_get_tree($params['vid'], $parent, 1);
    foreach ($children as $term) {
      if ($term->name == $label) {
        return $term->tid;
      }
    }

As the term is an object, it's passed by reference to taxonomy_term_save(), and in the inline documentation of taxonomy_term_save in drupal's taxonomy.module:

 * @return
 *   Status constant indicating whether term was inserted (SAVED_NEW) or updated
 *   (SAVED_UPDATED). When inserting a new term, $term->tid will contain the
 *   term ID of the newly created term.

so, replacing foreach ($children as $term) {...} with just return $term->tid; should be enough.

  $status = taxonomy_term_save($term);

  if ($status == SAVED_NEW) {
    // Reset the cached tree.
    _hs_taxonomy_hierarchical_select_get_tree($params['vid'], 0, -1, 1, TRUE);

    // Retrieve the tid.
    $children = _hs_taxonomy_hierarchical_select_get_tree($params['vid'], $parent, 1);
    
	return $term->tid;
  }

It fixed the issue for me.

Hope it helps!

Best regards

NWOM’s picture

Version: 7.x-3.0-alpha5 » 7.x-3.x-dev
Issue summary: View changes

I was having the same issue and the solution works great. In the current dev release, the code mentioned above, starts at line #771.

Would love to see this patched. Thanks.

stefan.r’s picture

Status: Needs work » Fixed

Committed to 7.x-dev

(see commit f196e92)

  • stefan.r committed f196e92 on 7.x-3.x
    Issue #1452114: "New item" isn't selected after creation
    

Status: Fixed » Closed (fixed)

Automatically closed - issue fixed for 2 weeks with no activity.