spivurno
1/20/2017 - 7:12 PM

Gravity Wiz // Gravity Forms // Move Field on Form (supports moving to different pages)

Gravity Wiz // Gravity Forms // Move Field on Form (supports moving to different pages)

{"0":{"title":"Test Form CLXXI","description":"","labelPlacement":"top_label","fields":[{"type":"text","id":1,"label":"Untitled","adminLabel":"","isRequired":true,"size":"medium","errorMessage":"","inputs":null,"formId":1563,"description":"","allowsPrepopulate":false,"inputMask":false,"inputMaskValue":"","inputType":"","labelPlacement":"","descriptionPlacement":"","subLabelPlacement":"","placeholder":"","cssClass":"","inputName":"","visibility":"visible","noDuplicates":false,"defaultValue":"","choices":"","conditionalLogic":"","failed_validation":"","productField":"","enablePasswordInput":"","maxLength":"","displayOnly":"","multipleFiles":false,"maxFiles":"","calculationFormula":"","calculationRounding":"","enableCalculation":"","disableQuantity":false,"displayAllCategories":false,"useRichTextEditor":false,"gwaclEnable":false,"advancedConditionalLogic":""},{"type":"select","id":2,"label":"Untitled","adminLabel":"","isRequired":false,"size":"medium","errorMessage":"","inputs":null,"choices":[{"text":"First Choice","value":"First Choice","isSelected":false,"price":""},{"text":"Second Choice","value":"Second Choice","isSelected":false,"price":""},{"text":"Third Choice","value":"Third Choice","isSelected":false,"price":""}],"formId":1563,"description":"","allowsPrepopulate":false,"inputMask":false,"inputMaskValue":"","inputType":"","labelPlacement":"","descriptionPlacement":"","subLabelPlacement":"","placeholder":"","cssClass":"","inputName":"","visibility":"","noDuplicates":false,"defaultValue":"","conditionalLogic":"","failed_validation":"","productField":"","enablePrice":"","displayOnly":""},{"type":"page","id":3,"label":"","adminLabel":"","isRequired":false,"size":"medium","errorMessage":"","inputs":null,"displayOnly":true,"nextButton":{"type":"text","text":"Next","imageUrl":""},"previousButton":{"type":"text","text":"Previous","imageUrl":""},"formId":1563,"description":"","allowsPrepopulate":false,"inputMask":false,"inputMaskValue":"","inputType":"","labelPlacement":"","descriptionPlacement":"","subLabelPlacement":"","placeholder":"","cssClass":"","inputName":"","visibility":"","noDuplicates":false,"defaultValue":"","choices":"","conditionalLogic":"","failed_validation":"","productField":""},{"type":"select","id":4,"label":"Untitled","adminLabel":"","isRequired":false,"size":"medium","errorMessage":"","inputs":null,"choices":[{"text":"First Choice","value":"First Choice","isSelected":false,"price":""},{"text":"Second Choice","value":"Second Choice","isSelected":false,"price":""},{"text":"Third Choice","value":"Third Choice","isSelected":false,"price":""}],"formId":1563,"description":"","allowsPrepopulate":false,"inputMask":false,"inputMaskValue":"","inputType":"","labelPlacement":"","descriptionPlacement":"","subLabelPlacement":"","placeholder":"","cssClass":"","inputName":"","visibility":"","noDuplicates":false,"defaultValue":"","conditionalLogic":"","failed_validation":"","productField":"","enablePrice":"","displayOnly":""},{"type":"page","id":5,"label":"","adminLabel":"","isRequired":false,"size":"medium","errorMessage":"","inputs":null,"displayOnly":true,"nextButton":{"type":"text","text":"Next","imageUrl":""},"previousButton":{"type":"text","text":"Previous","imageUrl":""},"formId":1563,"description":"","allowsPrepopulate":false,"inputMask":false,"inputMaskValue":"","inputType":"","labelPlacement":"","descriptionPlacement":"","subLabelPlacement":"","placeholder":"","cssClass":"","inputName":"","visibility":"","noDuplicates":false,"defaultValue":"","choices":"","conditionalLogic":"","failed_validation":"","productField":""},{"type":"select","id":6,"label":"Untitled","adminLabel":"","isRequired":false,"size":"medium","errorMessage":"","inputs":null,"choices":[{"text":"First Choice","value":"First Choice","isSelected":false,"price":""},{"text":"Second Choice","value":"Second Choice","isSelected":false,"price":""},{"text":"Third Choice","value":"Third Choice","isSelected":false,"price":""}],"formId":1563,"description":"","allowsPrepopulate":false,"inputMask":false,"inputMaskValue":"","inputType":"","labelPlacement":"","descriptionPlacement":"","subLabelPlacement":"","placeholder":"","cssClass":"","inputName":"","visibility":"","noDuplicates":false,"defaultValue":"","conditionalLogic":"","failed_validation":"","productField":"","enablePrice":"","displayOnly":""}],"button":{"type":"text","text":"Submit","imageUrl":""},"descriptionPlacement":"below","version":"2.1.2.4","id":1563,"useCurrentUserAsAuthor":true,"postContentTemplateEnabled":false,"postTitleTemplateEnabled":false,"postTitleTemplate":"","postContentTemplate":"","lastPageButton":{"type":"text","text":"Previous","imageUrl":""},"pagination":{"type":"percentage","pages":["","",""],"style":"blue","backgroundColor":null,"color":null,"display_progressbar_on_confirmation":false,"progressbar_completion_text":null,"gp-multi-page-navigation_enable":false,"gp-multi-page-navigation_activation_type":"manual"},"firstPageCssClass":"","confirmations":[{"id":"5882405328bd7","name":"Default Confirmation","isDefault":true,"type":"message","message":"Thanks for contacting us! We will get in touch with you shortly.","url":"","pageId":"","queryString":""}],"notifications":[{"id":"5882405328450","to":"{admin_email}","name":"Admin Notification","event":"form_submission","toType":"email","subject":"New submission from {form_title}","message":"{all_fields}"}]},"version":"2.1.2.4"}
<?php

add_filter( 'gform_pre_render_1563',     'move_my_field_please' );
add_filter( 'gform_pre_validation_1563', 'move_my_field_please' );
function move_my_field_please( $form ) {
    global $_moved_my_field;

	$source_field_id = 1;
	$target_field_id = 4;
	$before_after    = 'after';

	/* no need to change anything below */

	if( $_moved_my_field ) {
		return $form;
	}

	$form = gw_move_field( $form, $source_field_id, $target_field_id, $before_after );

	return $form;
}
<?php

function gw_move_field( $form, $source_field_id, $target_field_id, $before_after = 'after' ) {

	$source_field = $target_field = $target_index = null;

	for( $i = count( $form['fields'] ) - 1; $i >= 0; $i-- ) {

		if( $form['fields'][ $i ]->id == $source_field_id ) {
			$source_field = $form['fields'][ $i ];
			array_splice( $form['fields'], $i, 1 );
		}

		if( $form['fields'][ $i ]->id == $target_field_id ) {
			$target_index = $i + ( $before_after == 'before' ? -1 : 0 );
			$target_field = $form['fields'][ $i ];
		}

		if( $source_field !== null && $target_index !== null ) {
			$source_field->pageNumber = $target_field->pageNumber;
			array_splice( $form['fields'], $target_index, 0, array( $source_field ) );
			break;
		}

	}

	return $form;
}