@@ -112,6 +112,7 @@ protected function mutateBeforeSave(array $data): array
112112 }
113113
114114 $ builderBlocks = $ this ->extractBuilderBlocks ($ values );
115+
115116 $ allFields = $ this ->getAllFieldsIncludingBuilderFields ($ builderBlocks );
116117
117118 return $ this ->mutateFormData ($ data , $ allFields , function ($ field , $ fieldConfig , $ fieldInstance , $ data ) use ($ builderBlocks ) {
@@ -487,6 +488,8 @@ private function determineFieldLocation(Model $field, array $builderBlocks): arr
487488 return [
488489 'isInBuilder ' => true ,
489490 'builderData ' => $ block ['data ' ],
491+ 'builderUlid ' => $ builderUlid ,
492+ 'blockIndex ' => array_search ($ block , $ builderBlocks ),
490493 ];
491494 }
492495 }
@@ -496,6 +499,8 @@ private function determineFieldLocation(Model $field, array $builderBlocks): arr
496499 return [
497500 'isInBuilder ' => false ,
498501 'builderData ' => null ,
502+ 'builderUlid ' => null ,
503+ 'blockIndex ' => null ,
499504 ];
500505 }
501506
@@ -514,17 +519,25 @@ private function determineFieldLocation(Model $field, array $builderBlocks): arr
514519 */
515520 private function processBuilderFieldMutation (Model $ field , object $ fieldInstance , array $ data , array $ builderData , array $ builderBlocks ): array
516521 {
517- // Create a mock record with the builder data for the callback
518- $ mockRecord = $ this ->createMockRecordForBuilder ($ builderData );
522+ foreach ($ builderBlocks as $ builderUlid => &$ blocks ) {
523+ if (is_array ($ blocks )) {
524+ foreach ($ blocks as &$ block ) {
525+ if (isset ($ block ['data ' ]) && is_array ($ block ['data ' ]) && isset ($ block ['data ' ][$ field ->ulid ])) {
526+ // Create a mock record with the block data for the callback
527+ $ mockRecord = $ this ->createMockRecordForBuilder ($ block ['data ' ]);
519528
520- // Create a temporary data structure for the callback
521- $ tempData = [$ this ->record ->valueColumn => $ builderData ];
522- $ tempData = $ fieldInstance ->mutateBeforeSaveCallback ($ mockRecord , $ field , $ tempData );
529+ // Create a temporary data structure for the callback
530+ $ tempData = [$ this ->record ->valueColumn => $ block [ ' data ' ] ];
531+ $ tempData = $ fieldInstance ->mutateBeforeSaveCallback ($ mockRecord , $ field , $ tempData );
523532
524- // Update the original data structure with the mutated values
525- $ this ->updateBuilderBlocksWithMutatedData ($ builderBlocks , $ field , $ tempData );
533+ if (isset ($ tempData [$ this ->record ->valueColumn ][$ field ->ulid ])) {
534+ $ block ['data ' ][$ field ->ulid ] = $ tempData [$ this ->record ->valueColumn ][$ field ->ulid ];
535+ }
536+ }
537+ }
538+ }
539+ }
526540
527- // Update the main data structure
528541 $ data [$ this ->record ->valueColumn ] = array_merge ($ data [$ this ->record ->valueColumn ], $ builderBlocks );
529542
530543 return $ data ;
0 commit comments