skip to Main Content

In Gravity Form’s class GF_Field_Checkbox there is a method called get_value_export() that returns implode( ', ', $selected ) for a bunch of elements created by GFCommon::selection_display()

The class-gf-field-checkbox.php code so you can see what I am referencing.

    public function get_value_export( $entry, $input_id = '', $use_text = false, $is_csv = false ) {

        if ( empty( $input_id ) || absint( $input_id ) == $input_id ) {

            $selected = array();

            foreach ( $this->inputs as $input ) {

                $index = (string) $input['id'];

                if ( ! rgempty( $index, $entry ) ) {
                    $selected[] = GFCommon::selection_display( rgar( $entry, $index ), $this, rgar( $entry, 'currency' ), $use_text );
                }

            }

            return implode( ', ', $selected );
        ...

This is all well and good, however, the problem with this is that I’m exploding the values that are returned from this method.

$answer = explode(', ', $field->get_value_export($entry));

I do not want to do this as there exists an edge case where a potential value could have a comma which gets exploded. For example, say there is an option in my form like below

Label: Are you not entertained?
Value: 
 [x] Lorem ipsum dolor sit amet, consectetur adipiscing elit
 [x] Duis blandit, risus vel rutrum suscipit
 [ ] Duis cursus ex risus

As you can see the first two selections are selected, and this will be picked up and will then be exploded as such

['Lorem ipsum dolor sit amet', 'consectetur adipiscing elit', 'Duis blandit', 'risus vel rutrum suscipit']

When it should have been exploded like this

['Lorem ipsum dolor sit amet, consectetur adipiscing elit', 'Duis blandit, risus vel rutrum suscipit']

What method exists in GFAPI, or custom code can I use that could resolve this issue?

2

Answers


  1. Chosen as BEST ANSWER

    Rochelle's answer was useful in producing this solution. Instead of using GF_Field_Checkbox's method, I've created my own function that will pull out the necessary values into an array, while effectively getting rid of the comma issue, due to using explode()

    function get_value_export($entry, $input_id)
    {
        $items = array();
        foreach ($entry as $key => $value) {
            if ($value == "") continue;
            if (!is_numeric($key)) continue;
            if (absint($key) !== absint($input_id)) continue;
    
            $items[] = $value;
        }
        return $items;
    }
    

  2. You can retrieve the checkbox values in the following way:

        $entry = GFAPI::get_entry( **entry id here** );//add your entry id in parenthesis
        $field_id = **add field number here**;
        $items = array(); 
        $field_keys = array_keys( $entry );
        foreach ( $field_keys as $input_id ) {
          if ( is_numeric( $input_id ) && absint( $input_id ) == $field_id ) {
            $value = rgar( $entry, $input_id );
            if ( "" !== $value ) $items[ $input_id ] = str_replace(",",",",$value);
          }  
        }
        $answer = implode(",",$items);
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search