My Account
Forums
Home › Forums › General › Select Field: Change / Add own data sources › Reply To: Select Field: Change / Add own data sources
July 1, 2021 at 6:15 am
#52775
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']);