Skip to content
37 changes: 35 additions & 2 deletions classes/helpers/FrmFieldGridHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,24 @@ class FrmFieldGridHelper {
*/
private $section_is_open = false;

/**
* True if the first field in the row had frm_first, indicating an intentional group.
*
* Example: Two fields grouped in one row.
* - First field has frm_first → is_group_start = true.
* - Second field has no frm_first, but is_group_start is true → stays in the same row.
*
* @var bool
*/
private $is_group_start = false;

/**
* True if the first field in the row was a section (divider).
*
* @var bool
*/
private $is_section_start = false;

/**
* @param bool $nested
*/
Expand Down Expand Up @@ -155,14 +173,27 @@ public function maybe_begin_field_wrapper() {
* @return bool
*/
private function should_first_close_the_active_field_wrapper() {
if ( false === $this->parent_li || ! empty( $this->section_helper ) ) {
if ( false === $this->parent_li ) {
return false;
}

// Handle sections (dividers) before section_helper check.
// section_helper is created in set_field() before this runs.
// @see https://github.com/Strategy11/formidable-pro/issues/3820
if ( 'divider' === $this->field->type && ! $this->section_is_open ) {
return $this->current_list_size + $this->section_size > 12 || $this->is_frm_first || ! $this->is_group_start;
}

// When a section is open, let section_helper handle field grouping.
if ( ! empty( $this->section_helper ) ) {
return false;
}

if ( 'end_divider' === $this->field->type ) {
return false;
}
return ! $this->can_support_current_layout() || $this->is_frm_first;

return ! $this->can_support_current_layout() || $this->is_frm_first || ( $this->is_section_start && ! $this->is_group_start );
}

/**
Expand All @@ -173,6 +204,8 @@ private function begin_field_wrapper() {
$this->parent_li = true;
$this->current_list_size = 0;
$this->current_field_count = 0;
$this->is_group_start = $this->is_frm_first;
$this->is_section_start = 'divider' === $this->field->type;
}

/**
Expand Down
Loading