Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 1 addition & 9 deletions classes/helpers/FrmFieldsHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,15 +43,7 @@ public static function setup_new_vars( $type = '', $form_id = '' ) {
}

// Increase the field order of submit field and fields in the same row.
$last_row_field_ids = FrmAppHelper::get_post_param( 'last_row_field_ids', array() );
if ( $last_row_field_ids ) {
foreach ( $last_row_field_ids as $index => $last_row_field_id ) {
FrmField::update(
$last_row_field_id,
array( 'field_order' => $field_count + $index + 2 )
);
}
}
FrmSubmitHelper::update_last_row_fields_order_when_adding_field( $field_count );

return $values;
}
Expand Down
54 changes: 54 additions & 0 deletions classes/helpers/FrmSubmitHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,13 @@ class FrmSubmitHelper {
*/
const DEFAULT_ORDER = 9999;

/**
* Track the new order of last row fields after a new field is added.
*
* @var array Keys are field IDs, values are the order.
*/
private static $last_row_fields_order = array();

/**
* Gets submit field object.
*
Expand Down Expand Up @@ -192,4 +199,51 @@ public static function only_contains_submit_field( $fields ) {
}
return $submit_field;
}

/**
* Updates fields in the last row when new field is added.
*
* @since x.x
*
* @param int $field_count The current field count.
*/
public static function update_last_row_fields_order_when_adding_field( $field_count ) {
$last_row_field_ids = FrmAppHelper::get_post_param( 'last_row_field_ids', array() );
if ( ! is_array( $last_row_field_ids ) || empty( $last_row_field_ids ) ) {
return;
}
foreach ( $last_row_field_ids as $index => $last_row_field_id ) {
$last_row_field_id = absint( $last_row_field_id );
if ( ! $last_row_field_id ) {
continue;
}
// Plus 2 here because the new field has that plus 1.
$new_order = $field_count + $index + 2;
$updated = FrmField::update(
$last_row_field_id,
array( 'field_order' => $new_order )
);
if ( false !== $updated ) {
self::$last_row_fields_order[ $last_row_field_id ] = $new_order;
}
}
}

/**
* Prints the hidden input that contains the last row fields order to be processed in JS after adding new field.
*
* @since x.x
*/
public static function print_last_row_fields_order_input() {
if ( ! self::$last_row_fields_order ) {
return;
}

printf(
'<input id="frm-last-row-fields-order" type="hidden" value="%s" />',
esc_attr( wp_json_encode( self::$last_row_fields_order ) )
);

self::$last_row_fields_order = array();
}
Comment thread
Crabcyborg marked this conversation as resolved.
}
6 changes: 5 additions & 1 deletion classes/views/frm-forms/add_field.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,11 @@
<div class="divider_section_only">
<?php } ?>

<?php do_action( 'frm_extra_field_actions', $field['id'] ); ?>
<?php
FrmSubmitHelper::print_last_row_fields_order_input();

do_action( 'frm_extra_field_actions', $field['id'] );
?>

<div id="field_<?php echo esc_attr( $field['id'] ); ?>_inner_container" class="frm_inner_field_container">
<div class="frm-field-action-icons frm-show-hover">
Expand Down
2 changes: 1 addition & 1 deletion css/admin/frm-settings-components.css

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion css/frm_testing_mode.css

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion js/addons-page.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion js/form-templates.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion js/formidable-settings-components.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion js/formidable_admin.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion js/formidable_blocks.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion js/formidable_dashboard.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion js/formidable_overlay.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion js/formidable_styles.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion js/frm_testing_mode.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion js/onboarding-wizard.js

Large diffs are not rendered by default.

31 changes: 28 additions & 3 deletions js/src/admin/admin.js
Original file line number Diff line number Diff line change
Expand Up @@ -1615,15 +1615,22 @@ window.frmAdminBuildJS = function() {
* @return {Object}
*/
function getInsertNewFieldArgs( fieldType, sectionId, formId, hasBreak ) {
return {
const fieldArgs = {
action: 'frm_insert_field',
form_id: formId,
field_type: fieldType,
section_id: sectionId,
nonce: frmGlobal.nonce,
has_break: hasBreak,
last_row_field_ids: getFieldIdsInSubmitRow()
has_break: hasBreak
};

// Only send last row field IDs to update their order if this field isn't added to a repeater.
const isInRepeater = sectionId > 0 && document.getElementById( 'form_id' ).value !== formId;
if ( ! isInRepeater ) {
fieldArgs.last_row_field_ids = getFieldIdsInSubmitRow();
}

return fieldArgs;
}
Comment thread
Crabcyborg marked this conversation as resolved.

/**
Expand Down Expand Up @@ -2723,6 +2730,11 @@ window.frmAdminBuildJS = function() {
field.classList.remove( 'frm-newly-added' );
}, 1000 );

const lastRowOrderInput = field.querySelector( '#frm-last-row-fields-order' );
if ( lastRowOrderInput ) {
updateLastRowFieldsOrder( JSON.parse( lastRowOrderInput.value ) );
}

Comment thread
Crabcyborg marked this conversation as resolved.
if ( addFocus ) {
const bounding = field.getBoundingClientRect(),
container = document.getElementById( 'post-body-content' ),
Expand Down Expand Up @@ -2758,6 +2770,19 @@ window.frmAdminBuildJS = function() {
document.dispatchEvent( addedEvent );
}

function updateLastRowFieldsOrder( fieldsOrder ) {
if ( ! fieldsOrder || 'object' !== typeof fieldsOrder ) {
return;
}

Object.keys( fieldsOrder ).forEach( fieldId => {
const orderInput = document.querySelector( 'input[name="field_options[field_order_' + fieldId + ']"]' );
if ( orderInput ) {
orderInput.value = fieldsOrder[ fieldId ];
}
} );
}

/**
* Since multiple new fields may get added when a new field is inserted, check the HTML.
*
Expand Down