Search code examples
radio-buttoncategoriesdisplaygravityforms

Gravityforms - Add category mid-radio-button field


I am populating Gravityforms fields using the functions.php from my template and it works like a charm, but I have one field that is ... a challenge. I have been able to populate the choices from my database just fine, but with the functions.php I cannot control the content of the display area of the field so that I can, for example, add a title or header for each category. Is there a way to programattically adjust the display here's an example of what im hoping to accomplish

RadioButton Choice (Field ID 21)
    Dark Colors (category title)
        maroon (choice 1)
        navy blue (choice 2)
        black (Choice 3)
    Standard Colors (Category Title)
        Red (choice 4)
        blue (choice 5)
        gray (Choice 6)
    Light Colors )Category Title)
        pink (choice 7)
        sky blue (choice 8)
        white (Choice 9)

I am just looking for a way to add the category title between the choices. My DB Query has the categories as part of the response, but the only option I have to populate choices to to feed an array.

I have seen where I can add additional Gravityform fields and have them controlled by the same "single select" radio button option, but the categories involved change based on the DB query I call to dynamically populate the choices and could range from 1 category to 10, which will not have correlating fields in the form itself, as this is all under a single radio-button field.

Any thoughts would be appreciated.


Solution

  • I was able to use the link I posted in my comment to provide a solution. As part of my "choices"

    here is the function for the Pre-Render to populate choices. In this case I am populating a radio-button field with product images instead of default radio buttons

    add_filter('gform_pre_render_1', 'populate_choices');
    function populate_choices($form) {
        global $wpdb;
    
        // Now to get a list of the products I want to include on this radio-button
        $dataQ = "
            SELECT category, product_id, product_name 
            FROM product_table
            WHERE product_type = 'whatever I am looking for'
            ORDER BY category, product_name ASC
        ";
        $dataR = $wpdb->get_results($dataQ);
        $dataList = array();
    
        // get current protocol to use in Product Images
        $protocol = ((!empty($_SERVER['HTTPS']) && $_SERVER['HTTPS'] != 'off') || $_SERVER['SERVER_PORT'] == 443) ? "https://" : "http://";
    
        // Rebuild URL for use with product image graphics
        $productImageUrl = $protocol.$_SERVER['HTTP_HOST'].'/wp-content/product_images/';
    
        // Generate list of choices for the radio-button, along with category headers
        foreach ($form['fields'] as $field) {
            if ($field->id == '32') { // 32 My specific radio-button field
                $category = "";
                foreach ($dataR as $data) {
                    if ($category != $data->category) {
                        // If I hit a new category, add the "header" item to the "choices" list with a unique search item to identify it, in this case "###"
                        $category = $data->category;
                        $dataList[] = array(
                            'text' => '#'.$category."#",
                            'value' => ''
                        );
                    }
                    $productImage = str_replace(" ","",strtolower($data->product_name)).".jpg";
                    $productID = $data->product_id;
                    $dataList[] = array(
                        'text' => $data->product_name,
                        'value' => $productID,
                        'isSelected' => false,
                        'price' => '0.00',
                        'imageChoices_image' => $productImagehUrl.$productImage
                    );
                }
                $field->choices = $dataList;
            }
        }
    }
    

    I then added a specific field modifier to update the "#category#" choice elements with html to make the category names show up.

    // numbers after filter = "1" for form ID 1, "32" for field ID 32
    add_filter('gform_field_choice_markup_pre_render_1_32', function( $choice_markup, $choice) {
        if ( strpos( $choice['text'], '#' ) === 0 ) {
            $categoryName = str_replace("#","",$choice['text']);
            $choice_markup = '
            <li style="width:100%;text-align:left;">
                <span style="color:#000000;font-weight:bold;font-size:18px;">
                    <br>
                    '.$categoryName.'
                </span>
            </li>
           ';
        }
        return $choice_markup;
    }, 10, 2);