Gravity Forms联动下拉框子字段填充失败求助(PHP实现)
Fixing Your Gravity Forms Chained Selects Sub-Field Issue
Hey there! Let's work through the kinks in your sub-field code and get those dynamic features loading correctly based on the selected company.
Key Issues in Your Current Code
- Incorrect Hook Parameter Order: The
gform_chained_selects_input_choiceshook has a strict parameter order that you didn't match—this is why yourchain_valuewasn't pulling the selected parent value properly. - Mixed Return Values: You tried returning
$itemsfor all hooks, butgform_pre_render/gform_admin_pre_renderneed the modified$form, whilegform_chained_selects_input_choicesexpects filtered input choices. - No Company Filtering: Right now, you're loading all features from form 7 instead of only those linked to the selected parent company.
- Unnecessary Field Modification: For chained selects, sub-field options are handled directly via the
gform_chained_selects_input_choiceshook—you don't need to update the form's core field structure.
Corrected Code
// Handle form rendering hooks (no field changes needed here) add_filter("gform_pre_render_14", "populate_features_render"); add_filter("gform_admin_pre_render_14", "populate_features_render"); // Handle dynamic choices for the sub-field (input 5.2 on form 14) add_filter("gform_chained_selects_input_choices_14_5_2", "populate_features_choices", 10, 5); // For pre-render hooks, just return the unmodified form function populate_features_render($form) { return $form; } // Core logic to load features based on selected company function populate_features_choices($input_choices, $form_id, $field, $input_id, $chain_value) { // Only target our specific form (14) if ($form_id != 14) { return $input_choices; } // Grab the selected company from the parent chain value $selected_company = $chain_value; if (empty($selected_company)) { return $input_choices; // Return empty if no company is selected yet } // Fetch entries from form 7 (where company data is stored) $entries = GFAPI::get_entries(7); if (!is_array($entries)) { return $input_choices; } $items = array(); // Filter entries to only those matching the selected company (field 4 in form 7) foreach ($entries as $entry) { $entry_company = rgar($entry, '4'); if ($entry_company === $selected_company) { $feature = rgar($entry, '10'); // Only add non-empty features if (!empty($feature)) { $items[] = array( 'value' => $feature, 'text' => $feature, 'isSelected' => false ); } } } // Remove duplicate features (in case multiple entries have the same feature for one company) $items = array_map("unserialize", array_unique(array_map("serialize", $items))); return $items; }
What Changed & Why
- Split Hooks: We separated rendering hooks from the chained select logic to handle each correctly. The pre-render hooks just pass the form through since we don't need to modify its structure.
- Fixed Parameter Order: The
populate_features_choicesfunction uses the exact parameter order required by thegform_chained_selects_input_choiceshook, sochain_valuenow correctly gets the selected parent company. - Company-Specific Filtering: We now only pull features from entries in form 7 that match the selected company, instead of loading every feature in the system.
- Duplicate Removal: Added a quick check to eliminate duplicate features, which cleans up the dropdown if multiple entries list the same feature for a single company.
- Proper Return Values: Each hook returns the correct data type—
$formfor rendering, filtered choices for the chained select.
Quick Checks
- Double-check that field IDs in form 7 are correct: field 4 = company name, field 10 = feature. Adjust these numbers if your actual fields differ.
- If you want a default option (like "Select a feature...") when no company is selected, add it to
$itemsbefore returning when$selected_companyis empty.
内容的提问来源于stack exchange,提问作者Parkbum
相关产品推荐
相关产品推荐

