how to use multiselect in own module

Arnold Schw. - June 14, 2008 - 12:13
Project:Multiselect
Version:5.x-1.2
Component:Documentation
Category:support request
Priority:normal
Assigned:Unassigned
Status:closed
Description

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 ?

#1

jklsemicolon - June 16, 2008 - 00:55

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...

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.

#2

JordiTR - June 17, 2008 - 18:24

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

#3

Arnold Schw. - June 17, 2008 - 19:27
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 !

#4

jklsemicolon - June 18, 2008 - 16:36
Title:how to use multiselect in own module -Solved for me.....» how to use multiselect in own module

Thanks for the code snippet Arnold.

#5

Arnold Schw. - August 11, 2008 - 18:35
Status:active» closed

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

 
 

Drupal is a registered trademark of Dries Buytaert.