Last active
December 26, 2019 14:46
-
-
Save claygriffiths/25522029c1b6e04abd9b165fd6285b6e to your computer and use it in GitHub Desktop.
GP Populate Anything: Checkboxes from an Entry as Choices
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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