0

How do you load a select field with pre-selected items when the Chosen module is installed/enabled?

I am using the following code.

$my_list = array('item1' = 'item1', 'item2' = 'item2', 'item3' = 'item3');
$form['list_info']['my_list'] = array(
  '#title' => t('Select a list to manage:'),
  '#type' => 'select',
  '#options' => $my_list,
  '#default_value' => $my_list,
);

The select field shows up empty. Clicking it reveals the items and I can select them, but I need the field to show up with the items selected.

2
  • Set #options => array_keys(array_filter($my_list))
    – AKS
    May 22 '15 at 3:34
  • @AykeshK That will cause the actual passed values for to the submit handler to be removed by setting the array to an indexed one of the keys (array_keys) and remove empty values of wich there shouldnt even be any after array_keys (array_filter). But it won't solve the problem in any way. May 22 '15 at 9:55
0

Your select field is currently only accepting one selected option. This is why it won't allow an array as a default value. (It expects a string or an integer).

The following select field allows more than a selected option.

$form['list_info']['my_list'] = array(
  '#title' => t('Select a list to manage:'),
  '#type' => 'select',
  '#options' => $my_list,
  '#default_value' => $my_list,
  '#multiple' => TRUE,
);

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.