Hi,

I would like to use multilselect in my own module.
However, it doesn't show up.
Can you please add some basic documentation what the module is for, how it can be used, with some example code snippets ?
Currently I am trying to use it (without success) in hook_form() this way:

        $form['gnid'] = array(
                '#type' => 'multiselect',
                '#title' => t('Group'),
                '#default_value' => $subscribedGroupsArray,
                '#options' =>  $group_array,
                '#description' => t('Please choose an option.'),
                '#weight'      => 6,
                '#rows'        => 10,
                '#multiple'    => TRUE
        );

What am I doing wrong ?

Comments

michael_kirk’s picture

Hi Arnold,

The multiselect widget is used in conjunction with the CCK nodereference field. It is not part of the forms API, so you cannot specify it as a '#type' in your form array. As I understand it, the only types you can specify within your form element are listed here (drupal 5) : http://api.drupal.org/api/file/developer/topics/forms_api_reference.html/5

To use it, add a new field of type nodereference, then select multiselect as your widget. If you don't know how to do that, take a look at the cck handbook: http://drupal.org/node/101723

If you want to use it in your module, it would have to be a cck module.

That said, I'm not sure how one extends the form API. I would be interested in making this widget more generic and useful. I'll take a look.

jorditr’s picture

It would be great if it was a general widget, so useful in many situations. BTW, any plans for a drupal 6 release?

Arnold Schw.’s picture

Title: how to use multiselect in own module » how to use multiselect in own module -Solved for me.....

Hi all,

I made a workaround for this issue myself by simply adding some javascript to the default 'select'. Now the select widget behaves like simpleselect, as seen here: http://labs.mininova.org/simpleselect .
The javascript file looks like this and was pasted into mymodule.js:

var list = document.getElementById('edit-gnid'); //<<< note that I hardcoded the name of the widget here.....:-(
var savedlist = new Array(list.length);

  function updatelist() {
    for (var i = list.length - 1; i >= 0; i--) {
      list[i].selected = list[i].selected ^ savedlist[i];
    }
  }
  function savelist() {
    for (var i = list.length - 1; i >= 0; i--) {
      savedlist[i] = list[i].selected;
    }
  }

The javascript file is added to my custom module by drupal_add_js(drupal_get_path('module', 'mymodule').'/mymodule.js');

The form entry in my custom module is as follows:

        $form['gnid'] = array(
                '#type' => 'select',
                '#title' => t('Group'),
                '#default_value' => $subscribedGroupsArray,   // array with selected entries
                '#options' =>  $group_array,                  // array with possible entries
                '#description' => t('Please choose an option.'),
                '#weight'      => 6,
                '#rows'        => 10,
                '#multiple'    => TRUE,
                '#attributes'  => array( 'onmousedown' => 'savelist()', 'onchange' => 'updatelist()' )
        );

Note the #attributes entry, this takes care of the extra javascript actions in the html-select-widget.
And this simply works !

michael_kirk’s picture

Title: how to use multiselect in own module -Solved for me..... » how to use multiselect in own module

Thanks for the code snippet Arnold.

Arnold Schw.’s picture

Status: Active » Closed (fixed)

This will not be fixed here, so I'll close this call

desunit’s picture

I had the same requirements - I wanted to reuse that control in my custom form. After reviewing the multiselect control I've found that you could reuse most of the code and with some tweaking you could integrate it quite easily. First of all define $element:

  $element = array(
  	'#field_name' => 'field_business_county',
  	'#columns' => array('value'),
  	'module' => 'YOUR_MODULE'   // set here you current module name 
  );

  $form['#field_info']['field_business_county'] = $element;

Then define a multiselect values function:

function YOUR_MODULE_allowed_values($field)
{
	return array('field' => 'value', ...);
}

Invoke multiselect_select_process function and assign the result to your form:

  require_once drupal_get_path('module', 'multiselect') . '/multiselect.module';
  $element = multiselect_select_process($element, NULL, $form_state, $form);
  $form['county'] = $element['value'];
mcfroggie’s picture

Version: 5.x-1.2 » 6.x-1.x-dev
Priority: Normal » Critical
Status: Closed (fixed) » Active

Hi!

I am using Drupal 6 and have a problem that would require the use of Multiselect in my own module. Since I am quite new to drupal, I would very much appreciate if someone could point me to the right direction with a chunk of code that I can test.

attheshow’s picture

Priority: Critical » Normal
Status: Active » Closed (fixed)

The snippet in comment 6 appears to be for Drupal 6 actually.

kolade.ige’s picture

Issue summary: View changes

Thanks Arnold, jQuery did if for me too.