Skip to content

Commit 3002eb5

Browse files
committed
Fix repeaters inside blocks
1 parent a809d42 commit 3002eb5

File tree

3 files changed

+42
-15
lines changed

3 files changed

+42
-15
lines changed

frontend/js/utils/getFormData.js

+4-3
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

+16-9
Original file line numberDiff line numberDiff line change
@@ -273,22 +273,29 @@ 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

291-
foreach ($parentBlockFields['blocks'] as $index => $childBlock) {
298+
foreach ($parentBlockFields['blocks'] ?? [] as $index => $childBlock) {
292299
$childBlock = $this->buildBlock($childBlock, $object, $childBlock['is_repeater'] ?? false);
293300
$this->validateBlockArray($childBlock, $childBlock['instance'], true);
294301
$childBlock['child_key'] = $childBlock['child_key'] ?? Str::afterLast($childBlock['editor_name'] ?? $index, '|');
@@ -434,8 +441,8 @@ function ($files, $role) use ($locale, $block) {
434441

435442

436443
foreach (['Fields', 'Medias', 'Files', 'Browsers'] as $fieldKey) {
437-
if ($fields['blocks'.$fieldKey] ?? false) {
438-
$fields['blocks'.$fieldKey] = call_user_func_array('array_merge', $fields['blocks'.$fieldKey] ?? []);
444+
if ($fields['blocks' . $fieldKey] ?? false) {
445+
$fields['blocks' . $fieldKey] = call_user_func_array('array_merge', $fields['blocks' . $fieldKey] ?? []);
439446
}
440447
}
441448
}

src/Repositories/Behaviors/HandleRepeaters.php

+22-3
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,9 +514,9 @@ 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) {
499-
if (!empty($relatedItemFormFields['blocks'.$fieldKey])) {
500-
$fields['blocks'.$fieldKey] = array_merge($fields['blocks'.$fieldKey] ?? [], $relatedItemFormFields['blocks'.$fieldKey]);
517+
foreach (['Fields', 'Medias', 'Files', 'Browsers', 'Repeaters'] as $fieldKey) {
518+
if (!empty($relatedItemFormFields['blocks' . $fieldKey])) {
519+
$fields['blocks' . $fieldKey] = array_merge($fields['blocks' . $fieldKey] ?? [], $relatedItemFormFields['blocks' . $fieldKey]);
501520
}
502521
}
503522

0 commit comments

Comments
 (0)