Last active
April 6, 2019 16:58
-
-
Save wernersmit/9d1fdd480af69e17e32f97381dc3e5c9 to your computer and use it in GitHub Desktop.
Provide a JavaScript lookup function to get Gravity Forms field ID from labels
This file contains hidden or 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 | |
| /** | |
| * Provide an interface to obtain field information on the frontend. | |
| */ | |
| class GF_JS_Fields_Query | |
| { | |
| /** | |
| * @var Singleton | |
| */ | |
| private static $instance; | |
| /** | |
| * gets the instance via lazy initialization (created on first usage) | |
| */ | |
| public static function getInstance() | |
| { | |
| if (null === static::$instance) { | |
| static::$instance = new static(); | |
| } | |
| return static::$instance; | |
| } | |
| /** | |
| * is not allowed to call from outside to prevent from creating multiple instances, | |
| * to use the singleton, you have to obtain the instance from Singleton::getInstance() instead | |
| */ | |
| private function __construct() | |
| { | |
| } | |
| /** | |
| * prevent the instance from being cloned (which would create a second instance of it) | |
| */ | |
| private function __clone() | |
| { | |
| } | |
| /** | |
| * prevent from being unserialized (which would create a second instance of it) | |
| */ | |
| private function __wakeup() | |
| { | |
| } | |
| public static function print_script($gform_id) { | |
| $script = self::render_form_fields_lookup_scripts( $gform_id ); | |
| $script = '<script>'.$script.'</script>'; | |
| echo $script; | |
| } | |
| public static function render_form_fields_lookup_scripts( $gform_id ) { | |
| $form = GFAPI::get_form($gform_id); | |
| // get all fields. | |
| $fields = $form['fields']; | |
| $js_fields_lookup = array(); | |
| foreach ($fields as $field_k => $field_item) { | |
| $js_fields_lookup[$field_item->label] = $field_item->id; | |
| } | |
| $script = "\n" . 'window[\'gf_js_fields_query_'.$form['id'].'\'] = function(field_name) {' . "\n" . | |
| ' var gf_fields_lookup_data = '.json_encode($js_fields_lookup).';' . "\n" . | |
| ' if (typeof gf_fields_lookup_data[field_name] !== \'undefined\') {' . "\n" . | |
| ' return gf_fields_lookup_data[field_name];' . "\n" . | |
| ' } else { ' . "\n" . | |
| ' console.log(\'Field not found: form '.$form['id'].': \' + field_name);' . "\n" . | |
| ' return false;' . "\n" . | |
| ' } ' . "\n" . | |
| ' } '; | |
| return $script; | |
| } | |
| } | |
| $GF_JS_Fields_Query = GF_JS_Fields_Query::getInstance(); |
This file contains hidden or 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 | |
| include 'class-gf-fields-js-query.php'; | |
| // Bind action to wp_head for specific forms you require the lookup functionality for. | |
| // e.g. Form ID 1 | |
| $form_id = 1; | |
| add_action('wp_head', function() use ($form_id) { GF_JS_Fields_Query::print_script($form_id); } ); |
This file contains hidden or 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
| function gf_js_fields_query(gform_id, field_name) { | |
| if (typeof window['gf_js_fields_query_' + gform_id] == 'function') { | |
| return window['gf_js_fields_query_' + gform_id](field_name); | |
| } else { | |
| return false; | |
| } | |
| } | |
| // -- usage exaample: | |
| var gform_id = jQuery('input[name=gform_submit]').val(); | |
| var package_filter_index_value = '1'; | |
| if (typeof gform_id != 'undefined') { | |
| var package_filter_index_id = gf_js_fields_query(gform_id, 'Package_Filter_Index'); | |
| jQuery('#input_' + gform_id + '_' + package_filter_index_id).val(package_filter_index_value); | |
| } | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment