My Account

Forums

Forums General Select Field: Change / Add own data sources Reply To: Select Field: Change / Add own data sources

#52775
cubetech
Participant

You can do this via hook and erase the already existing contents and prefill it with our own.
Basically you can then prefill everything what you want 🙂

Example:


namespace ct_example;

class WsForm
{
    public static function Prefill($form)
    {
        $fieldId = 123;
        $matchingField = &wsf_form_get_field($form, $fieldId);
        wsf_field_rows_clear($matchingField);

        $prefillData = Helper::GetMyPrefillData(); // returns key/value pairs
        if ($prefillData) {
            foreach ($prefillData as $prefillEntryKey => $prefillEntryValue) {
                $row = (object)[
                    'default' => false, // you can create logic to preselect entries
                    'data' => [$prefillEntryValue, $prefillEntryKey] // you need to add the key/value fields to your dropdown!
                ];
                wsf_field_row_add($matchingField, $row);
            }
        }

        return $form;
    }
}
add_filter('wsf_pre_render', ['\\' . __NAMESPACE__ . '\\WsForm', 'Prefill']);