Created
May 25, 2019 02:15
-
-
Save claygriffiths/65bf6a94795f76f7c9616345677f40a7 to your computer and use it in GitHub Desktop.
Populate choices from a property that returns an array.
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 an array to populate the field 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(); | |
$object_type = gp_populate_anything()->get_object_type( rgar( $field, 'gppa-choices-object-type' ) ); | |
$templates = rgar( $field, 'gppa-choices-templates', array() ); | |
foreach ( $objects as $object ) { | |
$array = $object_type->get_object_prop_value( $object, rgar( $templates, 'value' ) ); | |
foreach ( $array as $array_item ) { | |
$choices[] = array( | |
'value' => $array_item, | |
'text' => $array_item, | |
); | |
} | |
} | |
return $choices; | |
}, 10, 3 ); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment