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?
CodePudding user response:
Modify the get_value_export() method of the GF_Field_Checkbox class to use a different delimiter to separate the selected options. Instead of using a comma, you could use a different character or string that is unlikely to appear in the option values.
You could modify the get_value_export() method to use a pipe (|) or a double pipe (||) as the delimiter:
$selected = implode( '|', $selected );
Then, when you explode the values, you would use the new delimiter:
$answer = explode( '|', $field->get_value_export( $entry ) );
CodePudding user response:
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);