What is the proper way to add a - None - to the select list (or is it currently not possible)?

I tried the following below but it does not allow me to select - None - as the default option.

|-None -
Example|Example
option|Other

Also I realize that not entering any data into the text field when Option is selected does the same effect but I would rather not have Other be my primary field. Preferably - None - would not have a text field with it.

Thank you for any and all help! Cheers.

Support from Acquia helps fund testing for Drupal Acquia logo

Comments

jcarlson34’s picture

Oops typo... by option|Other I meant other|Other ;)

jcarlson34’s picture

Well I might have answered my own question (there's probably a better long term solution than this code) but here's the modifications I made to cck_select_other.module to get - None - to work:

<?php
  // Set default value if we have instance data for delta, a default
  // value setting, or something basic if none at all.
  if (!empty($items[$delta]['value'])) {
    $def = (in_array($items[$delta]['value'], array_keys($options))) ? $items[$delta]['value'] : 'other';
    $otherdef = ($def == 'other') ? $items[$delta]['value'] : '';
  }
  else if (isset($instance['default_value'])) {
    $def = isset($instance['default_value'][0]['select_other_list']) ? $instance['default_value'][0]['select_other_list'] : 'other';
    $otherdef = ($def == 'other') ? $instance['default_value'][0]['value'] : '';
  }
  else {
    $def = ''; //Changed from $def = 'other'
    $otherdef = '';
  }
?>
<?php
function cck_select_other_options($field) {

  if (!isset($field['widget'])) {
    return array();
  }

  $options = eval($field['widget']['settings']['select_list_options_fieldset']['advanced_options']['select_list_options_php']);

  if (empty($options)) {
    $options[''] = t('- None -'); //Added optional None here
	$options_str = $field['widget']['settings']['select_list_options'];
    if (!empty($options_str)) {
      $options_arr = preg_split("/[\r]?[\n]/", $options_str);
      if (count($options_arr) > 0) {
        foreach ($options_arr as $option_str) {
          $option_arr = preg_split("/\|/", $option_str);
          if (count($option_arr) == 2) {
            $options[check_plain($option_arr[0])] = t('@option', array('@option' => $option_arr[1]));
          }?>
mradcliffe’s picture

Component: Documentation » Code
Assigned: Unassigned » mradcliffe
Category: support » feature
Status: Active » Needs work

I agree. I think that for non-required select lists, a -None- option should be provided that bypasses things and sets the value to an empty string.

It will probably look a bit different from what you have up there, but should be similar.

cwithout’s picture

Thirded. But I also think the option should be available for required fields as well. D7 core select now has this ability.

Forcing a default is not a good UX, since often users skip a question that's already got an answer pre-selected. When they submit the form, instead of being prompted to make a selection, the user ends up with the default answer.

mradcliffe’s picture

Oh, I see. Yes, that does make sense to me too now.

mradcliffe’s picture

Status: Needs work » Needs review
FileSize
6.3 KB

This is almost ready. I think there's one case that's not covered that I will need to write a test for and maybe fix.

What happens when you save a field with a value, and then select -none- after that?

mradcliffe’s picture

Status: Needs review » Fixed
cwithout’s picture

Thanks for working on this. I worked around needing this module for my last project with some custom jQuery and Views templates, but I'm sure I'll need the functionality again in the future, and this module will make that easier with this feature working.

Unfortunately, I installed the current dev, but wasn't able to get it to work. I edited an existing content type to add a non-required Select Other field. Then I edited an existing node, and was able to set the value of that field to "Other" and manually enter the other answer. That node saved successfully.

However, when I went to edit that node, the value I had set it to didn't show up. I entered another value, and saved. I got the error "list_illegal_value". No matter what I set the field to, I get the error "list_illegal_value".

I repeated on several nodes and content types. Sometimes I was able to save. Sometimes I got the error "list_illegal_value". Often I was only able to save if I selected "none" or the previous value but not if I wanted to change the value. I never saw the current value get populated when editing a node.

I haven't used this module before, since it doesn't meet my needs without this patch, so I'm not sure if I configured the field as this module needs.

I have under both "Select list options" and "Allowed values list":

1|Something
2|Another Thing
3|Yet Another
other|Other
mradcliffe’s picture

Status: Fixed » Needs work

Hmm, I do not think I wrote a test to test what happens when you have a non-required field with allowed values configured. You don't need to use the allowed values if you don't need to. It restricts values just to that list.

cwithout’s picture

Ah. Ok. Since it's the first thing that comes up when you create the field, I assumed allowed values were probably needed. I cleared out all the existing values on nodes (set them all to "none") so I could remove the values from the allowed values list. Now I'm able to change the value of the Select Other field without the error.

Is there a way to get rid of or hide the allowed values list? If not, it would be good to document that adding allowed values causes problems.

With that taken care of, it's working better, but I'm still seeing a couple issues:
1. Numeric keys throw off the functionality.
2. Displaying keys rather than values on node view. (Except "other" value. Other shows up correctly.)

Below are the details on my testing.

Issue #1: Using the options in #8, if I edit a node where I've set the value to "Other", the value I type into the text field shows up in the node view. (works) But on the edit form, that value doesn't get populated. It comes up with "none". The rest of the values show up correctly on the edit form, except when I have the first option "Something" selected. That also comes up as "none".

The values for every option besides "other" also don't show up correctly in the node view. Example: When I select "Something", and view the node, it shows the value is "0" (though its key is "1"). When I set it to "Another Thing", when viewing the node, it shows "1" (its key is "2"). It appears it may be showing their position in the list rather than the value or the key.

I guessed that the issue might be that the keys are integers. It seems that's the case. On the same content type, I added another Select Other field with strings as the key, and the values are populated correctly on edit.

Issue #2: With non-integer keys, I get the correctly-selected values on viewing the node -- except that it shows the key rather than the value. Example: In the select options, I have "stuff|Stuff". On node view, I see "stuff" rather than "Stuff".

I changed the keys in the field used for my first example to non-integers and got these same results. This strongly suggests it was the integers causing issue #1.

My example data:
Doesn't work. Results in wacky behavior:

1|Something
2|Another Thing
3|Yet Another
other|Other

Works (except key is shown on view node):

stuff|Stuff
things|Things
more|More
other|Other
one|Something
two|Another Thing
three|Yet Another
other|Other

Non-integer numbers seemed to also work the same as the above 2 examples.

1.25|Something
5.12|Another Thing
45.4|Yet Another
other|Other

I don't know whether integers as keys something you want to fix programmatically or just document as something that you shouldn't do. I personally like to use integers for my keys, because it's 1-2 characters so less to store and has the least potential for confusion when someone decides to change the verbiage on the value later. The fix might be as simple as forcibly type-casting the key to a string or it might be more involved.

cwithout’s picture

Regarding required fields, I have a question. I'm getting the field always populating a value rather than having a 'none' or 'select one' option. I even tried entering "|select one" as an option, hoping it would see that as empty and force the user to make a selection. That didn't happen.

Is there currently functionality to allow for a required field not to have a value pre-selected? That's something I was hoping for, since (in my opinion) pre-populated required fields are unusable. They result in too much erroneous data. If not, I could still use this as an optional field that's checked via custom jQuery, but that would be nice to have.

Thanks for you work!

mradcliffe’s picture

Oh, no, the required option negates the -none- option. That should be core behavior too, I think.

cwithout’s picture

Nope. I believe that was Drupal 6 core's behavior.

Drupal 7 required select lists now have "- Select a value -" as the default (unless another default is selected when the field is configured). I just verified on my D7 test environment.

mradcliffe’s picture

Status: Needs work » Fixed

Finally brought in default values more in line with core values. _none/- None - and ''/- Select a value -.

2ab79c941d5b10ff040a6516ba0268d15a38810a

jcarlson34’s picture

Thanks for all the hard work you two. Can't wait to test this out.

Status: Fixed » Closed (fixed)

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