File tree Expand file tree Collapse file tree
src/PrefillGravityForms/Controllers Expand file tree Collapse file tree Original file line number Diff line number Diff 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
You can’t perform that action at this time.
0 commit comments