What's the best way to set a default value to a Select2 autocomplete field widget on form load (I'm using a reference view to populate options). I've tried a ton of different variations from within form_alter with no success, such as...

$form['field_name']['widget']['#select2']['id'] = 12

I can set the hidden <select> element with...

$form['field_name']['widget']['#default_value'] = 12

...but this value doesn't populate into the Select2 Widget.

I've combed through docs and issues without finding a solution. What am I missing?

1 Answer 1


With the select2 widget the #default_value' is an array so try with

 $form['field_name']['widget']['#default_value'] = [12];

It will work!

  • I appreciate the response. Unfortunately as i mentioned above, this only populates the hidden <select> element and not the actual Select2 Autocomplete field. Any other tips?!?!
    – Yuckle
    Commented Jun 14, 2021 at 3:28
  • i have tested it on select2 widget what you mentioned above is only 12 as integer not array [12] as it in my answer add [] it will work
    – berramou
    Commented Jun 14, 2021 at 10:32
  • Thank you for the reply - you are totally correct. I added some custom code previous to render the dropdown's HTML... and that messed things up. My bad ;)
    – Yuckle
    Commented Jun 14, 2021 at 16:25
  • you are welcome, i'm glad that helped
    – berramou
    Commented Jun 14, 2021 at 16:49

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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