Last active
August 5, 2024 03:44
-
-
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
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 | |
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; | |
} | |
// Just use any priority later than the default of 10. | |
//add_filter( 'gppa_input_choice', 'gppa_image_choices_from_form_entry', 100, 4);// all forms | |
//add_filter( 'gppa_input_choice_FORMID', 'gppa_image_choices_from_form_entry', 100, 4);// specific form (ID) | |
//add_filter( 'gppa_input_choice_FORMID_FIELDID', 'gppa_image_choices_from_form_entry', 100, 4);// specific form and field (ID) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment