Skip to content

Commit 50194e3

Browse files
committed
Fix repeaters inside blocks
1 parent a809d42 commit 50194e3

File tree

3 files changed

+37
-10
lines changed

3 files changed

+37
-10
lines changed

frontend/js/utils/getFormData.js

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -74,16 +74,17 @@ export const buildBlock = (block, rootState, isRepeater = false, childKey) => {
7474

7575
const base = {
7676
id: block.id,
77-
editor_name: block.name,
77+
type: block.type,
7878
medias: gatherSelected(rootState.mediaLibrary.selected, block),
7979
browsers: gatherSelected(rootState.browser.selected, block),
80+
content,
8081
// gather repeater blocks from the repeater store module
8182
blocks,
8283
repeaters,
8384
}
8485
return isRepeater
85-
? { ...content, ...base, is_repeater: true, repeater_target_id: block.repeater_target_id}
86-
: { ...base, type: block.type, content, child_key: childKey }
86+
? { ...base, is_repeater: true, repeater_target_id: block.repeater_target_id}
87+
: { ...base, editor_name: block.name, child_key: childKey }
8788
}
8889

8990
export const isBlockEmpty = (blockData) => {

src/Repositories/Behaviors/HandleBlocks.php

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -273,18 +273,25 @@ private function getChildBlocks($object, $parentBlockFields)
273273
{
274274
$childBlocksList = Collection::make();
275275

276-
if (empty($parentBlockFields['blocks'])) {
277-
return $childBlocksList;
276+
if (!empty($parentBlockFields['blocks'])) {
277+
// Fallback if frontend or revision is still on the old schema
278+
if (is_int(key(current($parentBlockFields['blocks'])))) {
279+
foreach ($parentBlockFields['blocks'] as $childKey => $childBlocks) {
280+
foreach ($childBlocks as $childBlock) {
281+
$childBlock['child_key'] = $childKey;
282+
$parentBlockFields['blocks'][] = $childBlock;
283+
}
284+
unset($parentBlockFields['blocks'][$childKey]);
285+
}
286+
}
278287
}
279288

280-
// Fallback if frontend or revision is still on the old schema
281-
if (is_int(key(current($parentBlockFields['blocks'])))) {
282-
foreach ($parentBlockFields['blocks'] as $childKey => $childBlocks) {
289+
if (!empty($parentBlockFields['repeaters'])) {
290+
foreach ($parentBlockFields['repeaters'] as $childKey => $childBlocks) {
283291
foreach ($childBlocks as $childBlock) {
284292
$childBlock['child_key'] = $childKey;
285293
$parentBlockFields['blocks'][] = $childBlock;
286294
}
287-
unset($parentBlockFields['blocks'][$childKey]);
288295
}
289296
}
290297

src/Repositories/Behaviors/HandleRepeaters.php

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -131,6 +131,12 @@ public function updateRepeaterMorphMany(
131131
$relationField[$morphFieldId] = $object->id;
132132
$relationField[$morphFieldType] = $object->getMorphClass();
133133

134+
if (is_array($relationField['content'] ?? null)) {
135+
$content = $relationField['content'];
136+
unset($relationField['content']);
137+
$relationField = array_merge($content, $relationField);
138+
}
139+
134140
if (isset($relationField['id']) && Str::startsWith($relationField['id'], $relation)) {
135141
// row already exists, let's update
136142
$id = str_replace($relation . '-', '', $relationField['id']);
@@ -210,6 +216,12 @@ public function updateRepeaterWithPivot(
210216
}
211217
}
212218

219+
if (is_array($relationField['content'] ?? null)) {
220+
$content = $relationField['content'];
221+
unset($relationField['content']);
222+
$relationField = array_merge($content, $relationField);
223+
}
224+
213225
if (isset($relationField['id']) && Str::startsWith($relationField['id'], $relation)) {
214226
// row already exists, let's update, the $id is the id in the pivot table.
215227
$pivotRowId = str_replace($relation . '-', '', $relationField['id']);
@@ -307,6 +319,7 @@ public function updateRepeater(
307319

308320
foreach ($relationFields as $index => $relationField) {
309321
$relationField['position'] = $index + 1;
322+
310323
// If the relation is not an "existing" one try to match it with our session.
311324
if (
312325
! Str::startsWith($relationField['id'], $relation) &&
@@ -315,6 +328,12 @@ public function updateRepeater(
315328
$relationField['id'] = $relation . '-' . $id;
316329
}
317330

331+
if (is_array($relationField['content'] ?? null)) {
332+
$content = $relationField['content'];
333+
unset($relationField['content']);
334+
$relationField = array_merge($content, $relationField);
335+
}
336+
318337
// Set the active data based on the parent.
319338
if (! isset($relationField['languages']) && isset($relationField['active'])) {
320339
foreach (array_keys($relationField['active']) as $langCode) {
@@ -495,7 +514,7 @@ function ($files, $role) use ($locale, $relation, $relationItem) {
495514
foreach ($relatedItemFormFields['blocks'] ?? [] as $key => $block) {
496515
$fields['blocks'][str_contains($key, '|') ? $key : "blocks-$relation-{$relationItem->id}|$key"] = $block;
497516
}
498-
foreach (['Fields', 'Medias', 'Files', 'Browsers'] as $fieldKey) {
517+
foreach (['Fields', 'Medias', 'Files', 'Browsers', 'Repeaters'] as $fieldKey) {
499518
if (!empty($relatedItemFormFields['blocks'.$fieldKey])) {
500519
$fields['blocks'.$fieldKey] = array_merge($fields['blocks'.$fieldKey] ?? [], $relatedItemFormFields['blocks'.$fieldKey]);
501520
}

0 commit comments

Comments
 (0)