AnswerBun.com

How to pre-select items in a select with chosen

Drupal Answers Asked by Jav on December 3, 2020

How do you load a select with items preselected when the chosen module is installed/enabled?

I’m doing the following:

$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,
  );

But the select shows up empty. Clicking it reveals the items and I can select them, but I need the chosen select to show up with the items selected. How’s this possible?

One Answer

Your select item 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 int).

adding following line should work

'#multiple' => true,

Answered by Suranga Panagamuwa Gamage on December 3, 2020

Add your own answers!

Related Questions

Node is not created, but nid is returned

2  Asked on December 26, 2021 by ziobudda

   

EntityDrupalWrapper->value() causes infinite recursion on user login?

1  Asked on December 24, 2021 by tigertrussell

   

Feed import breaks due to ajax error

1  Asked on December 24, 2021 by kenpeter

 

Multi site Image cache issue

0  Asked on December 24, 2021 by user99683

   

How to make relationships between users

1  Asked on December 22, 2021

   

Need CKEditor to encode special characters

1  Asked on December 22, 2021 by bemarlan

   

Checkboxes in a simple form are not displayed

1  Asked on December 22, 2021 by sebish

   

How to change scheduler date format in *_node_form

1  Asked on December 21, 2021 by oskar-calvo

   

hiding taxonomy terms on term page

1  Asked on December 21, 2021 by user3027413

   

TypeError on “drush config-export”

1  Asked on December 21, 2021

     

“Broken pipe” between Nginx and PHP-FPM

1  Asked on December 21, 2021 by stephen-winnall

 

Restrict delete operation on a node referenced by another node

2  Asked on December 19, 2021 by user48552

   

How to perform views date calculations using global math?

2  Asked on December 19, 2021 by tinohuda

   

Get menu link siblings

4  Asked on December 19, 2021 by erin-mclaughlin

 

Ask a Question

Get help from others!

© 2022 AnswerBun.com. All rights reserved. Sites we Love: PCI Database, MenuIva, UKBizDB, Menu Kuliner, Sharing RPP, SolveDir