From 6278531fbfccf090a0b4aed95b38502204ab2758 Mon Sep 17 00:00:00 2001 From: Ivan Bereznev Date: Wed, 31 Aug 2022 18:58:42 +0300 Subject: [PATCH] #1184 Different Matrix Block Types are merged together --- src/fields/Matrix.php | 14 ++++++++++++-- 1 file changed, 12 insertions(+), 2 deletions(-) diff --git a/src/fields/Matrix.php b/src/fields/Matrix.php index 06eaf7dd..0e38fd7c 100644 --- a/src/fields/Matrix.php +++ b/src/fields/Matrix.php @@ -139,8 +139,9 @@ public function parseField(): mixed // sub-field data that could be arrays or single values. Let's build our Matrix-ready data foreach ($fieldData as $blockSubFieldHandle => $value) { $handles = explode('.', $blockSubFieldHandle); - $blockIndex = 'new' . ($handles[0] + 1); $blockHandle = $handles[1]; + // Inclusion of block handle here prevents blocks of different types from being merged together + $blockIndex = 'new' . $blockHandle . ($handles[0] + 1); $subFieldHandle = $handles[2]; $disabled = Hash::get($this->fieldInfo, 'blocks.' . $blockHandle . '.disabled', false); @@ -155,7 +156,16 @@ public function parseField(): mixed // $order++; } - return Hash::expand($preppedData); + $expanded = Hash::expand($preppedData); + + // Although it seems to work with block handles in keys, it's better to keep things clean + $index = 1; + $resultBlocks = []; + foreach ($expanded as $blockData) { + $resultBlocks['new' . $index++] = $blockData; + } + + return $resultBlocks; }