Provide a setting to let the end-user decide whether to Add 'Other' value to the list of choices for this field. e.g. for known recurring usage.

That would be far better than to have a content type with a select_or_other field set for all entries to be added to the list, as some options may be beneficial to be added for reuse while others are not and would otherwise unnecessarily fill up the list and make it harder to find entries.

e.g. additional checkbox for "let the user decide"

UPDATE: I implemented the below which achieves the above and I think is a better alternative, especially as as one has to make a conscious decision if a custom value is required and whether it should be added or not. Otherwise the user may be tempted to create a custom value every time when it could have been added. Also avoids typos and eliminates copy/paste.

Comments

markusd1984 created an issue. See original summary.

markusd1984’s picture

I managed to implement my self but not yet savvy enough to build a patch, in case somebody wants to test and create one, much appreciated. Tested this against 7.x-3.0-alpha with all possible scenarios all working as expected.

 Make add other value to list optional

Value is added to list if either first option is selected or both (regardless of checkbox selected) as well as if only second option is enabled with the optional individual checkbox ticked.

Checkbox will only be displayed if only "Make adding Other value optional" is enabled.
Meaning if both are enabled including "Add 'Other' values" then it will be hidden instead since it won't be required (first option overrules).
This looks same as without this customisation, that's why i originally included a reference in the Other field title that "will also be added to the list".

Other textfield no checkbox when primary enabled or optional not enabled

updated screenshot with updated Other field title as will used the checkbox in most cases now. :)
Other textfield with checkbox when optional is enabled - even when primary not enabled

To do: Possibly using a select list instead of checkbox for the type of widget setting, to select between "Never / Always / Optional" ?

CODE:
In select_or_other.module
1) add below "$element['other'] = array( ..... );"

//patch for optional individual checkbox of adding value to other list of choices https://www.drupal.org/project/select_or_other/issues/3155490
// condition to only show checkbox if only optional adding is enabled and "Add 'Other'" is not enabled
//in content type select_or_other field widget settings
$instance = field_widget_instance($element, $form_state);
if (isset($instance['widget']['settings']['other_unknown_new_option_optional']) && $instance['widget']['settings']['other_unknown_new_option_optional']
&& !$instance['widget']['settings']['other_unknown_new_option']){
 $element['other_unknown_new_option_checkbox'] = array(
    '#type' => 'checkbox',
    '#title' => t("Add also to the list of choices."),
    '#default_value' => $settings['other_unknown_new_option_checkbox'],
    '#default_value' => FALSE,
  '#weight' => 30,
'#attributes' => array('class' => array('select-or-other-other-checkbox')),
  );
}

in select_or_other.field_widget.inc

2) add in function select_or_other_field_widget_info() {
$settings = array(

'other_unknown_new_option_optional' => FALSE,
'other_unknown_new_option_checkbox' => FALSE,

3) add under $form['other_unknown_new_option'] = array( .... );

$form['other_unknown_new_option_optional'] = array(
    '#type' => 'checkbox',
    '#title' => t("Make adding 'Other' value to the list of choices for this field optional via checkbox next to other textfield."),
 '#default_value' => $settings['other_unknown_new_option_optional'],
  );

4) change condition below
// Add values to available options is configured to do so.
$instance = field_widget_instance($element, $form_state);

//  if (isset($instance['widget']['settings']['other_unknown_new_option']) && $instance['widget']['settings']['other_unknown_new_option']) {
  if (isset($instance['widget']['settings']['other_unknown_new_option']) && $instance['widget']['settings']['other_unknown_new_option']
|| isset($element['other_unknown_new_option_checkbox']['#value']) && $element['other_unknown_new_option_checkbox']['#value']) {

and the select_or_other.js

5) Below the code

/**
 * @file select_or_other.js
 */

(function ($) {

  function select_or_other_check_and_show(ele, page_init) {
    var speed;
    if (page_init) {
      speed = 0;
    }
    else {
//       speed = 200;
speed = 0; //display without delay to avoid visually moving to the right due to being centered via css for registration titles
      ele = jQuery(ele).parents(".select-or-other")[0];
    }
    var $other_element = jQuery(ele).find(".select-or-other-other").parents("div.form-item").first();
    var $other_input = $other_element.find('input');

//checkbox
var $other_element2 = jQuery(ele).find(".select-or-other-other-checkbox").parents("div.form-item").first();
var $other_input2 = $other_element2.find('input');
//

    if (jQuery(ele).find(".select-or-other-select option:selected[value=select_or_other], .select-or-other-select:checked[value=select_or_other]").length) {
      $.fn.prop ? $other_input.prop('required', true) : $other_input.attr('required', true)
      $other_element.show(speed, function() {
        if(!page_init) {
          $(this).find(".select-or-other-other").focus();
        }
      });
    }

    else {
      $other_element.hide(speed);
      $.fn.prop ? $other_input.prop('required', false) : $other_input.removeAttr('required');

      if (page_init)
      {
        // Special case, when the page is loaded, also apply 'display: none' in case it is
        // nested inside an element also hidden by jquery - such as a collapsed fieldset.
        jQuery(ele).find(".select-or-other-other").parents("div.form-item").first().css("display", "none");
      }
    }

//checkbox
if (jQuery(ele).find(".select-or-other-select option:selected[value=select_or_other], .select-or-other-select:checked[value=select_or_other]").length) {
      $other_element2.show(speed, function() {
        if(!page_init) {
          $(this).find(".select-or-other-other-checkbox").focus();
       }
     });
}

    else {
      $other_element2.hide(speed);
      if (page_init)
      {
        // Special case, when the page is loaded, also apply 'display: none' in case it is
        // nested inside an element also hidden by jquery - such as a collapsed fieldset.
        jQuery(ele).find(".select-or-other-checkbox").parents("div.form-item").first().css("display", "none");
      }
    }

}
//

  /**
   * The Drupal behaviors for the Select (or other) field.
   */
  Drupal.behaviors.select_or_other = {
    attach: function(context) {
      jQuery(".select-or-other:not('.select-or-other-processed')", context)
        .addClass('select-or-other-processed')
        .each(function () {
          select_or_other_check_and_show(this, true);
        });
      jQuery(".select-or-other-select", context)
        .not("select")
        .click(function () {
          select_or_other_check_and_show(this, false);
        });
      jQuery("select.select-or-other-select", context)
        .change(function () {
          select_or_other_check_and_show(this, false);
        });
    }
  };

})(jQuery);
markusd1984’s picture

Issue summary: View changes
markusd1984’s picture

Aligned the label next to the checkbox with some CSS.

 optional select_or_other checkbox side by side

CSS

/*select_or_other Other checkbox aligned next to label*/
.page-node-edit .form-item-title-field-und-other-unknown-new-option-checkbox, .page-node-add .form-item-title-field-und-other-unknown-new-option-checkbox input {width:30px;}
.page-node-edit .form-item-title-field-und-other-unknown-new-option-checkbox, .page-node-add .form-item-title-field-und-other-unknown-new-option-checkbox label {display:inline-block !important;}

FYI - I also added Other option always on top but between select and options in lists! and added some CSS + Chosen module for select list to highlight the Other option.

Other highlighted by CSS + Chosen

ivnish’s picture

Component: Field widget (non-specific or listed) » Code
Status: Needs work » Closed (outdated)

Automatically closed because Drupal 7 security and bugfix support has ended as of 5 January 2025. If the issue verifiably applies to later versions, please reopen with details and update the version.