Skip to content

Commit

Permalink
gppa-populate-acf-field-choices.php: Added new snippet.
Browse files Browse the repository at this point in the history
spivurno authored Dec 5, 2023

Verified

This commit was created on GitHub.com and signed with GitHub’s verified signature.
1 parent d0264b4 commit 67d912e
Showing 1 changed file with 39 additions and 0 deletions.
39 changes: 39 additions & 0 deletions gp-populate-anything/gppa-populate-acf-field-choices.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
<?php
/**
* Gravity Perks // Populate Anything // Populate Choice-based ACF Field as Choices
* https://gravitywiz.com/documentation/gravity-forms-populate-anything/
*
* Instruction Video: Incoming...
*
* Use Populate Anything's field settings to map a choice-based ACF field to a Gravity Forms choice-based field (e.g.
* Drop Down, Radio Buttons, Checkboxes, etc). Then, use this snippet to automatically convert the ACF choices into
* choices in the Gravity Forms field.
*/
add_filter( 'gppa_input_choices', function ( $choices, $field ) {

if ( empty( $field->choices ) ) {
return $choices;
}

$object = rgar( $choices[0], 'object' );
if ( ! $object|| ! is_a( $object, 'WP_Post' ) || $object->post_type !== 'acf-field' ) {
return $choices;
}

$acf_field_config = maybe_unserialize( $object->post_content );
if ( ! $acf_field_config || empty( $acf_field_config['choices'] ) ) {
return $choices;
}

$new_choices = array();

foreach ( $acf_field_config['choices'] as $value => $label ) {
$new_choices[] = array(
'value' => $value,
'text' => $label,
'isSelected' => false,
);
}

return $new_choices;
}, 10, 2 );

0 comments on commit 67d912e

Please sign in to comment.