Skip to content

Commit

Permalink
Fix repeaters inside blocks
Browse files Browse the repository at this point in the history
  • Loading branch information
Tofandel committed Feb 28, 2024
1 parent a809d42 commit 3002eb5
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 15 deletions.
7 changes: 4 additions & 3 deletions frontend/js/utils/getFormData.js
Original file line number Diff line number Diff line change
Expand Up @@ -74,16 +74,17 @@ export const buildBlock = (block, rootState, isRepeater = false, childKey) => {

const base = {
id: block.id,
editor_name: block.name,
type: block.type,
medias: gatherSelected(rootState.mediaLibrary.selected, block),
browsers: gatherSelected(rootState.browser.selected, block),
content,
// gather repeater blocks from the repeater store module
blocks,
repeaters,
}
return isRepeater
? { ...content, ...base, is_repeater: true, repeater_target_id: block.repeater_target_id}
: { ...base, type: block.type, content, child_key: childKey }
? { ...base, is_repeater: true, repeater_target_id: block.repeater_target_id}
: { ...base, editor_name: block.name, child_key: childKey }
}

export const isBlockEmpty = (blockData) => {
Expand Down
25 changes: 16 additions & 9 deletions src/Repositories/Behaviors/HandleBlocks.php
Original file line number Diff line number Diff line change
Expand Up @@ -273,22 +273,29 @@ private function getChildBlocks($object, $parentBlockFields)
{
$childBlocksList = Collection::make();

if (empty($parentBlockFields['blocks'])) {
return $childBlocksList;
if (!empty($parentBlockFields['blocks'])) {
// Fallback if frontend or revision is still on the old schema
if (is_int(key(current($parentBlockFields['blocks'])))) {
foreach ($parentBlockFields['blocks'] as $childKey => $childBlocks) {
foreach ($childBlocks as $childBlock) {
$childBlock['child_key'] = $childKey;
$parentBlockFields['blocks'][] = $childBlock;
}
unset($parentBlockFields['blocks'][$childKey]);
}
}
}

// Fallback if frontend or revision is still on the old schema
if (is_int(key(current($parentBlockFields['blocks'])))) {
foreach ($parentBlockFields['blocks'] as $childKey => $childBlocks) {
if (!empty($parentBlockFields['repeaters'])) {
foreach ($parentBlockFields['repeaters'] as $childKey => $childBlocks) {
foreach ($childBlocks as $childBlock) {
$childBlock['child_key'] = $childKey;
$parentBlockFields['blocks'][] = $childBlock;

Check warning on line 293 in src/Repositories/Behaviors/HandleBlocks.php

View check run for this annotation

Codecov / codecov/patch

src/Repositories/Behaviors/HandleBlocks.php#L290-L293

Added lines #L290 - L293 were not covered by tests
}
unset($parentBlockFields['blocks'][$childKey]);
}
}

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


foreach (['Fields', 'Medias', 'Files', 'Browsers'] as $fieldKey) {
if ($fields['blocks'.$fieldKey] ?? false) {
$fields['blocks'.$fieldKey] = call_user_func_array('array_merge', $fields['blocks'.$fieldKey] ?? []);
if ($fields['blocks' . $fieldKey] ?? false) {
$fields['blocks' . $fieldKey] = call_user_func_array('array_merge', $fields['blocks' . $fieldKey] ?? []);
}
}
}
Expand Down
25 changes: 22 additions & 3 deletions src/Repositories/Behaviors/HandleRepeaters.php
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,12 @@ public function updateRepeaterMorphMany(
$relationField[$morphFieldId] = $object->id;
$relationField[$morphFieldType] = $object->getMorphClass();

if (is_array($relationField['content'] ?? null)) {
$content = $relationField['content'];
unset($relationField['content']);
$relationField = array_merge($content, $relationField);

Check warning on line 137 in src/Repositories/Behaviors/HandleRepeaters.php

View check run for this annotation

Codecov / codecov/patch

src/Repositories/Behaviors/HandleRepeaters.php#L134-L137

Added lines #L134 - L137 were not covered by tests
}

if (isset($relationField['id']) && Str::startsWith($relationField['id'], $relation)) {
// row already exists, let's update
$id = str_replace($relation . '-', '', $relationField['id']);
Expand Down Expand Up @@ -210,6 +216,12 @@ public function updateRepeaterWithPivot(
}
}

if (is_array($relationField['content'] ?? null)) {
$content = $relationField['content'];
unset($relationField['content']);
$relationField = array_merge($content, $relationField);

Check warning on line 222 in src/Repositories/Behaviors/HandleRepeaters.php

View check run for this annotation

Codecov / codecov/patch

src/Repositories/Behaviors/HandleRepeaters.php#L220-L222

Added lines #L220 - L222 were not covered by tests
}

if (isset($relationField['id']) && Str::startsWith($relationField['id'], $relation)) {
// row already exists, let's update, the $id is the id in the pivot table.
$pivotRowId = str_replace($relation . '-', '', $relationField['id']);
Expand Down Expand Up @@ -307,6 +319,7 @@ public function updateRepeater(

foreach ($relationFields as $index => $relationField) {
$relationField['position'] = $index + 1;

// If the relation is not an "existing" one try to match it with our session.
if (
! Str::startsWith($relationField['id'], $relation) &&
Expand All @@ -315,6 +328,12 @@ public function updateRepeater(
$relationField['id'] = $relation . '-' . $id;
}

if (is_array($relationField['content'] ?? null)) {
$content = $relationField['content'];
unset($relationField['content']);
$relationField = array_merge($content, $relationField);

Check warning on line 334 in src/Repositories/Behaviors/HandleRepeaters.php

View check run for this annotation

Codecov / codecov/patch

src/Repositories/Behaviors/HandleRepeaters.php#L331-L334

Added lines #L331 - L334 were not covered by tests
}

// Set the active data based on the parent.
if (! isset($relationField['languages']) && isset($relationField['active'])) {
foreach (array_keys($relationField['active']) as $langCode) {
Expand Down Expand Up @@ -495,9 +514,9 @@ function ($files, $role) use ($locale, $relation, $relationItem) {
foreach ($relatedItemFormFields['blocks'] ?? [] as $key => $block) {
$fields['blocks'][str_contains($key, '|') ? $key : "blocks-$relation-{$relationItem->id}|$key"] = $block;

Check warning on line 515 in src/Repositories/Behaviors/HandleRepeaters.php

View check run for this annotation

Codecov / codecov/patch

src/Repositories/Behaviors/HandleRepeaters.php#L514-L515

Added lines #L514 - L515 were not covered by tests
}
foreach (['Fields', 'Medias', 'Files', 'Browsers'] as $fieldKey) {
if (!empty($relatedItemFormFields['blocks'.$fieldKey])) {
$fields['blocks'.$fieldKey] = array_merge($fields['blocks'.$fieldKey] ?? [], $relatedItemFormFields['blocks'.$fieldKey]);
foreach (['Fields', 'Medias', 'Files', 'Browsers', 'Repeaters'] as $fieldKey) {
if (!empty($relatedItemFormFields['blocks' . $fieldKey])) {
$fields['blocks' . $fieldKey] = array_merge($fields['blocks' . $fieldKey] ?? [], $relatedItemFormFields['blocks' . $fieldKey]);

Check warning on line 519 in src/Repositories/Behaviors/HandleRepeaters.php

View check run for this annotation

Codecov / codecov/patch

src/Repositories/Behaviors/HandleRepeaters.php#L517-L519

Added lines #L517 - L519 were not covered by tests
}
}

Expand Down

0 comments on commit 3002eb5

Please sign in to comment.