Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save jetsloth/845149a12ac45bcd3ff03aab36348095 to your computer and use it in GitHub Desktop.
Save jetsloth/845149a12ac45bcd3ff03aab36348095 to your computer and use it in GitHub Desktop.
An example for using Gravity Perks Populate Anything with Image Choices where the image source is a form entry, and another Image Choices field
<?php
function gppa_image_choices_from_form_entry( $choice, $field, $object, $objects ) {
$object_type = rgobj($field, 'gppa-choices-object-type');
if ( $object_type != "gf_entry" || !function_exists('gf_image_choices') || !gf_image_choices()->field_has_image_choices_enabled($field) ) {
return $choice;
}
$templates = rgobj($field, 'gppa-choices-templates');
$template = (!empty($templates)) ? rgar($templates, 'imageChoices_image') : "";
if ( empty($template) || strpos($template, 'gf_field_') === FALSE ) {
return $choice;
}
$source_field_id = str_replace('gf_field_', '', $template);
$source_form_id = rgobj($object, 'form_id');
$source_value = rgobj($object, $source_field_id);
if ( empty($source_form_id) || !GFAPI::form_id_exists( $source_form_id ) ) {
return $choice;
}
$source_field = GFFormsModel::get_field($source_form_id, $source_field_id);
if ( empty($source_field) || !gf_image_choices()->field_has_image_choices_enabled($source_field) ) {
return $choice;
}
foreach( $source_field['choices'] as $source_choice ) {
if ( rgar($source_choice, 'value') == $source_value ) {
$choice['imageChoices_image'] = rgar($source_choice, 'imageChoices_image');
$choice['imageChoices_imageID'] = rgar($source_choice, 'imageChoices_imageID');
$choice['imageChoices_largeImage'] = rgar($source_choice, 'imageChoices_largeImage');
break;
}
}
return $choice;
}
//add_filter( 'gppa_input_choice', 'gppa_image_choices_from_form_entry', 10, 4);// all forms
//add_filter( 'gppa_input_choice_FORMID', 'gppa_image_choices_from_form_entry', 10, 4);// specific form (ID)
//add_filter( 'gppa_input_choice_FORMID_FIELDID', 'gppa_image_choices_from_form_entry', 10, 4);// specific form and field (ID)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment