Support

Account

Forum Replies Created

  • Hello Buddies,

    It took me HOURS, and i don’t get every line of code i used, but I just coded what may make your day !

    WHAT I WANTED : I wanted to dynamically populate certain fields from a gravity form, using values from repeater subfields i had on an option page.

    So, here it is :

    add_filter("gform_pre_render_ID", "dynamic_populate");
    // Update the 'ID' to the ID of your form (You have it in URL while modifying the form). This hook happens each time your form is loaded (Pre_render).
    
    function dynamic_populate($form) {
        foreach($form['fields'] as &$field){
        //At this point, function will run for each field in my Gravity Form, but right after i add a condition, see what's next.
         
            if(strpos($field['cssClass'], 'dynamicfield') === false)
                continue;
            //Condition : each field which has the class dynamicfield (example). If it doesn't then, continue ! Don't forget to add a class to the field you want to dynamically populate in your form. PS: You can add whichever condition you want. Example : Run only on select fields, run only on select fields AND classname...
            
            global $wpdb;
            $rows = $wpdb->get_results($wpdb->prepare( 
                "
                SELECT *
                FROM $wpdb->options
                WHERE option_name LIKE 'REPEATERNAME_%%_SUBFIELDNAME'
                "
            ));
            //Call WP Database and SELECT all data FROM the table you want, for me it was option table as my repeater is on an option page. Then find each rows WHERE option_name value is like 'REPEATERNAME_%%_SUBFIELDNAME'. To get this, read the data naming convention part in here : http://www.advancedcustomfields.com/resources/querying-the-database-for-repeater-sub-field-values/.
    
             //If your repeater isn't on an option page, you might want to call FROM wp_postmeta WHERE meta_key LIKE 'REPEATERNAME_%%_SUBFIELDNAME'.
            
            $choices = array(array('text' => 'Choisissez une base', 'value' => ' '));
            //Default choice for my dynamic select field. You shall not use any value, so if it's required it won't validate.
            
            if( $rows ){
                foreach( $rows as $row ) {
                //If there are results, for each result, find the 'repeater row number'
                    preg_match('_([0-9]+)_', $row->option_name, $matches);
                    //Note that option_name is the name of the column from my option database, you might want to change it.
    
                    $name = 'REPEATERNAME_' . $matches[0] . '_SUBFIELDNAME';
                    $value = get_option($name);
                    //Get the subfield value from the row.
    
                    $choices[] = array('text' => $value, 'value' => $value);
                    //Create an option array for each subfield value.
                }  
            }
    
            $field['choices'] = $choices;
            //Once we've looped through all of the subfields values and created all of our options, we assign the $choices array (which has all of our new options) to the $field choices property.
    
        }
    
        return $form;
        //Display the new dynamically populated form.
    }

    I hope this is quite clear cause i’m not a PHP expert, and i’m also french so maybe my english isn’t that good.

    Tell me if it worked for you, and if you were looking after it.

    PS: Eliott, thanks for the powerful ACF Plug-in, i hope that solution will help i think it’s quite useful and powerful ! Or maybe i did a hell of a job for nothing that much interesting haha !

    Have a great day all!

    Check my sources :
    Dynamic populate Gravity Forms fields before Form pre-render => http://www.gravityhelp.com/documentation/gravity-forms/knowledge-base/tutorials/dynamically-populating-drop-down-fields/
    Querying the database for subfields values => http://www.advancedcustomfields.com/resources/querying-the-database-for-repeater-sub-field-values/

Viewing 1 post (of 1 total)