Hooks running multiple times [RESOLVED]

Hi all,

I have the following excerpts inside a class that extends GF_Field:

<?php
class DDMWGF_Field_Authority extends GF_Field {

	public function __construct( $data = array() ){
        parent::__construct( $data );
		$this->add_hooks();
	}
	
	public function add_hooks(){
		add_action('gform_editor_js_set_default_values', array($this, 'js_set_default_values' ) );
		add_action('gform_field_standard_settings', array($this, 'tab_content'), 10, 2);
	}

	public function js_set_default_values() {
		?>
		case "<?php echo $this->type; ?>" :
			field.label = <?php echo json_encode( esc_html(DDMWGF_DDA) ); ?>;
	        field.checkboxLabel = "<?php echo DDMWGF_DDA_AGREE; ?>";
			field.isRequired = true;
			break;
		<?php
	}

	public function tab_content( $placement, $form_id ){
		if($placement === -1){
			?>
			<li class="ddmwgf_link_fields field_setting">
			
				<label for='field_link_ddmwgf_bsb' >
					<?php echo DDMWGF_BSB; ?>
					<?php gform_tooltip( "form_field_link_ddmwgf_bsb" ) ?>
				</label>
				<select id='field_link_ddmwgf_bsb' onBlur="SetFieldProperty( 'field_link_ddmwgf_bsb', this.value);">
					<!-- automatically filled using JavaScript -->
				</select>
			</li>
		
			<li class="ddmwgf_link_fields field_setting">
				<label for='field_link_ddmwgf_acc' >
					<?php echo DDMWGF_ACC; ?>
					<?php gform_tooltip( "form_field_link_ddmwgf_acc" ) ?>
				</label>
				<select id='field_link_ddmwgf_acc' onBlur="SetFieldProperty( 'field_link_ddmwgf_acc', this.value);">
					<!-- automatically filled using JavaScript -->
				</select>
			</li>
		
			<li class="ddmwgf_link_fields field_setting">
				<label for='field_link_ddmwgf_name' >
					<?php echo DDMWGF_NAME; ?>
					<?php gform_tooltip( "form_field_link_ddmwgf_name" ) ?>
				</label>
				<select id='field_link_ddmwgf_name' onBlur="SetFieldProperty( 'field_link_ddmwgf_name', this.value);">
					<!-- automatically filled using JavaScript -->
				</select>
			</li>
		
			<li class="ddmwgf_link_fields field_setting">
				<label for='field_link_ddmwgf_cusname' >
					<?php echo DDMWGF_CUSTOMER_NAME; ?>
					<?php gform_tooltip( "form_field_link_ddmwgf_cusname" ) ?>
				</label>
				<select id='field_link_ddmwgf_cusname' onBlur="SetFieldProperty( 'field_link_ddmwgf_cusname', this.value);">
					<!-- automatically filled using JavaScript -->
				</select>
			</li>
		
			<li class="ddmwgf_link_fields field_setting">
				<label for='field_link_ddmwgf_cusaddr' >
					<?php echo DDMWGF_CUSTOMER_ADDRESS; ?>
					<?php gform_tooltip( "form_field_link_ddmwgf_cusaddr" ) ?>
				</label>
				<select id='field_link_ddmwgf_cusaddr' onBlur="SetFieldProperty( 'field_link_ddmwgf_cusaddr', this.value);">
					<!-- automatically filled using JavaScript -->
				</select>
			</li>
		
			<li class="ddmwgf_link_fields field_setting">
				<label for='field_link_ddmwgf_cusemail' >
					<?php echo DDMWGF_CUSTOMER_EMAIL; ?>
					<?php gform_tooltip( "form_field_link_ddmwgf_cusemail" ) ?>
				</label>
				<select id='field_link_ddmwgf_cusemail' onBlur="SetFieldProperty( 'field_link_ddmwgf_cusemail', this.value);">
					<!-- automatically filled using JavaScript -->
				</select>
			</li>
		
			<li class="ddmwgf_link_fields field_setting">
				<label for='field_link_ddmwgf_cusphone' >
					<?php echo DDMWGF_CUSTOMER_PHONE; ?>
					<?php gform_tooltip( "form_field_link_ddmwgf_cusphone" ) ?>
				</label>
				<select id='field_link_ddmwgf_cusphone' onBlur="SetFieldProperty( 'field_link_ddmwgf_cusphone', this.value);">
					<!-- automatically filled using JavaScript -->
				</select>
			</li>
		
		
			<?php
		}
	}
}

(I’ve left out the rest of the class for clarity as this is the problem code)

Can anyone identify why these fields are appearing 3 consecutive times like below?

By adding the hooks in this way, they are being added for every field of that type that is instantiated, instead of only once. Try an approach like this:

	public function __construct( $data = array() ) {
		parent::__construct( $data );
		if ( ! has_action( 'init', array( __class__, 'add_hooks' ) ) ) {
			add_action( 'init', array( __class__, 'add_hooks' ), 11 );
		}
	}

By the way, when extending GF_FIeld you wouldn’t use the gform_editor_js_set_default_values filter. You would override the get_form_editor_inline_script_on_page_render function. See the following page of the documentation:

1 Like

Interesting, considering there was only one instance of the field type on this form, that it instantiates 3 times. That approach did rectify it, however.

As to the alternative method of setting the defaults, the documentation you linked to is unclear. By the logic in that article, the function would be:

43 function SetDefaultValues_direct_debit_authority(field) {
44    field.label = json_encode( esc_html(DDMWGF_DDA) );
45    field.checkboxLabel = DDMWGF_DDA_AGREE;
46    field.isRequired = true;
47 }

This is not valid PHP and causes a critical error: PHP Parse error: syntax error, unexpected ')', expecting variable (T_VARIABLE) in .../authority.php on line 43 …?

EDIT: I stand corrected - after a few reads I understand that you mean to use get_form_editor_inline_script_on_page_render() to insert the above into the js. With the functions being shown outside the context of that php function lower on the page, that isn’t clear.

1 Like