PLEASE NOTE: These forums are no longer utilized and are provided as an archive for informational purposes only. All support issues will be handled via email using our support ticket system. For more detailed information on this change, please see this blog post.

Populating Drop Down with value from ACF field

  1. I'm trying to figure out the code for the gform_pre_render that will allow me to pull the data stored in my ACF custom field and populate a drop down list.

    I'm not trying to create posts or update the database, just an email contact form. I have a multi-selection field in ACF called "charter_types" that works with a custom post type. I need to add a contact form on the post type, and have the Charter Type dropdown field populated with the multiple values entered on each post.

    I've tried various code, but just can't quite get this figured out. Example if this will help.

    Post 1: field "charter_type" has 2 values stored, "offshore" and "fly".
    Post 2: field "charter_type" has 3 values store, "offshore", "fly" and "tarpon".

    When displaying gform 1 on Post 1, the Drop-Down field labeled Select Charter Type needs to display only "offshore" and "fly".

    When displaying gform 1 on Post 2, the Drop-Down field needs to display "offshore", "fly" and "tarpon".

    This code seems to be the closest, but I'm getting an invalid argument supplied on the for each.

    add_filter("gform_pre_render", "populate_dropdown");
    
    //Note: when changing drop down values, we also need to use the gform_admin_pre_render so that the right values are displayed when editing the entry.
    add_filter("gform_admin_pre_render", "populate_dropdown");
    
    function populate_dropdown($form){
    
        //only populating drop down for form id 1
        if($form["id"] != 1)
           return $form;
    
        //Reading values from custom field charter_types
        $chartertypes = get_field('charter_type');
    
        //Creating drop down item array.
        $items = array();
    
        //Adding initial blank value.
        $items[] = array("text" => "", "value" => "");
    
        //Adding charter type values to the items array
        foreach($chartertypes as $chartertype)
            $items[] = array("value" => $chartertype, "text" => $chartype);
    
        //Adding items to field id 4. Replace 4 with your actual field id. You can get the field id by looking at the input name in the markup.
        foreach($form["fields"] as &$field)
            if($field["id"] == 4){
                $field["choices"] = $items;
            }
    
        return $form;

    I think this might be related to another post talking about how ACF stores these values serialized in the DB.

    Is there any solution I can use here?

    Posted 11 years ago on Wednesday May 30, 2012 | Permalink
  2. David Peralty

    You might want to talk to the plugin maker of ACF because your code, in terms of putting data into a drop down is correct from the Gravity Forms side if the values are stored in a normal array.

    Posted 11 years ago on Wednesday May 30, 2012 | Permalink
  3. Thanks David, although not what I wanted to hear 'cause it doesn't work :-(

    Okay, I'm off to talk to Elliot and see if we can figure this out.

    Posted 11 years ago on Thursday May 31, 2012 | Permalink
  4. Okay, I solved it! There is probably a much cleaner way of doing this, but it's working for me.
    I created a new array and used IF statements to check the ACF field array to see what values are set for that post, and used array_push to build this new array with only the values that should be there.

    Then, I use this new array as the base for the foreach statement that builds the choices items.

    Here's my code in case anyone else is trying to do something similar with Gravity Forms and ACF.

    add_filter("gform_pre_render", "populate_dropdown");
    
    //Note: when changing drop down values, we also need to use the gform_admin_pre_render so that the right values are displayed when editing the entry.
    add_filter("gform_admin_pre_render", "populate_dropdown");
    
    function populate_dropdown($form){
    
        //only populating drop down for form id 1 - change to be your form id
        if($form["id"] != 1)
           return $form;
    
        //Create a new array from the values in the acf field.
        //Using if command to only grab the values that exist for that post, repeat as needed
        $chartertypes = array();
    
        if(in_array('offshore', get_field('charter_types') ))
    	array_push($chartertypes, "Deep Sea Offshore");
    
        if(in_array('flat', get_field('charter_types') ))
    	array_push($chartertypes, "Backcountry Flats");
    
        //Creating drop down item array.
        $items = array();
    
        //Adding initial blank value.
        $items[] = array("text" => "Select Charter Type", "value" => "");
    
        //Adding charter type values to the items array
        foreach($chartertypes as $chartertype)
           $items[] = array("value" => $chartertype, "text" => $chartertype);
    
        //Adding items to field id 4. Replace 4 with your actual field id. You can get the field id by looking at the input name in the markup.
        foreach($form["fields"] as &$field)
            if($field["id"] == 4){
                $field["choices"] = $items;
            }
    
        return $form;
        }
    Posted 11 years ago on Thursday May 31, 2012 | Permalink
  5. David Peralty

    Glad you were able to figure it out. Thanks for posting your solution.

    Posted 11 years ago on Thursday May 31, 2012 | Permalink

This topic has been resolved and has been closed to new replies.