Skip to content

Instantly share code, notes, and snippets.

@richardW8k
Last active August 29, 2015 14:05
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save richardW8k/f860c80943458f684b41 to your computer and use it in GitHub Desktop.
Save richardW8k/f860c80943458f684b41 to your computer and use it in GitHub Desktop.
Testing the GF_Field class in Gravity Forms 1.9
<?php
/**
* Plugin Name: Gravity Forms Button Field
* Last Modified: 18/08/2014
*/
if ( ! class_exists( 'GFForms' ) ) {
die();
}
class GF_Field_Button_Settings {
public function __construct() {
add_action( 'gform_editor_js_set_default_values', array( $this, 'set_field_defaults' ) );
add_action( 'gform_field_standard_settings', array( $this, 'button_type_setting' ) );
add_action( 'gform_field_advanced_settings', array( $this, 'onclick_setting' ) );
add_filter( 'gform_tooltips', array( $this, 'tooltips' ) );
add_filter( 'gform_submit_button', array( $this, 'remove_submit_button' ), 10, 2 );
add_filter( 'gform_pre_render', array( $this, 'log_pre_render' ) );
}
public function set_field_defaults() {
?>
case 'button' :
if ( ! field.label ) {
field.label = '<?php _e( 'Button', 'gravityforms' ); ?>';
}
break;
<?php
}
// add button type dropdown to the fields properties tab
public function button_type_setting( $position ) {
if ( $position == 25 ) {
?>
<li class="button_type_setting field_setting">
<label for="button_type">
<?php _e( 'Button Type', 'gravityforms' ); ?>
<?php gform_tooltip( 'form_field_type' ) ?>
</label>
<select id="button_type" onchange="SetFieldProperty('buttonType', this.value); ToggleButtonType(this.value);">
<option value="button"><?php _e( 'Button', 'gravityforms' ); ?></option>
<option value="submit"><?php _e( 'Submit', 'gravityforms' ); ?></option>
</select>
</li>
<li class="disable_footer_submit_setting field_setting">
<input type="checkbox" id="disable_footer_submit" onclick="SetFieldProperty('disableSubmit', this.checked);"/>
<label for="disable_footer_submit" class="inline">
<?php _e( 'Disable Footer Submit Button', 'gravityforms' ); ?>
<?php gform_tooltip( 'disable_footer_submit' ) ?>
</label>
</li>
<?php
}
}
// add onclick input to the fields advanced tab
public function onclick_setting( $position ) {
if ( $position == 50 ) {
?>
<li class="button_onclick_setting field_setting">
<label for="button_onclick">
<?php _e( 'Button onclick', 'gravityforms' ); ?>
<?php gform_tooltip( 'button_onclick' ) ?>
</label>
<textarea id="button_onclick" class="fieldwidth-3 fieldheight-2" onkeyup="SetFieldProperty('onclick', this.value);"></textarea>
</li>
<?php
}
}
public function tooltips( $tooltips ) {
$tooltips['button_onclick'] = "Enter your custom script for the buttons onclick attribute. Don't forget to add the ending semicolon.";
$tooltips['disable_footer_submit'] = 'When enabled the submit button in the form footer will be removed.';
return $tooltips;
}
public function remove_submit_button( $button, $form ) {
foreach ( $form['fields'] as $field ) {
if ( $field->type == 'button' && $field->disableSubmit ) {
return '';
}
}
return $button;
}
function log_pre_render( $form ) {
GFCommon::log_debug( "log_pre_render(): \$fields => " . print_r( $form['fields'], true ) );
return $form;
}
}
new GF_Field_Button_Settings();
class GF_Field_Button extends GF_Field {
public $type = 'button';
public $displayOnly = true;
public $buttonType = 'button';
public function get_form_editor_button() {
return array(
'group' => 'advanced_fields',
'text' => __( 'Button', 'gravityforms' )
);
}
public function get_form_editor_field_title() {
return __( 'Button', 'gravityforms' );
}
public function get_form_editor_field_settings() {
return array(
'button_type_setting',
'button_onclick_setting',
'conditional_logic_field_setting',
'css_class_setting',
'disable_footer_submit_setting',
'label_setting',
);
}
public function get_field_input( $form, $value = '', $lead = null ) {
$is_entry_detail = $this->is_entry_detail();
$is_form_editor = $this->is_form_editor();
$form_id = $form['id'];
$id = intval( $this->id );
$type = $this->buttonType;
$label = esc_html( $this->label );
$disabled_text = $is_form_editor ? "disabled='disabled'" : "";
$tabindex = $this->get_tabindex();
$onclick_attr = ( $type == 'submit' || $this->__isset( 'onclick' ) ) ? "onclick='{SCRIPT}'" : '';
$onclick_val = esc_attr( $this->__get( 'onclick' ) );
if ( $type == 'submit' ) {
$onclick_val .= "if(window[\"gf_submitting_{$form_id}\"]){return false;}";
if ( GFFormsModel::is_html5_enabled() ) {
$onclick_val .= " if( ! jQuery('#gform_{$form_id}')[0].checkValidity || jQuery('#gform_{$form_id}')[0].checkValidity() ){window[\"gf_submitting_{$form_id}\"]=true;}";
} else {
$onclick_val .= " window[\"gf_submitting_{$form_id}\"]=true;";
}
}
$onclick_attr = str_replace( '{SCRIPT}', $onclick_val, $onclick_attr );
return "<div class='ginput_container'><button type='{$type}' id='gform_button_{$form_id}_{$id}' class='button gform_button' {$tabindex} {$onclick_attr} {$disabled_text}><span>{$label}</span></button></div>";
}
public function get_field_content( $value, $force_frontend_label, $form ) {
return sprintf( '%s{FIELD}', $this->get_admin_buttons() );
}
public function get_entry_field_display( $value, $currency = '', $use_text = false, $format = 'html', $media = 'screen' ) {
return false;
}
public function get_form_editor_inline_script_on_page_render() {
$script = "
jQuery(document).bind( 'gform_load_field_settings', function( event, field, form ) {
jQuery( '#button_onclick' ).val( field.onclick == undefined ? '' : field.onclick );
jQuery( '#button_type').val( field.buttonType == undefined ? 'button' : field.buttonType );
jQuery( '#disable_footer_submit').prop( 'checked', field.disableSubmit == true ? true : false );
jQuery( '.disable_footer_submit_setting' ).toggle( field.buttonType == 'submit' );
});
function ToggleButtonType( type ) {
var field = GetSelectedField(),
isSubmitButton = type == 'submit',
defaultLabel = jQuery( '#button_type option:selected' ).text();
jQuery( '#field_label' ).val( defaultLabel );
SetFieldProperty( 'label', defaultLabel );
jQuery( '.disable_footer_submit_setting' ).toggle( isSubmitButton );
if ( ! isSubmitButton ) {
SetFieldProperty( 'disableSubmit', false );
}
}
";
return $script;
}
}
GF_Fields::register( new GF_Field_Button() );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment