Skip to content

Instantly share code, notes, and snippets.

@efc
Last active June 16, 2016 16:21
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save efc/8003f82b4b3067fd658872b53e80987f to your computer and use it in GitHub Desktop.
Save efc/8003f82b4b3067fd658872b53e80987f to your computer and use it in GitHub Desktop.
Submit Test Add-On for Gravity Forms
<?php
GFForms::include_addon_framework();
class SubmitTestAddOn extends GFAddOn {
protected $_version = "1.0";
protected $_min_gravityforms_version = '1.9';
protected $_slug = 'submittestaddon';
protected $_path = 'submittestaddon/submittestaddon.php';
protected $_full_path = __FILE__;
protected $_title = 'Submit Test Add-On';
protected $_short_title = 'Submit Test Add-On';
private static $_instance = null;
/**
* Get an instance of this class.
*
* @return SubmitTestAddOn
*/
public static function get_instance() {
if ( self::$_instance == null ) {
self::$_instance = new SubmitTestAddOn();
}
return self::$_instance;
}
/**
* Configures the settings which should be rendered on the Form Settings > Simple Add-On tab.
*
* @return array
*/
public function form_settings_fields( $form ) {
error_log(__FUNCTION__.' settings here' );
$valueOne = $this->get_setting( 'mydropdown' );
$choicesTwo = [
'first' => [
[ 'label'=>'None', 'value'=>'' ],
[ 'label'=>'11', 'value'=>'11' ],
[ 'label'=>'12', 'value'=>'12' ],
[ 'label'=>'13', 'value'=>'13' ],
],
'second' => [
[ 'label'=>'None', 'value'=>'' ],
[ 'label'=>'21', 'value'=>'21' ],
[ 'label'=>'22', 'value'=>'22' ],
[ 'label'=>'23', 'value'=>'23' ],
],
'third' => [
[ 'label'=>'None', 'value'=>'' ],
[ 'label'=>'31', 'value'=>'31' ],
[ 'label'=>'32', 'value'=>'32' ],
[ 'label'=>'13', 'value'=>'33' ],
],
];
return array(
array(
'title' => esc_html__( 'Submit Test Settings', 'submittestaddon' ),
'fields' => array(
array(
'label' => esc_html__( 'My Dropdown', 'submittestaddon' ),
'type' => 'select',
'name' => 'mydropdown',
'tooltip' => esc_html__( 'This is the tooltip', 'submittestaddon' ),
'choices' => array(
array(
'label' => esc_html__( 'None', 'submittestaddon' ),
'value' => '',
),
array(
'label' => esc_html__( 'First Choice', 'submittestaddon' ),
'value' => 'first',
),
array(
'label' => esc_html__( 'Second Choice', 'submittestaddon' ),
'value' => 'second',
),
array(
'label' => esc_html__( 'Third Choice', 'submittestaddon' ),
'value' => 'third',
),
),
'onchange' => 'jQuery(this).parents("form").submit();',
),
array(
'label' => esc_html__( 'My Second Dropdown', 'submittestaddon' ),
'type' => 'select',
'name' => 'myseconddropdown',
'tooltip' => esc_html__( 'This is the tooltip', 'submittestaddon' ),
'choices' => array_key_exists($valueOne, $choicesTwo) ? $choicesTwo[$valueOne] : array(
array(
'label' => 'None',
'value' => '',
),
),
'validation_callback' => array( $this, 'second_validation_handler'),
),
),
),
);
}
}
<?php
/*
Plugin Name: Gravity Forms Submit Test Add-On
Plugin URI: http://tenseg.net
Description: A test to demonstrate the use of self-submission with the Add-On Framework
Version: 1.0
Author: Eric Celeste
Author URI: http://tenseg.net
*/
add_action( 'gform_loaded', array( 'Submit_Test_AddOn_Bootstrap', 'load' ), 5 );
class Submit_Test_AddOn_Bootstrap {
public static function load() {
if ( ! method_exists( 'GFForms', 'include_addon_framework' ) ) {
return;
}
require_once( 'class-submittestaddon.php' );
GFAddOn::register( 'SubmitTestAddOn' );
}
}
function submit_test_addon() {
return SubmitTestAddOn::get_instance();
}
@efc
Copy link
Author

efc commented Jun 16, 2016

On the GFAddOn Settings API page they write: "The dependency is only checked upon page load and does not fire with events. ... To do this, you would need to force the page to submit so the dependency will be checked again upon page load."

But how do you actually create a dependency like this?

The code above is one example. In it, the choices presented in the second drop down are dependent on the value chosen in the first dropdown. The keys to making this work are the onchange JavaScript attribute in the settings for the first dropdown and the use of get_setting() to pull up the in-process settings of that first dropdown when building the second.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment