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.

Change Custom Field / Multi-Select output (to work with Advanced Custom Fields)

  1. Hello,
    I am working on a project which uses Gravity Forms (GF) as the frontend UI and Custom Post Types enhanced by Advanced Custom Fields (ACF) to manage data backend.
    All is working very good but 1 thing, when dealing with multi-selection.

    Here are the differences between GF & ACF :
    - GF stores multi-selection as many custom meta fields with the same meta name.
    - ACF stores multi-selection as a single custom meta, with its data separated by a comma.
    Therefore, they can match together.

    Could it be possible to have GF working the same way ACF is when using Custom Fields ?
    Or maybe you could tell me where I could make a hack into GF code (not very recommended though) or maybe a new filter ?

    Best regards,
    Antoine
    Antoine

    Posted 12 years ago on Thursday March 8, 2012 | Permalink
  2. schnettker
    Member

    Hi,

    i have the same problem. Did you solve it for you?

    Posted 12 years ago on Thursday April 26, 2012 | Permalink
  3. Nope ; not solved.

    Posted 12 years ago on Thursday April 26, 2012 | Permalink
  4. schnettker
    Member

    i am realy unhappy about that situation, because a solution seems to be "easy".

    ACF stores the value in this form
    value - a:2:{i:0;s:4:"ipad";i:1;s:6:"iphone";}

    GF needs two rows an stores it like this
    value - 16
    value - 17

    The IDs are different. I Use the example from the wiki:

    function preise_cat($form){
        foreach($form['fields'] as &$field){
    
            if(strpos($field['cssClass'], 'preise') === false)
                continue;
            $categories= get_categories(array('child_of'=>'15','hide_empty'=>0));
            $choices = array();
    		$i=0;
            foreach($categories as $category){
                $choices[$i]['text'] = $category->name;
    			$choices[$i]['value'] = $category->cat_ID;
    			$i++;
            }
    
            $field['choices'] = $choices;
        }
        return $form;
    }

    Is it possible to passing the values after posting the form? Where can i put a additional function to modify the values? And why are the IDs different? The problem occurs only if i need multiple choices. Simple fields are working fine!

    Posted 12 years ago on Friday April 27, 2012 | Permalink
  5. David Peralty

    While I don't know enough about ACF to help get the data to line up like you want, I can point you to the Pre Submission Hook that will let you run custom functions after validation is complete, but before it is saved to the database or sent out as a notification.

    Have a look at:
    http://www.gravityhelp.com/documentation/page/Gform_pre_submission_filter

    Posted 12 years ago on Friday April 27, 2012 | Permalink
  6. schnettker
    Member

    Okay, it nearly works .... but ...

    add_filter("gform_pre_submission_filter_1", "ser_preise");
    function ser_preise($form){
      /// some code
        $preise=maybe_serialize($preise);
         $_POST["input_7_1"] = $preise;
        //Echo the array
        echo  $preise;
        return $form;

    The Echo shows me the right result (a:2:{i:0;s:4:"ipad";i:1;s:6:"iphone";}) , but in database the result is

    s:35:"a:2:{i:0;s:4:"ipad";i:1;s:6:"iphone";}";

    It seems there is another "serialize" on the data

    Posted 12 years ago on Monday April 30, 2012 | Permalink
  7. schnettker
    Member

    if i look in the backend form and the notification email then i found the expected result.

    a:2:{i:0;s:4:"ipad";i:1;s:6:"iphone";}

    Why modify gravity form a serialized string again?

    Posted 12 years ago on Wednesday May 2, 2012 | Permalink
  8. Gravity Forms stores custom field data this way when handling multi-value field types because it's a standard way of storing custom field data in WordPress.

    You store multiple custom field values, using the same custom field key, and then you can pull all of them in to display them in your theme template by looking through the values that are pulled together as an array using built in WordPress functions.

    I understand you are unhappy with the situation, but the way Gravity Forms is doing things is not wrong or incorrect. It's just that as with anything there are a variety of ways to accomplish the same thing. Gravity Forms does it this way, ACF chose to do it another way.

    What we can do is look into adding a hook to Gravity Forms that can be used to tell Gravity Forms to store the values as an array in a single custom field rather than as multiple custom fields. I'll pass this along to our lead developer to see if this is something we can have implemented.

    In the mean time, what David suggested is definitely one way you can handle this. If you need to store the values in a specific format so another plugin you are using can work with it, then you can do this by using the gform_pre_submission hook to manipulate the data.

    Gravity Forms itself stores data in a serialized format, so there could be some added complexity going on here that you could be encountering.

    I am going to forward this to our lead developer who will take a look at what you are doing and respond to you with some suggestions.

    Posted 12 years ago on Wednesday May 2, 2012 | Permalink
  9. schnettker
    Member

    Sorry for my bad english. It seems you don't understand my Problem.

    If i chose checkboxes the gravity forms stores (if 2 checkboxes checked) the value like
    name_of_field value_1
    name_of_field value_2

    2 rows

    That is not nice but as david told i can fix it with gform_pre_submission_filter. Look at my code above - i did it allready! I create allready the wanted value (a:2:{i:0;s:4:"ipad";i:1;s:6:"iphone";} ) and in the mail i see that value (trailing after a dot).

    But when i look in the database, then i found a
    s:35:"a:2:{i:0;s:4:"ipad";i:1;s:6:"iphone";}";

    That is the serialized value of my wanted result. I don't understand why gravity form serialize that again!? If gravity form want a serialzied value, then why saving 2 rows? My data is perfectly in line 7 on my example above - but stored is another value

    Posted 12 years ago on Thursday May 3, 2012 | Permalink
  10. Yea, I see the issue. The string is getting serialized again. The problem is that the extra serialization is coming from WordPress. What I would suggest is that you use the gform_post_data filter instead. This filter you will give you total control on how the post is created and you can manipulate the data to fit your needs.
    http://www.gravityhelp.com/documentation/page/Gform_post_data

    Posted 12 years ago on Thursday May 3, 2012 | Permalink
  11. schnettker
    Member

    HI, in your example (GForm_post_data) i only see the possibility to change standard post fields. But in my case i have a custom field to manipulate.

    Posted 12 years ago on Friday May 4, 2012 | Permalink
  12. schnettker
    Member

    i analysed the data in $post_data (in my GForm_post_data function) and found the following

    Array
    (
        [post_custom_fields] => Array
            (
               [preise] => a:2:{i:0;s:4:"geld";i:1;s:4:"ipad";}
            )
    )

    That is the wanted result! Why it change to

    s:36:"a:2:{i:0;s:4:"geld";i:1;s:4:"ipad";}";

    ?

    Where can i stop the conversion?

    Posted 12 years ago on Monday May 7, 2012 | Permalink
  13. schnettker
    Member

    okay ... i found a solution without any hooks or filters but the solution isn't nice because i can't upgrade the plugin

    i modify your forms_model.php (line 1745 and more)

    case "checkbox" :
                            $value = explode(",", $value);
    						$arr = array();
                            if(is_array($value)){
                                foreach($value as $item){
                                    if(!rgblank($item))
                                        $arr[]=$item;
                                }
    							add_post_meta($post_id, $meta_name, $arr);
                            }
                        break;

    is their a way to put the code on a other place?

    Btw. my solution gives a clean output in the backend and works with all my other plugins and templates

    Posted 12 years ago on Monday May 7, 2012 | Permalink
  14. cmccrone
    Member

    Same issue. I wanted to use multi select with GF to create custom fields. Lets say my custom field is called "colors". It currently adds them individually and only displays the first one on the post/page.

    Field Name: Colors Value: Red
    Field Name: Colors Value: Blue
    Field Name: Colors Value: Green

    How can i make it like this:

    Field Name: Colors Value: Red, blue, green

    OR with returns

    Field Name: Colors Value: Red
    Blue
    Green

    Posted 12 years ago on Wednesday July 11, 2012 | Permalink
  15. David Peralty

    You will have to in the gform_pre_submission grab the details, strip things out, merge them together using PHP and then send them back to the form as one string. This would be reasonably complex to do, but a skilled PHP developer could create this add-on for you.

    Posted 12 years ago on Wednesday July 11, 2012 | Permalink

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