Skip to content

Commit 49ecfad

Browse files
committed
Fix all BC breaks
1 parent 3002eb5 commit 49ecfad

File tree

4 files changed

+47
-154
lines changed

4 files changed

+47
-154
lines changed

frontend/js/utils/getFormData.js

Lines changed: 25 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -31,17 +31,17 @@ export const stripOutBlockNamespace = (name, id) => {
3131
return nameWithoutBlock.match(/]/gi).length > 1 ? nameWithoutBlock.replace(']', '') : nameWithoutBlock.slice(0, -1)
3232
}
3333

34-
export const buildBlock = (block, rootState, isRepeater = false, childKey) => {
35-
const parentRepeaters = rootState.repeaters.repeaters;
36-
const repeaterIds = Object.keys(parentRepeaters);
34+
export const buildBlock = (block, rootState, isRepeater = false, isInsideRepeater = isRepeater) => {
35+
const repeaterIds = Object.keys(rootState.repeaters.repeaters);
3736
const prefix = 'blocks-' + block.id + '|';
3837
const repeaters = repeaterIds.filter(repeaterKey => {
3938
return repeaterKey.startsWith(prefix)
4039
})
4140
.reduce((acc, repeaterKey) => {
42-
acc[repeaterKey.replace(prefix, '')] = parentRepeaters[repeaterKey].map(repeaterItem => {
43-
return buildBlock(repeaterItem, rootState, true)
44-
})
41+
acc[repeaterKey.replace(prefix, '')] = rootState.repeaters.repeaters[repeaterKey]
42+
.map(repeaterItem => {
43+
return buildBlock(repeaterItem, rootState, true, isRepeater)
44+
})
4545

4646
return acc
4747
}, {})
@@ -50,41 +50,38 @@ export const buildBlock = (block, rootState, isRepeater = false, childKey) => {
5050
const blocks = blockIds.filter(blockKey => {
5151
return blockKey.startsWith(prefix)
5252
}).reduce((acc, blockKey) => {
53-
acc.push(...rootState.blocks.blocks[blockKey].map(repeaterItem => {
54-
if (isRepeater) {
55-
repeaterItem = {...repeaterItem, name: repeaterItem.name.replace(prefix, '')}
53+
const key = blockKey.replace(prefix, '');
54+
rootState.blocks.blocks[blockKey].forEach(blockItem => {
55+
const block = buildBlock(blockItem, rootState, false);
56+
if (isInsideRepeater) {
57+
acc.push(block);
58+
} else {
59+
if (!acc[key]) {
60+
acc[key] = [];
61+
}
62+
acc[key].push(block)
5663
}
57-
return buildBlock(repeaterItem, rootState, false, blockKey.replace(prefix, ''))
58-
}));
64+
})
5965
return acc;
60-
}, [])
66+
}, isInsideRepeater ? [] : {})
6167

6268
// retrieve all fields for this block and clean up field names
6369
const content = rootState.form.fields.filter((field) => {
6470
return isBlockField(field.name, block.id)
65-
}).map((field) => {
66-
return {
67-
name: stripOutBlockNamespace(field.name, block.id),
68-
value: field.value
69-
}
70-
}).reduce((content, field) => {
71-
content[field.name] = field.value
72-
return content
73-
}, {});
71+
}).reduce((acc, field) => {
72+
acc[stripOutBlockNamespace(field.name, block.id)] = field.value;
73+
return acc;
74+
}, {})
7475

7576
const base = {
7677
id: block.id,
77-
type: block.type,
7878
medias: gatherSelected(rootState.mediaLibrary.selected, block),
7979
browsers: gatherSelected(rootState.browser.selected, block),
80-
content,
8180
// gather repeater blocks from the repeater store module
82-
blocks,
83-
repeaters,
8481
}
85-
return isRepeater
86-
? { ...base, is_repeater: true, repeater_target_id: block.repeater_target_id}
87-
: { ...base, editor_name: block.name, child_key: childKey }
82+
return isInsideRepeater
83+
? { ...content, ...base, repeater_target_id: block.repeater_target_id, blocks, repeaters}
84+
: { ...base, content, is_repeater: isRepeater, type: block.type, editor_name: block.name, blocks: {...blocks, ...repeaters} }
8885
}
8986

9087
export const isBlockEmpty = (blockData) => {

src/Repositories/Behaviors/HandleBlocks.php

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

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-
}
276+
foreach ($parentBlockFields['blocks'] ?? [] as $childKey => $childBlocks) {
277+
if (str_contains($childKey, '|')) {
278+
continue;
286279
}
287-
}
288-
289-
if (!empty($parentBlockFields['repeaters'])) {
290-
foreach ($parentBlockFields['repeaters'] as $childKey => $childBlocks) {
291-
foreach ($childBlocks as $childBlock) {
292-
$childBlock['child_key'] = $childKey;
293-
$parentBlockFields['blocks'][] = $childBlock;
294-
}
280+
foreach ($childBlocks as $index => $childBlock) {
281+
$childBlock = $this->buildBlock($childBlock, $object, $childBlock['is_repeater'] ?? false);
282+
$this->validateBlockArray($childBlock, $childBlock['instance'], true);
283+
$childBlock['child_key'] = $childKey;
284+
$childBlock['position'] = $index + 1;
285+
$childBlock['editor_name'] = $parentBlockFields['editor_name'] ?? 'default';
286+
$childBlock['blocks'] = $this->getChildBlocks($object, $childBlock);
287+
288+
$childBlocksList->push($childBlock);
295289
}
296290
}
297291

298-
foreach ($parentBlockFields['blocks'] ?? [] as $index => $childBlock) {
299-
$childBlock = $this->buildBlock($childBlock, $object, $childBlock['is_repeater'] ?? false);
300-
$this->validateBlockArray($childBlock, $childBlock['instance'], true);
301-
$childBlock['child_key'] = $childBlock['child_key'] ?? Str::afterLast($childBlock['editor_name'] ?? $index, '|');
302-
$childBlock['position'] = $index + 1;
303-
$childBlock['editor_name'] = $parentBlockFields['editor_name'] ?? 'default';
304-
$childBlock['blocks'] = $this->getChildBlocks($object, $childBlock);
305-
306-
$childBlocksList->push($childBlock);
307-
}
308-
309292
return $childBlocksList;
310293
}
311294

@@ -482,7 +465,7 @@ protected function getBlockBrowsers($block)
482465
try {
483466
$relationRepository = $this->getModelRepository($relation);
484467
$relatedItems = $relationRepository->get([], ['id' => $ids], [], -1);
485-
} catch (\Throwable $th) {
468+
} catch (\Throwable) {
486469
$relatedItems = collect();
487470
}
488471
$sortedRelatedItems = array_flip($ids);
@@ -494,6 +477,7 @@ protected function getBlockBrowsers($block)
494477
$items = Collection::make(array_values($sortedRelatedItems))->filter(function ($value) {
495478
return is_object($value);
496479
})->map(function ($relatedElement) use ($relation) {
480+
// TODO this needs refactoring, it's duplicated from HandleBrowsers
497481
return [
498482
'id' => $relatedElement->id,
499483
'name' => $relatedElement->titleInBrowser ?? $relatedElement->title,
@@ -503,6 +487,7 @@ protected function getBlockBrowsers($block)
503487
'edit',
504488
$relatedElement->id
505489
),
490+
'endpointType' => $relatedElement->getMorphClass(),
506491
] + (classHasTrait($relatedElement, HasMedias::class) ? [
507492
'thumbnail' => $relatedElement->defaultCmsImage(['w' => 100, 'h' => 100]),
508493
] : []);

src/Repositories/Behaviors/HandleRepeaters.php

Lines changed: 0 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -131,12 +131,6 @@ 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-
140134
if (isset($relationField['id']) && Str::startsWith($relationField['id'], $relation)) {
141135
// row already exists, let's update
142136
$id = str_replace($relation . '-', '', $relationField['id']);
@@ -216,12 +210,6 @@ public function updateRepeaterWithPivot(
216210
}
217211
}
218212

219-
if (is_array($relationField['content'] ?? null)) {
220-
$content = $relationField['content'];
221-
unset($relationField['content']);
222-
$relationField = array_merge($content, $relationField);
223-
}
224-
225213
if (isset($relationField['id']) && Str::startsWith($relationField['id'], $relation)) {
226214
// row already exists, let's update, the $id is the id in the pivot table.
227215
$pivotRowId = str_replace($relation . '-', '', $relationField['id']);
@@ -328,12 +316,6 @@ public function updateRepeater(
328316
$relationField['id'] = $relation . '-' . $id;
329317
}
330318

331-
if (is_array($relationField['content'] ?? null)) {
332-
$content = $relationField['content'];
333-
unset($relationField['content']);
334-
$relationField = array_merge($content, $relationField);
335-
}
336-
337319
// Set the active data based on the parent.
338320
if (! isset($relationField['languages']) && isset($relationField['active'])) {
339321
foreach (array_keys($relationField['active']) as $langCode) {

tests/integration/Blocks/BlockChildrenTest.php

Lines changed: 7 additions & 78 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88

99
class BlockChildrenTest extends TestCase
1010
{
11-
public function testBCOfBlockSchema(): void
11+
public function testSorting(): void
1212
{
1313
$module = AnonymousModule::make('childrenservers', $this->app)
1414
->boot();
@@ -24,6 +24,7 @@ public function testBCOfBlockSchema(): void
2424
$blocks = [
2525
'blocks' => [
2626
[
27+
'editor_name' => 'description',
2728
'browsers' => [],
2829
'medias' => [],
2930
'blocks' => [
@@ -52,78 +53,6 @@ public function testBCOfBlockSchema(): void
5253
],
5354
'id' => time() + 2,
5455
],
55-
]
56-
],
57-
'type' => 'a17-block-quote',
58-
'content' => [
59-
'quote' => 'This is the quote.',
60-
'author' => 'This is the author.',
61-
],
62-
'id' => time(),
63-
],
64-
],
65-
];
66-
67-
$update = $repository->update($server->id, $blocks);
68-
69-
// Check the nested child order.
70-
$this->assertEquals(
71-
'This is the nested quote at position 2.',
72-
$update->blocks[0]->children[0]->content['quote']
73-
);
74-
$this->assertEquals(
75-
'This is the nested quote at position 1.',
76-
$update->blocks[0]->children[1]->content['quote']
77-
);
78-
}
79-
80-
public function testSorting(): void
81-
{
82-
$module = AnonymousModule::make('childrenservers', $this->app)
83-
->boot();
84-
85-
/** @var ModuleRepository $repository */
86-
$repository = app()->make($module->getRepositoryClassName());
87-
88-
$server = $repository->create([
89-
'title' => 'Hello world',
90-
'published' => true,
91-
]);
92-
93-
$blocks = [
94-
'blocks' => [
95-
[
96-
'editor_name' => 'description',
97-
'browsers' => [],
98-
'medias' => [],
99-
'blocks' => [
100-
[
101-
'browsers' => [],
102-
'medias' => [],
103-
'blocks' => [],
104-
'child_key' => 'content',
105-
'editor_name' => 'blocks-yada-1|content',
106-
'type' => 'a17-block-quote',
107-
'is_repeater' => false,
108-
'content' => [
109-
'quote' => 'This is the nested quote at position 2.',
110-
'author' => 'This is the nested author at position 2.',
111-
],
112-
'id' => time() + 1,
113-
],
114-
[
115-
'browsers' => [],
116-
'medias' => [],
117-
'blocks' => [],
118-
'child_key' => 'content',
119-
'editor_name' => 'blocks-yada-1|content',
120-
'type' => 'a17-block-quote',
121-
'is_repeater' => false,
122-
'content' => [
123-
'quote' => 'This is the nested quote at position 1.',
124-
'author' => 'This is the nested author at position 1.',
125-
],
126-
'id' => time() + 2,
12756
],
12857
],
12958
'type' => 'a17-block-quote',
@@ -150,14 +79,14 @@ public function testSorting(): void
15079

15180
// Now we update it a second time, but we update the order.
15281
$blocksUpdate = $blocks;
153-
$blocksUpdate['blocks'][0]['blocks'][0]['id'] = $update->blocks[1]->id;
154-
$blocksUpdate['blocks'][0]['blocks'][1]['id'] = $update->blocks[2]->id;
82+
$blocksUpdate['blocks'][0]['blocks'][0][0]['id'] = $update->blocks[1]->id;
83+
$blocksUpdate['blocks'][0]['blocks'][0][1]['id'] = $update->blocks[2]->id;
15584

15685
// We now swap them so their actual position is correct.
157-
$backup = $blocksUpdate['blocks'][0]['blocks'][0];
86+
$backup = $blocksUpdate['blocks'][0]['blocks'][0][0];
15887

159-
$blocksUpdate['blocks'][0]['blocks'][0] = $blocksUpdate['blocks'][0]['blocks'][1];
160-
$blocksUpdate['blocks'][0]['blocks'][1] = $backup;
88+
$blocksUpdate['blocks'][0]['blocks'][0][0] = $blocksUpdate['blocks'][0]['blocks'][0][1];
89+
$blocksUpdate['blocks'][0]['blocks'][0][1] = $backup;
16190

16291
$update = $repository->update($server->id, $blocksUpdate);
16392

0 commit comments

Comments
 (0)