I’m doing a lot of work with dynamic population for gravity forms fields using the “Advanced > Allow field to be populated dynamically” method and entering a parameter name that I am then setting the value of by function.
I’m trying to do the same thing with a dropdown field, because I am using the same field across multiple forms, and the method that involves referencing each form by id is getting kind of nuts (plus inconvenient when new forms are added or old ones are deleted, or there is a server move).
Nothing in the gravity forms docs I can find seems to directly address how to do this, so I’ve pieced together the following from docs on how to format data for the select + my working code for other text fields where I am populating by parameter.
So far, nothing seems to be working to populate the form select. I’ve simplified as much as possible, so code is as follows (and I am including “userlist” as my parameter name):
Hi @lizfulghum - I think I follow your challenge’s description…
And I hugely can relate, as I was a lead dev on a similar project.
I decided to document and publish a plugin of the core of the enhancements to GF we made. Perhaps this new (free) plugin can make your life a bit easier ?
This “Shared Fields” plugin isn’t published yet, as I have remaining the two Guides to complete the necessary documentation (this is a dev’s most unloved chore lol).
Take a gander at my intro and about pages, and LMK if this sounds worth checking out, as I’m still seeking some beta test help
Hey James, this is certainly an interesting plugin, and I’ll absolutely take a look to see if it’s something that would be useful.
In this particular instance, I don’t think it actually addresses my particular needs.
Gravity forms seems to have a couple of ways that allow you to populate data using the Advanced > Allow field to be populated dynamically option and then specifying a parameter.
I have some database queries that I need to run in order to populate a field dynamically (it must be run each time the form is displayed, because the information may change), so I use the hook method to attach my custom function.
I’m able to do this all day long with a text field, but I guess I’m not formatting the information correctly for a select field, because it’s not working for that.
I reviewed my source code for the “select” type (drop down) field handling. While I’m not using hook gfrom_field_value as you are, I am using pre_render, which effectively requires the same handling.
In your code you are not setting the key ‘isSelected’ to ‘yes’
From my working code for select use case:
($tmpVal contains the target item that I want set to selected)
case MFSFS_FIELD_TYPE_DROP_DOWN: // (select)
foreach ($field->choices as &$choAry) {
if (! empty($choAry['value']) && $choAry['value'] == $tmpVal) {
$choAry['isSelected'] = 'yes';
if ($LOC_debug > 0) {
GFCommon::log_debug("mfsfs_load_fields(1): case $fieldType - choosen item: " . $choAry['text'] . " for value: $tmpVal");
}
break;
}
}
break;
Bonus tip: Add the missing field object to your inbound function args, enable log debugging and insert a log_debug() diagnostic to dump the field array; you will see it’s a bit different than your code is expecting for the format of your returned array.