Pre-populate entity reference with radios widget

Created on 22 July 2020, over 4 years ago
Updated 13 March 2025, 29 days ago

Hello,

I was trying to pre-populate a (taxonomy term) entity reference field that uses a radio widget and took me a few hours of debugging, so I'm posting my solution here.

1) You need to implement the hook_prepopulate_whitelist_alter() and whitelist both the radios and the radio element type. If you only whitelist the radios, which is the obvious solution, it will not work.

2) The query string for your field should be: edit[field_name][widget][123]=123, where "field_name" is the machine name of your field, and 123 is the id of the referenced entity (in my case: the term id).

🐛 Bug report
Status

Needs review

Version

2.0

Component

Documentation

Created by

🇳🇴Norway efpapado

Live updates comments and jobs are added and updated live.
Sign in to follow issues

Comments & Activities

Not all content is available!

It's likely this issue predates Contrib.social: some issue and comment data are missing.

  • 🇨🇦Canada joelpittet Vancouver

    It looks like something changed the useage needs a value of 'true' now
    prepopulate/src/Populate.php:137

            case 'checkbox':
              $form['#checked'] = $value === 'true';
    

    So that would be
    edit[field_name][widget][123]=true

Production build 0.71.5 2024