FEATURE optimizing behaviour
All checks were successful
continuous-integration/drone/push Build is passing
All checks were successful
continuous-integration/drone/push Build is passing
This commit is contained in:
parent
746363d81a
commit
5c9476cb61
|
|
@ -1,12 +1,16 @@
|
|||
<?php
|
||||
|
||||
// Apply the filter to multiple hooks to ensure choices are available during render, validation, and submission
|
||||
add_filter('gform_pre_render', 'populate_taxonomy_terms');
|
||||
add_filter('gform_pre_validation', 'populate_taxonomy_terms');
|
||||
add_filter('gform_pre_submission_filter', 'populate_taxonomy_terms');
|
||||
add_filter('gform_admin_pre_render', 'populate_taxonomy_terms');
|
||||
|
||||
function populate_taxonomy_terms($form)
|
||||
{
|
||||
// Specify the form ID and the ID of the select field
|
||||
$form_id = 2;
|
||||
$field_id = 15;
|
||||
$field_id = 16;
|
||||
|
||||
// Check if the current form matches the specified form ID
|
||||
if ($form['id'] == $form_id) {
|
||||
|
|
@ -16,20 +20,39 @@ function populate_taxonomy_terms($form)
|
|||
'hide_empty' => false, // Include empty terms
|
||||
));
|
||||
|
||||
// Prepare choices array for the select field
|
||||
$choices = array();
|
||||
foreach ($terms as $term) {
|
||||
$choices[] = array(
|
||||
'text' => $term->name,
|
||||
'value' => $term->term_id,
|
||||
);
|
||||
}
|
||||
|
||||
// Find the select field by ID
|
||||
// Find the checkbox field by ID
|
||||
foreach ($form['fields'] as &$field) {
|
||||
if ($field->id == $field_id) {
|
||||
// Update choices for the select field
|
||||
// Prepare choices and inputs array for checkboxes
|
||||
$choices = array();
|
||||
$inputs = array();
|
||||
$index = 1;
|
||||
|
||||
foreach ($terms as $term) {
|
||||
$choices[] = array(
|
||||
'text' => $term->name,
|
||||
'value' => $term->name, // Use term name as value for display
|
||||
'isSelected' => false,
|
||||
);
|
||||
|
||||
// For checkboxes, each choice needs a corresponding input
|
||||
$inputs[] = array(
|
||||
'id' => $field->id . '.' . $index,
|
||||
'label' => $term->name,
|
||||
'name' => '',
|
||||
);
|
||||
|
||||
$index++;
|
||||
}
|
||||
|
||||
// Update choices and inputs for the checkbox field
|
||||
$field->choices = $choices;
|
||||
$field->inputs = $inputs;
|
||||
|
||||
// Ensure the field is not marked as administrative only
|
||||
$field->adminOnly = false;
|
||||
|
||||
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in New Issue
Block a user