The Select (or other) module provides a Forms API element, and integrates it with the Fields API. However, developers and coders can make use of this element in a custom form for any purpose. This page outlines the developer usage and available attributes.

Example developer usage in Forms API

<?php
$form['my_field'] = array(
  '#type' => 'select_or_other_select',
  '#title' => t('Choose an option'),
  '#default_value' => array('value_1'),
  '#options' => array(
    'value_1' => t('One'),
    'value_2' => t('Two'),
    'value_3' => t('Three'),
  ),
  '#other_option' => t('Other (please type a value)'),   // Text to show as 'other' option (optional, defaults to "- Other -")
  '#required' => TRUE,
  '#multiple' => TRUE,
  '#other_unknown_defaults' => 'other', // If the #default_value is not a valid choice in #options, what should we do? Possible values 'append', 'ignore', 'other'  (if 'other' specified you can also override #other_delimiter).
  '#other_delimiter' => ', ', // Delimiter string to delimit multiple 'other' values into the 'other' textfield.  If this is FALSE only the last value will be used.
  '#select_type' => 'select', // Defaults to 'select'.  Can also be 'radios' or 'checkboxes'.
);
?>

The #properties you give the element that are recognised as FAPI select properties will be automatically assigned to the Select element. Also any #properties assigned as #select_property or #other_property will be passed down to the corresponding form element. For example, to use ajax with the select_or_other element, use the property #select_ajax or #other_ajax, instead of simply #ajax.

Note that in the form validate/submit function, $form_state->getValue('my_field') always returns an array (which is always a one-element array if #multiple is FALSE), unlike regular 'select' type elements.

Drupal 7 example

<?php
$form['my_field'] = array(
  '#type' => 'select_or_other',
  '#title' => t('Choose an option'),
  '#default_value' => array('value_1'),
  '#options' => array(
    'value_1' => t('One'),
    'value_2' => t('Two'),
    'value_3' => t('Three'),
  ),
  '#other' => t('Other (please type a value)'),   // Text to show as 'other' option
  '#required' => TRUE,
  '#multiple' => TRUE,
  '#other_unknown_defaults' => 'other', // If the #default_value is not a valid choice in #options, what should we do? Possible values 'append', 'ignore', 'other'  (if 'other' specified you can also override #other_delimiter).
  '#other_delimiter' => ', ', // Delimiter string to delimit multiple 'other' values into the 'other' textfield.  If this is FALSE only the last value will be used.
  '#select_type' => 'select', // Defaults to 'select'.  Can also be 'radios' or 'checkboxes'.
);
?>

Comments

gregoryshearer’s picture

I did not see this mentioned anywhere but this is a little bit of CSS that will insert a line of text before/above the other text field when it appears.

#edit-submitted-stateprovincecode-other-wrapper:before {
content: "Please enter the State that is not on the list.";
font-weight:bold;
}

Of course you need to change the field name to whatever you are using.

carl.brown’s picture

The CSS :before selector is unsupported in IE7 and below, with only partial support in IE8. So use this with caution. It would be better to use a hook to add this content in instead.

danielb’s picture

The way I do it is to use either #other_prefix or #other_field_prefix. Simple.

airspoon’s picture

I'm some-what new to Drupal and am familiar with PHP, but just barely. With that being said, can I cut and past the above code into the "allowed values PHP code" on the "field settings" page for it to work? Is that how I can get this module to work, or is it something else?

Thanks.

danielb’s picture

No. This documentation is for module developers. If using with fields use the 'text' or numeric field types and choose Select (or other) to be the widget.

oranges13’s picture

Very informative. Thank you for thinking of developers :)

mortona2k’s picture

If other is selected along with another option, is this counted as valid, invalid, or is it configurable? I need a multiple checkbox widget that allows me to collect at least one selection, including other. The ability to write in that other is icing on the cake. Does this module work for me?

The other Andrew Morton

danielb’s picture

Yes it should. Think of the whole thing like a textfield where anything goes, except there are a few easy select options to help users out or nudge them in the right direction.

laura s’s picture

I assume the "other" value does not end up adding the entry to the vocabulary as a new term, but it might be worth clarifying how "other" value is handled.

_____ ____ ___ __ _ _
Laura Scott :: design » blog » tweet

ptmkenny’s picture

On the field edit page, the "Other value as default value" allows you to select how other will be handled:

  • Add the values to the other textfield-> store the value but don't change the list
  • Append the values to the current list-> store the value and add to the list attached to the node/user/entity
  • Append the values to the available options-> store the value and add the value to the list so that it can be selected on other nodes/users/entities
  • Ignore the values-> do not store the value
ThaRaven403’s picture

Hi,

I'm currently using this element as a select dropdown. Options are filled with a key/value array, with the key being the ID of the list items (which I'll use to submit). But when something is entered in the "other" field instead, I'll be submitting through another method to send the entered text.

So my question is, is there was a way to identify if a value was selected or if something was entered in the "other" field? I could check that if the value is a long then it is an ID, otherwise it's not, but then again someone could enter numbers in the "other" field, and I don't think anyways that it's the best method to verify this...

Thanks in advance,
Bruno

adaddinsane’s picture

#other_element_validate => array('my_other_element_validation'),

Steve Turnbull, D8 developer

Like Science Fiction, Fantasy or Steampunk stories? Check out https://taupress.com/book-list

bkirkendall’s picture

I just installed the Select or Other module on my site and implemented it in a custom module of my own. However, the form where I use it depends on the use of #ajax to dynamically trigger changes to other fields based on the values supplied for the select or other field. If #ajax is not supported directly, is there a suggested work-around? Many thanks!

ericbellot’s picture

In Drupal 8 version, the module Select Or Other provide 2 FAPI elements:

  • select_or_other_select: display a selection list
  • select_or_other_buttons: display radios buttons or checkboxes depending of required parameter.

Other settings seem not be changed.

dafeder’s picture

If you are updating a field defined as select_or_other via Services' REST API, this module will lead to a slightly unexpected schema for field update. To switch select values, for instance, you would need a PUT request to do something like

curl -X PUT -i -H "Content-type: application/json" http://yourdomain.com/yourendpoint/node/22.json -d '{
  "field_some_select_or_other_field": {"und": {"select": {"value": "your_key"}}}
}'
subhra44’s picture

It seems if you select Other from the select dropdown, the other text field is mandatory. How make the field non mandatory without modifying the validate function in module?

bdone’s picture

if you need to implement the select_or_other_taxonomy module, included as of 7.x-3.0-alpha2, add the non-forms API property named "#field_widget", as shown in this example:

  $form['my_term_reference_field'] = array(
    '#type' => 'select_or_other',
    '#title' => t('Select your terms.'),
    '#required' => TRUE,
    '#multiple' => TRUE,
    '#select_type' => 'checkboxes',
    '#field_widget' => 'select_or_other_taxonomy',
    '#other_unknown_defaults' => 'other',
    '#other_delimiter' => FALSE,
    '#options' => i18n_taxonomy_allowed_values(field_info_field('your_term_field_machine_name_here')),
  );
lucasrossi’s picture

Type should be select_or_other_select instead of select_or_other.
With the provided type from the example, the field wasn't rendering.

togbonna’s picture

Can the API of this module be used in BaseFieldDefinition? The following does not work: (Edited: I gave the wrong code sample. I have corrected it with the right one)

$fields['visitor_type'] = BaseFieldDefinition::create('list_string')
      ->setLabel(t('Type'))
      ->setDescription(t('Visitor type.'))
      ->setSettings([
        'allowed_values' => [
          'value_a' => 'Value A',
          'value_b' => 'Value B',
          'value_c' => 'Value B',
        ]
      ])
      ->setDefaultValue('')
      ->setDisplayOptions('view', [
        'label' => 'above',
        'type' => 'list_default',
        'weight' => -46,
      ])
      ->setDisplayOptions('form', [
        'type' => 'select_or_other_select',
        'settings' => [
          '#other' => t('Other (please type a value)'),
          '#other_unknown_defaults' => 'other',
        ],
        'third_party_settings' => [
          '#other' => t('Other (please type a value)'),
          '#other_unknown_defaults' => 'other',
        ],
        'weight' => -46,
      ])
      ->setDisplayConfigurable('form', TRUE)
      ->setDisplayConfigurable('view', TRUE);

Any ideas on how to fix this?

www.icelark.com
Think.Act.Be
@togbonna
@icelark

togbonna’s picture

Never mind, I was able to resolve it with the following changes to my code:

      ->setDisplayOptions('form', [
       'type' => 'select_or_other_list',
        'settings' => [
          'other' => t('Other (please type a value)'),
          'other_unknown_defaults' => 'other',
        ],
        'weight' => -46,
      ])

Note the change of the setDisplayOptions form type from 'select_or_other_select' to 'select_or_other_list'. That was my main mistake.

www.icelark.com
Think.Act.Be
@togbonna
@icelark