Skip to content

Commit f5cd761

Browse files
author
Mike van den Hoek
committed
(fix): prefill children information
1 parent 3bfebc5 commit f5cd761

1 file changed

Lines changed: 9 additions & 5 deletions

File tree

src/PrefillGravityForms/Controllers/BaseController.php

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ abstract class BaseController
2626

2727
protected GravityFormsSettings $settings;
2828
protected TeamsLogger $teams;
29-
protected int $numberOfChildrenPrefilled = 0;
29+
protected array $prefilledChildrenMappingOptions = [];
3030

3131
public function __construct()
3232
{
@@ -122,15 +122,19 @@ protected function replaceChildIndexPlaceholder(string $linkedMappingOption): st
122122
return $linkedMappingOption;
123123
}
124124

125+
// Store the children mapping option used to keep track of the number of times a mapping option is used.
126+
$this->prefilledChildrenMappingOptions[] = $linkedMappingOption;
127+
128+
$timesMappingOptionIsUsed = count(array_filter($this->prefilledChildrenMappingOptions, function ($field) use ($linkedMappingOption) {
129+
return $field === $linkedMappingOption;
130+
}));
131+
125132
$linkedMappingOption = str_replace(
126133
'kinderen.*',
127-
sprintf('kinderen.%d', $this->numberOfChildrenPrefilled),
134+
sprintf('kinderen.%d', $timesMappingOptionIsUsed ? $timesMappingOptionIsUsed - 1 : 0),
128135
$linkedMappingOption
129136
);
130137

131-
// Increment the counter to ensure the next child gets a unique index.
132-
$this->numberOfChildrenPrefilled++;
133-
134138
return $linkedMappingOption;
135139
}
136140

0 commit comments

Comments
 (0)