|
| 1 | +<?php |
| 2 | +/** |
| 3 | + * Gravity Perks // Entry Blocks // Filters Block: Use Select for Country Input Filter |
| 4 | + * https://gravitywiz.com/documentation/gravity-forms-entry-blocks/ |
| 5 | + * |
| 6 | + * Experimental Snippet 🧪 |
| 7 | + * |
| 8 | + * All input fields in the Entry Blocks filter are text inputs by default. This snippet changes the input type to a |
| 9 | + * select for the Address field's Country input. |
| 10 | + */ |
| 11 | +add_filter( 'gpeb_filter_form_field', function ( $field ) { |
| 12 | + if ( ! rgar( $field, 'gpebFilterInputField' ) ) { |
| 13 | + return $field; |
| 14 | + } |
| 15 | + |
| 16 | + $input_field = $field['gpebFilterInputField']; |
| 17 | + |
| 18 | + // If we're not working with an Address field, return the field as-is. |
| 19 | + if ( $input_field['type'] !== 'address' ) { |
| 20 | + return $field; |
| 21 | + } |
| 22 | + |
| 23 | + // Get the input ID. |
| 24 | + $input_id = explode( '.', rgar( $field, 'gpebFilterSearch' ) )[1]; |
| 25 | + |
| 26 | + switch ( $input_id ) { |
| 27 | + // Country |
| 28 | + case 6: |
| 29 | + $country_choices = array_map( function ( $country ) { |
| 30 | + return array( |
| 31 | + 'text' => $country, |
| 32 | + 'value' => $country, |
| 33 | + ); |
| 34 | + }, $field['gpebFilterInputField']->get_countries() ); |
| 35 | + |
| 36 | + // Change the field to a select and populate it with countries from the address field. |
| 37 | + $new_field = new \GF_Field_Select( array( |
| 38 | + 'id' => $field['id'], |
| 39 | + 'label' => $field['label'], |
| 40 | + 'gpebFilterSearch' => $field['gpebFilterSearch'], |
| 41 | + 'gpebFilterInputField' => $field['gpebFilterInputField'], |
| 42 | + 'size' => $field['size'], |
| 43 | + 'inputs' => $field['inputs'], |
| 44 | + 'formId' => $field['formId'], |
| 45 | + 'type' => 'select', |
| 46 | + 'choices' => $country_choices, |
| 47 | + ) ); |
| 48 | + return $new_field; |
| 49 | + } |
| 50 | + |
| 51 | + return $field; |
| 52 | +} ); |
0 commit comments