The default term in single select widget should be empty. Instead, it defaults to the first term which will essentially makes this a required field as they are not able to unselect a term. The same goes for radios.

I had the need for a non-required, single-select widget, so I updated the code. after line 29 ($element['#options'] = $options;), I added this:

$element['#options'] = $options;

Became this:

if ($element['#type'] == 'select') {
  $element['#options'] = _term_reference_filter_by_views_array_unshift_assoc($element['#options'], '', '');
} else if ($element['#type'] == 'radios') {
  $element['#options'] = _term_reference_filter_by_views_array_unshift_assoc($element['#options'], '', 'N/A');
}

I also added a new function so that I could do an array_unshift and keep the index values (term IDs):

/**
 * Array unshift associative
 */
function _term_reference_filter_by_views_array_unshift_assoc(&$arr, $key, $val)  { 
  $arr = array_reverse($arr, true); 
  $arr[$key] = $val; 
  return array_reverse($arr, true); 
} 

Comments

capellic’s picture

Status: Active » Needs review

-- REMOVED --

capellic’s picture

Patch attached per the case above.

capellic’s picture

Here's a better patch which has "- None -" as the default display for the default, unselected value.

Alex Andrascu’s picture

Hi guys? What happened to this patch? It doesn't work against dev version but it looks like it applies well against alpha version.
Also dev version is older then alpha? I'm a bit confused here and need this patch commited.
Can I help somehow? :)

Jovean’s picture

Version: 7.x-1.x-dev » 7.x-2.x-dev
StatusFileSize
new2.41 KB

This is essential functionality, IMO. Anyway, here's a patch I've rolled against the 7.x.2.x branch ... I think it should work better.

vasike’s picture

Status: Needs review » Fixed

commited : http://drupalcode.org/project/term_reference_filter_by_views.git/commit/...
thank you guys

by the way, as Alex Andrascu said, the patch can't be applied. done manually
tahnks again

jimmyko’s picture

I have found the same problem and patched in a different way in #1922104. Please help to compare which approach is better.

Status: Fixed » Closed (fixed)

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