Skip to content

Instantly share code, notes, and snippets.

@wpmudev-sls
Last active June 4, 2022 14:58
Show Gist options
  • Save wpmudev-sls/0577621206dd420e93c9c701b08b6b87 to your computer and use it in GitHub Desktop.
Save wpmudev-sls/0577621206dd420e93c9c701b08b6b87 to your computer and use it in GitHub Desktop.
[Forminator] Fix Search Inputs on Selec Fields
{"type":"form","data":{"fields":[{"id":"address-1","element_id":"address-1","form_id":"wrapper-516-6725","type":"address","options":[],"cols":12,"conditions":[],"wrapper_id":"wrapper-516-6725","street_address":false,"address_city":false,"address_state":false,"address_zip":false,"address_country":"true","address_line":false,"street_address_label":"Street Address","street_address_placeholder":"E.g. 42 Wallaby Way","address_city_label":"City","address_city_placeholder":"E.g. Sydney","address_state_label":"State/Province","address_state_placeholder":"E.g. New South Wales","address_zip_label":"ZIP / Postal Code","address_zip_placeholder":"E.g. 2000","address_country_label":"Country","address_line_label":"Apartment, suite, etc","street_address_required_message":"This field is required. Please enter the street address.","address_zip_required_message":"This field is required. Please enter the zip code.","address_country_required_message":"This field is required. Please select the country.","address_city_required_message":"This field is required. Please enter the city.","address_state_required_message":"This field is required. Please enter the state.","address_line_required_message":"This field is required. Please enter address line.","address_country_placeholder":"Brazil"},{"id":"select-1","element_id":"select-1","form_id":"wrapper-4085-6743","type":"select","options":[{"label":"Option 1","value":"one","limit":"","key":"1575-3642"},{"label":"Option 2","value":"two","limit":"","key":"2678-1249"}],"cols":12,"conditions":[],"wrapper_id":"wrapper-4085-6743","value_type":"single","field_label":"Select","placeholder":"This placeholder belongs to the select field..."}],"settings":{"pagination-header":"nav","paginationData":{"pagination-header-design":"show","pagination-header":"nav"},"formName":"Select2 Placeholder Bug","version":"1.14.11","form-border-style":"none","form-padding":"","form-border":"","fields-style":"open","validation":"on_submit","form-style":"default","enable-ajax":"true","autoclose":"true","submission-indicator":"show","indicator-label":"Submitting...","form-type":"default","submission-behaviour":"behaviour-thankyou","thankyou-message":"Thank you for contacting us, we will be in touch shortly.","submitData":{"custom-submit-text":"Send Message","custom-invalid-form-message":"Error: Your form is not valid, please fix the errors!"},"validation-inline":"1","form-expire":"no_expire","form-padding-top":"0","form-padding-right":"0","form-padding-bottom":"0","form-padding-left":"0","form-border-width":"0","form-border-radius":"0","cform-label-font-family":"Roboto","cform-label-custom-family":"","cform-label-font-size":"12","cform-label-font-weight":"bold","cform-title-font-family":"Roboto","cform-title-custom-family":"","cform-title-font-size":"45","cform-title-font-weight":"normal","cform-title-text-align":"left","cform-subtitle-font-family":"Roboto","cform-subtitle-custom-font":"","cform-subtitle-font-size":"18","cform-subtitle-font-weight":"normal","cform-subtitle-text-align":"left","cform-input-font-family":"Roboto","cform-input-custom-font":"","cform-input-font-size":"16","cform-input-font-weight":"normal","cform-radio-font-family":"Roboto","cform-radio-custom-font":"","cform-radio-font-size":"14","cform-radio-font-weight":"normal","cform-select-font-family":"Roboto","cform-select-custom-family":"","cform-select-font-size":"16","cform-select-font-weight":"normal","cform-multiselect-font-family":"Roboto","cform-multiselect-custom-font":"","cform-multiselect-font-size":"16","cform-multiselect-font-weight":"normal","cform-dropdown-font-family":"Roboto","cform-dropdown-custom-font":"","cform-dropdown-font-size":"16","cform-dropdown-font-weight":"normal","cform-calendar-font-family":"Roboto","cform-calendar-custom-font":"","cform-calendar-font-size":"13","cform-calendar-font-weight":"normal","cform-button-font-family":"Roboto","cform-button-custom-font":"","cform-button-font-size":"14","cform-button-font-weight":"500","cform-timeline-font-family":"Roboto","cform-timeline-custom-font":"","cform-timeline-font-size":"12","cform-timeline-font-weight":"normal","cform-pagination-font-family":"","cform-pagination-custom-font":"","cform-pagination-font-size":"16","cform-pagination-font-weight":"normal","payment_require_ssl":"","submission-file":"delete","form_name":"Select2 Placeholder Bug","form_status":"publish"},"client_id":null,"notifications":[{"slug":"notification-1234-4567","label":"Admin Email","email-recipients":"default","recipients":"[email protected]","email-subject":"New Form Entry #{submission_id} for {form_name}","email-editor":"You have a new website form submission: <br/> {all_fields} <br/>---<br/> This message was sent from {site_url}.","email-attachment":"true"}]},"status":"publish","version":"1.14.11"}
<?php
/**
* Plugin Name: [Forminator] Fix Search Inputs on Selec Fields
* Plugin URI: https://premium.wpmudev.org
* Description: When you add a select field on the form and also add an address field with country enabled, the placeholder for all search fields (inside the dropdown options of each one) became the same. And even that you disable the search option in the select field, the search input will be displayed - this plugin fixes these problems.
* Author: Glauber Silva @ WPMUDEV
* Author URI: https://premium.wpmudev.org
* Jira Task: SLS-2407
* License: GPLv2 or later
*
* @package WPMUDEV_Forminator_Fix_Search_Inputs_on_Selec_Fields
*/
defined( 'ABSPATH' ) || exit;
function wpmudev_forminator_fix_search_inputs_on_selec_fields( $html ) {
ob_start();
?>
<script type="text/javascript">
( ( $,d ) => {
$( d ).ready( function() {
$('select').on("select2:open", function (e) {
var placeholder = '';
if ( !! this.dataset.defaultValue ) {
placeholder = this.dataset.defaultValue;
} else if ( !! this.dataset.placeholder ) {
placeholder = this.dataset.placeholder;
}
$('.select2-search__field').attr( "placeholder", placeholder );
if ( this.dataset.search == "true" ) {
$('.select2-search__field').show();
} else {
$('.select2-search__field').hide();
}
//console.log( 'Placeholder: ', placeholder ) ;
//console.log("select2:open", e);
});
} );
} ) ( jQuery,document );
</script>
<?php
$javascript = ob_get_clean();
return $html . $javascript; // phpcs:ignore
}
add_filter( 'forminator_render_form_markup', 'wpmudev_forminator_fix_search_inputs_on_selec_fields', PHP_INT_MAX );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment