The standard multiple select list can be difficult to use, if your users are not very technical. Most of the time they won't know that you have to hold down command or ctrl key if you want to select multiple options. Also, once you have more than 10 options, the select list becomes very difficult to use. One wrong move and you can loose all the selected options.

The Multiselect module defines a widget that allows users to move selected items from one box to another. Visually this helps users to see which items have been selected. The widget can be used on a bunch of field types and it can be implemented as a form element using FAPI.

If you want to use Multiselect on an entity reference field, then use the 7.x-1.x-dev version.

Getting Started

Download and install the Multiselect module.

Configure Multiselect on Term Reference Field

In this section, we'll use the Multiselect widget on the Tags field that is created when you install Drupal using the standard installation profile.

1. Structure -> "Content types" and click on "manage fields" within the Article row.

2. Click on Edit within the Tags row and then click on the "Widget tab" in the top right corner.

3. Select Multiselect from the "Widget type" select list and click on Continue.

Fig 1.0

4. Go to Content -> "Add content" and click on Article. Now the Tags widget has changed to a Multiselect widget.

Fig 1.1

Form API Multiselect Widget

You can also use the Multiselect widget on custom forms by defining '#type' => 'multiselect' on a form element. Thankfully, the module ships with "Multiselect FAPI Example" module that shows you a working example.

1. Enable the "Multiselect FAPI Example" module.

2. Go to /multiselect_fapi_example and you should see the example form.

Fig 1.2

Open the multiselect_fapi_example.module file and go to multiselect_fapi_example_display(). In this function you'll see the multiselector form element.

$form['multiselector'] = array(
  '#type' => 'multiselect',
  '#title' => t('Here is the multiselect'),
  '#options' => array(
    'title' => t('Titles only'),
    'teaser' => t('Titles plus teaser'),
    'fulltext' => t('Full text'),
    '1' => t('Option 1'),
    '2' => t('Option 2'),
  ),
  '#default_value' => _multiselect_fapi_example_options(),
  '#multiple' => TRUE,
  '#size' => 15,
  '#required' => TRUE,
);

If you have any questions, please leave a comment.

Tags: 

Ivan is the founder of Web Wash and spends most of his time consulting and writing about Drupal. He's been working with Drupal for 6 years and has successfully completed several large Drupal projects in Australia.

Connect: Twitter drupal.org LinkedIn App.net

Comments

Very good the tutorial, thanks. I have a question, how can I change the labels "avaible options" and "selected options"?
Thanks for your help.

ivan's picture

You can't change those labels via a configuration page. However, there are two ways you could do it.

The first way, is to use the String Overrides (http://drupal.org/project/stringoverrides) module. This module allows you to override text that is passed through the "t()" function. This is the least "hacky" way. :)

Another possible option is to use JavaScript to change the labels.

If you're curious, look at the _multiselect_build_widget_code() function to see the labels.

Is there a way to make the multi select widget wider in D6. We are using two fields in a view as the source for the data populating the widget, but not all of the information is visible.

ivan's picture

I haven't used the D6 version in a while, but couldn't you widen the form elements using CSS?

Hello ivan, Thanks for this great post,
Can u please tell me how to use multiselect in drupal 7 webform.

ivan's picture

I don't think there is out-of-the-box integration between Webform and Multiselect. You'll have to implement a custom webform field to use Multiselect.

I found an issue in the Webform issue queue asking the same question. (https://drupal.org/node/1984178)

i will try to do it by template preprocessor ...by d way thanks for reply :)

Ivan, thanks for the hint to use the 7.x-1.x-dev version of the module on entity reference fields.
I was ready to give up and not use the module but then found your posting. Perfect!

In both the example and the on my custom module. The select boxes are not side by side they are on top of each other and neither the Add or Remove links work. Any ideas about what I might be doing wrong?

ivan's picture

Not sure what the problem is, you should ask in the issue queue.

Add new comment