|
| 1 | +<?php |
| 2 | +/** |
| 3 | + * Gravity Perks // Populate Anything // JetEngine Repeater Mapper |
| 4 | + * https://gravitywiz.com/documentation/gravity-forms-populate-anything/ |
| 5 | + * |
| 6 | + * Populate all rows from a JetEngine Repeater into a choice-based field. |
| 7 | + * |
| 8 | + * Instructions |
| 9 | + * |
| 10 | + * 1. Enable "Populate choices dynamically" on any choice-based field. |
| 11 | + * 2. Select the "Post" object type. |
| 12 | + * 3. Apply any desired filters to determine which post(s) should have their repeater data populated. |
| 13 | + * 4 Use the "Choice Template" to map each choice property to a repeater subfield. To do so you will need to: |
| 14 | + * |
| 15 | + * a. Select "Custom Value" from the "Value" dropdown and specify the repeater name and desire subfield like so: |
| 16 | + * "repeater_name/subfield_name". |
| 17 | + * b. Repeat for each Choice Template property (e.g. Label, and Price, if applicable). |
| 18 | + * |
| 19 | + * 5. Add the "gppa-jetengine-repeater-mapper" to the field's CSS Class Name setting. |
| 20 | + * |
| 21 | + * Plugin Name: GP Populate Anything — JetEngine Repeater Mapper |
| 22 | + * Plugin URI: https://gravitywiz.com/documentation/gravity-forms-populate-anything/ |
| 23 | + * Description: Populate all rows from a JetEngine Repeater into a choice-based field. |
| 24 | + * Author: Gravity Wiz |
| 25 | + * Version: 0.1 |
| 26 | + * Author URI: https://gravitywiz.com |
| 27 | + */ |
| 28 | +add_filter( 'gppa_input_choices', function( $choices, $field, $objects ) { |
| 29 | + |
| 30 | + if ( strpos( $field->cssClass, 'gppa-jetengine-repeater-mapper' ) === false ) { |
| 31 | + return $choices; |
| 32 | + } |
| 33 | + |
| 34 | + $map = array(); |
| 35 | + |
| 36 | + foreach ( $field->{'gppa-choices-templates'} as $template => $key ) { |
| 37 | + // Look for ACF repeater meta: gf_custom:repeater-name/subfield-name |
| 38 | + if ( preg_match( '/^gf_custom:(.+?)\/(.+?)$/', $key, $matches ) ) { |
| 39 | + list( , $repeater, $subfield ) = $matches; |
| 40 | + $map[ $template ] = $subfield; |
| 41 | + } |
| 42 | + } |
| 43 | + |
| 44 | + if ( ! $repeater ) { |
| 45 | + return $choices; |
| 46 | + } |
| 47 | + |
| 48 | + $choices = array(); |
| 49 | + |
| 50 | + foreach ( $objects as $object ) { |
| 51 | + $rows = rgar( get_post_meta( $object->ID, $repeater ), 0 ); |
| 52 | + if ( $rows ) { |
| 53 | + foreach ( $rows as $row ) { |
| 54 | + if ( isset( $map['price'] ) ) { |
| 55 | + $choices[] = array( |
| 56 | + 'value' => rgar( $row, $map['value'] ), |
| 57 | + 'text' => rgar( $row, $map['label'] ), |
| 58 | + 'price' => rgar( $row, $map['price'] ), |
| 59 | + ); |
| 60 | + } else { |
| 61 | + $choices[] = array( |
| 62 | + 'value' => rgar( $row, $map['value'] ), |
| 63 | + 'text' => rgar( $row, $map['label'] ), |
| 64 | + ); |
| 65 | + } |
| 66 | + } |
| 67 | + } |
| 68 | + } |
| 69 | + |
| 70 | + return $choices; |
| 71 | +}, 10, 3 ); |
0 commit comments