Skip to content

Instantly share code, notes, and snippets.

@claygriffiths
Last active December 26, 2019 14:46
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save claygriffiths/25522029c1b6e04abd9b165fd6285b6e to your computer and use it in GitHub Desktop.
Save claygriffiths/25522029c1b6e04abd9b165fd6285b6e to your computer and use it in GitHub Desktop.
GP Populate Anything: Checkboxes from an Entry as Choices
<?php
/**
* See https://gravitywiz.com/documentation/how-do-i-install-a-snippet/ for details on how to install snippets like these.
*
* The following snippet allows you to use a checkbox field as choices.
*
* This will use whatever property is selected in the "Value Template" for the choices.
*
* FORMID is the form that the field with dynamically populated choices is on
* FIELDID is the field ID of the field that you wish to modify the choices for
*/
add_filter( 'gppa_input_choices_FORMID_FIELDID', function ( $choices, $field, $objects ) {
$choices = array();
$templates = rgar( $field, 'gppa-choices-templates', array() );
foreach ( $objects as $object ) {
$field = str_replace( 'gf_field_', '', rgar( $templates, 'value' ) );
foreach ( $object as $meta_key => $meta_value ) {
if ( absint( $meta_key ) === absint( $field ) ) {
/**
* Some fields such as the multi-select store the selected values in one meta value.
*
* Other fields such as checkboxes store them as individual meta values.
*/
$meta_value = GFAddOn::maybe_decode_json( $meta_value );
if ( is_array( $meta_value ) ) {
foreach ( $meta_value as $value ) {
$choices[] = array(
'value' => $value,
'text' => $value,
);
}
} else {
$choices[] = array(
'value' => $meta_value,
'text' => $meta_value,
);
}
}
}
}
return $choices;
}, 10, 3 );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment