Last active
August 2, 2020 14:02
-
-
Save w-jerome/53eb9adae20bd61c842edfa744d4a5e3 to your computer and use it in GitHub Desktop.
WordPress — Markup Gravity Forms
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 | |
| /* | |
| * Documentation : | |
| * https://docs.gravityforms.com/ | |
| * https://gravitywiz.com/gravity-forms-hook-reference/ | |
| */ | |
| // On execute ce code sinon il fait les modifications également quand on est dans le panneau d'administration | |
| if (!is_admin()) { | |
| /* | |
| * Ce hook passe sur tout les formulaire et wrap les inputs["checkbox", "radio"] dans des label | |
| */ | |
| function gform_field_choice_markup_pre_render_function($choice_markup, $choice, $field, $value) { | |
| preg_match_all('/<input([^>]+)>/', $choice_markup, $matches); | |
| if (!empty($matches[0])) { | |
| $choice_markup = '<label class="field-choice field-choice--'.$field->type.'">'; | |
| $choice_markup .= $matches[0][0]; | |
| $choice_markup .= '<span class="field-choice__fake-input"></span>'; | |
| $choice_markup .= '<span class="field-choice__label">'.$choice['text'].'</span>'; | |
| $choice_markup .= '</label>'; | |
| } | |
| return $choice_markup; | |
| } | |
| add_filter('gform_field_choice_markup_pre_render', 'gform_field_choice_markup_pre_render_function', 10, 4); | |
| /* | |
| * Ce hook passe sur input et ajoute les bonnes class CSS | |
| */ | |
| function gform_field_input_function($input, $field, $value, $lead_id, $form_id) { | |
| $is_required = ($field->isRequired) ? 'true' : 'false'; | |
| $is_invalid = ($field->failed_validation) ? ' aria-invalid="true"': ''; | |
| $placeholder = ($field->placeholder) ? ' placeholder="'.$field->placeholder.'"': ''; | |
| if ($field->type === 'text' || $field->type === 'email' || $field->type === 'phone') { | |
| $input = '<input type="text" name="input_'.$field->id.'" id="input_'.$form_id.'_'.$field->id.'" value="'.$value.'" class="field-text || field--w100" aria-required="'.$is_required.'"'.$placeholder.$is_invalid.'>'; | |
| } else if ($field->type === 'textarea') { | |
| $input = '<textarea name="input_'.$field->id.'" id="input_'.$form_id.'_'.$field->id.'" class="field-text || field--w100" aria-required="'.$is_required.'"'.$placeholder.$is_invalid.'>'.$value.'</textarea>'; | |
| } | |
| return $input; | |
| } | |
| add_filter('gform_field_input', 'gform_field_input_function', 10, 5); | |
| /* | |
| * Ce hook passe sur champs et change la class CSS de son container | |
| */ | |
| function gform_field_container_function($field_container, $field, $form, $css_class, $style, $field_content) { | |
| $css_class = (!empty($field->cssClass)) ? ' '.$field->cssClass : ''; | |
| $css_class .= ($field->isRequired) ? ' field-group--is-required' : ''; | |
| $css_class .= (!$field->visibility) ? ' field-group--is-hidden' : ''; | |
| $css_class .= ($field->failed_validation) ? ' field-group--is-invalid' : ''; | |
| $field_container = preg_replace("/class='([^']+)'/", 'class=\'${1} field-group field-group--'.$field->type.$css_class.'\'', $field_container); | |
| return $field_container; | |
| } | |
| add_filter('gform_field_container', 'gform_field_container_function', 10, 6); | |
| /* | |
| * Ce hook passe sur le bouton submit et change les class CSS | |
| */ | |
| function gform_submit_button_function($button, $form) { | |
| $button = preg_replace('/\/>$/', '>', $button); | |
| $button = preg_replace('/<input ([^>]+)>/', '<button ${1}><span>'.$form['button']['text'].'</span></button>', $button); | |
| $button = preg_replace('/class=\'([^\']+)\'/', 'class=\'btn btn--min-width--large btn--large btn--animation || gform_button\'', $button); | |
| return $button; | |
| } | |
| add_filter('gform_submit_button', 'gform_submit_button_function', 10, 2); | |
| /* | |
| * Ce hook passe suprime la class CSS du wrapper | |
| */ | |
| function gform_get_form_filter_function($form_string, $form) { | |
| $form_string = preg_replace('/<ul id=\'gform_fields_([0-9]+)\' ([^>]+)>/', '<ul id=\'gform_fields_${1}\' class=\'grid__row\'>', $form_string); | |
| // On ajoute les class "grid" et "grid__row" pour créer un système de grille | |
| // $form_string = preg_replace('/gform_body/', 'grid || gform_body', $form_string); | |
| $form_string = preg_replace('/description_below/', 'grid__row || description_below', $form_string); | |
| // On ajoute les class "title" au label | |
| $form_string = preg_replace('/gfield_label+/', 'title title--smaller || gfield_label', $form_string); | |
| $form_string = preg_replace('/<label ([^class]+[^>]+)>/', '<label ${1} class="title title--smaller || gfield_label">', $form_string); | |
| return $form_string; | |
| } | |
| add_filter('gform_get_form_filter', 'gform_get_form_filter_function', 10, 2); | |
| /* | |
| * On passe le javascript de gravity forms dans le footer | |
| */ | |
| add_filter('gform_init_scripts_footer', '__return_true'); | |
| /* | |
| * Si un formulaire est Ajax, alors le javascript qui lui sera généré sera encapsulé dans une fonction pour éviter le problème de chargement Jquery | |
| */ | |
| function gform_cdata_open_function() { | |
| return 'document.addEventListener("DOMContentLoaded", function() {'; | |
| } | |
| function gform_cdata_close_function() { | |
| return '}, false);'; | |
| } | |
| add_filter('gform_cdata_open', 'gform_cdata_open_function'); | |
| add_filter('gform_cdata_close', 'gform_cdata_close_function'); | |
| } | |
| add_filter('gform_enable_field_label_visibility_settings', '__return_true'); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment