As of 28th November 2014, the video subscription has been shutdown. All videos are available for FREE to watch.

Using Multiselect Module in Drupal 7

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

Submitted by tine on

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

Submitted by ivan on

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.

Submitted by David Blankman on

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

Submitted by ivan on

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

ivan's picture

Submitted by ivan on

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)

Submitted by Rahul on

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

Submitted by Oemer on

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!

Submitted by Simon Carr on

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?

Add new comment