Skip to content

Commit aaaea5f

Browse files
Fix replicator set handling (#88)
* Fix replicator set handling * Fix styling --------- Co-authored-by: duncanmcclean <duncanmcclean@users.noreply.github.com>
1 parent ce7a831 commit aaaea5f

File tree

2 files changed

+253
-13
lines changed

2 files changed

+253
-13
lines changed

src/Http/Controllers/GuestEntryController.php

Lines changed: 19 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -203,12 +203,25 @@ protected function processField($entry, Field $field, $key, $value, $request): m
203203
return $fieldHandle === 'type';
204204
})
205205
->map(function ($value, $fieldHandle) use ($entry, $replicatorField, $index, $set, $request) {
206-
$field = collect($set['fields'])
207-
->where('handle', $fieldHandle)
208-
->map(function ($field) {
209-
return new Field($field['handle'], $field['field']);
210-
})
211-
->first();
206+
// Handle sets stored in the legacy format...
207+
if (isset($set['fields'])) {
208+
$field = collect($set['fields'])
209+
->where('handle', $fieldHandle)
210+
->map(function ($field) {
211+
return new Field($field['handle'], $field['field']);
212+
})
213+
->first();
214+
} else {
215+
$field = collect($set['sets'])
216+
->flatMap(function ($tab) {
217+
return $tab['fields'];
218+
})
219+
->where('handle', $fieldHandle)
220+
->map(function ($field) {
221+
return new Field($field['handle'], $field['field']);
222+
})
223+
->first();
224+
}
212225

213226
if (! $field) {
214227
return $value;

tests/Http/Controllers/GuestEntryControllerTest.php

Lines changed: 234 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -83,8 +83,10 @@
8383

8484
$this->assertNotNull($entry);
8585
$this->assertSame($entry->collectionHandle(), 'comments');
86-
$this->assertSame($entry->get('title'), 'BLAH So, I was sitting there and somebody came up to me and I asked them something.');
87-
$this->assertSame($entry->slug(), 'blah-so-i-was-sitting-there-and-somebody-came-up-to-me-and-i-asked-them-something');
86+
$this->assertSame($entry->get('title'),
87+
'BLAH So, I was sitting there and somebody came up to me and I asked them something.');
88+
$this->assertSame($entry->slug(),
89+
'blah-so-i-was-sitting-there-and-somebody-came-up-to-me-and-i-asked-them-something');
8890
});
8991

9092
it('can store entry with duplicate slug', function () {
@@ -117,9 +119,11 @@
117119

118120
$tree->tree([
119121
['entry' => 'one'],
120-
['entry' => 'two', 'children' => [
121-
['entry' => 'fantastic-one'],
122-
]],
122+
[
123+
'entry' => 'two', 'children' => [
124+
['entry' => 'fantastic-one'],
125+
],
126+
],
123127
])->save(0);
124128

125129
$this
@@ -736,7 +740,8 @@
736740
'_collection' => encrypt('comments'),
737741
'title' => 'This is great',
738742
'slug' => 'this-is-great',
739-
'attachment' => UploadedFile::fake()->createWithContent('foobar.svg', '<?xml version="1.0" encoding="UTF-8" standalone="no"?><svg xmlns="http://www.w3.org/2000/svg" width="500" height="500"><script type="text/javascript">alert(`Bad stuff could go in here.`);</script></svg>'),
743+
'attachment' => UploadedFile::fake()->createWithContent('foobar.svg',
744+
'<?xml version="1.0" encoding="UTF-8" standalone="no"?><svg xmlns="http://www.w3.org/2000/svg" width="500" height="500"><script type="text/javascript">alert(`Bad stuff could go in here.`);</script></svg>'),
740745
])
741746
->assertRedirect();
742747

@@ -1893,7 +1898,8 @@
18931898
'_collection' => encrypt('albums'),
18941899
'_id' => encrypt('allo-mate-idee'),
18951900
'record_label' => 'Unknown',
1896-
'attachment' => UploadedFile::fake()->createWithContent('foobar.svg', '<?xml version="1.0" encoding="UTF-8" standalone="no"?><svg xmlns="http://www.w3.org/2000/svg" width="500" height="500"><script type="text/javascript">alert(`Bad stuff could go in here.`);</script></svg>'),
1901+
'attachment' => UploadedFile::fake()->createWithContent('foobar.svg',
1902+
'<?xml version="1.0" encoding="UTF-8" standalone="no"?><svg xmlns="http://www.w3.org/2000/svg" width="500" height="500"><script type="text/javascript">alert(`Bad stuff could go in here.`);</script></svg>'),
18971903
])
18981904
->assertRedirect();
18991905

@@ -2399,6 +2405,227 @@
23992405
$this->assertCount(2, $entry->get('things'));
24002406
});
24012407

2408+
it('can update entry with replicator field in the newish way', function () {
2409+
Blueprint::make('albums')
2410+
->setNamespace('collections.albums')
2411+
->setContents([
2412+
'tabs' => [
2413+
'main' => [
2414+
'display' => 'Hauptteil',
2415+
'sections' => [
2416+
[
2417+
'fields' => [
2418+
[
2419+
'handle' => 'title',
2420+
'field' => [
2421+
'type' => 'text',
2422+
'validate' => ['required'],
2423+
],
2424+
],
2425+
],
2426+
],
2427+
[
2428+
'fields' => [
2429+
[
2430+
'handle' => 'category',
2431+
'field' => [
2432+
'type' => 'replicator',
2433+
'display' => 'Kategorie / Seite',
2434+
'sets' => [
2435+
'category' => [
2436+
'display' => 'Kategorie / Seite',
2437+
'sets' => [
2438+
'category' => [
2439+
'display' => 'Kategorie / Seite',
2440+
'fields' => [
2441+
[
2442+
'handle' => 'name',
2443+
'field' => [
2444+
'type' => 'text',
2445+
'display' => 'Name',
2446+
],
2447+
],
2448+
[
2449+
'handle' => 'checklist',
2450+
'field' => [
2451+
'type' => 'grid',
2452+
'display' => 'Checkliste',
2453+
'mode' => 'stacked',
2454+
'fields' => [
2455+
[
2456+
'handle' => 'hl',
2457+
'field' => [
2458+
'type' => 'text',
2459+
'display' => 'Überschrift',
2460+
'localizable' => false,
2461+
'width' => 75,
2462+
],
2463+
],
2464+
[
2465+
'handle' => 'is_done',
2466+
'field' => [
2467+
'type' => 'toggle',
2468+
'display' => 'Erledigt',
2469+
'localizable' => false,
2470+
'width' => 25,
2471+
],
2472+
],
2473+
[
2474+
'handle' => 'description',
2475+
'field' => [
2476+
'type' => 'textarea',
2477+
'display' => 'Beschreibung',
2478+
'localizable' => false,
2479+
],
2480+
],
2481+
],
2482+
],
2483+
],
2484+
],
2485+
],
2486+
],
2487+
],
2488+
],
2489+
],
2490+
],
2491+
],
2492+
],
2493+
],
2494+
],
2495+
],
2496+
'title' => 'Checklist',
2497+
])
2498+
->save();
2499+
2500+
Collection::make('albums')->save();
2501+
2502+
Entry::make()
2503+
->id('allo-mate-idee')
2504+
->collection('albums')
2505+
->slug('allo-mate')
2506+
->data([
2507+
'title' => 'Allo Mate!',
2508+
'category' => [
2509+
[
2510+
'id' => 'mfzefa0t',
2511+
'name' => 'Home',
2512+
'checklist' => [
2513+
[
2514+
'id' => 'mfzefj0y',
2515+
'hl' => 'Actionbild',
2516+
'description' => 'Test',
2517+
'is_done' => true,
2518+
],
2519+
[
2520+
'id' => 'mfzg61ls',
2521+
'hl' => 'asdsfgsdg',
2522+
'description' => 'dfdfdfg',
2523+
],
2524+
],
2525+
'type' => 'category',
2526+
'enabled' => true,
2527+
],
2528+
[
2529+
'id' => 'mfzg64m6',
2530+
'name' => 'Leistungen',
2531+
'checklist' => [
2532+
[
2533+
'id' => 'mfzg68ik',
2534+
'hl' => 'dfgdfgfd',
2535+
'is_done' => true,
2536+
'description' => 'dfgfdgfdg',
2537+
],
2538+
[
2539+
'id' => 'mfzg6d0f',
2540+
'hl' => 'gfhgfhgfh',
2541+
'description' => 'asddfggfdg',
2542+
],
2543+
],
2544+
'type' => 'category',
2545+
'enabled' => true,
2546+
],
2547+
],
2548+
])
2549+
->save();
2550+
2551+
$this
2552+
->post(route('statamic.guest-entries.update'), [
2553+
'_collection' => encrypt('albums'),
2554+
'_id' => encrypt('allo-mate-idee'),
2555+
'category' => [
2556+
[
2557+
'id' => 'mfzefa0t',
2558+
'name' => 'Home',
2559+
'checklist' => [
2560+
['id' => 'mfzefj0y', 'hl' => 'Actionbild', 'description' => 'Test', 'is_done' => true],
2561+
['id' => 'mfzg61ls', 'hl' => 'asdsfgsdg', 'description' => 'dfdfdfg'],
2562+
],
2563+
// 'type' => 'category',
2564+
'enabled' => true,
2565+
],
2566+
[
2567+
'id' => 'mfzg64m6',
2568+
'name' => 'Leistungen',
2569+
'checklist' => [
2570+
['id' => 'mfzg68ik', 'hl' => 'dfgdfgfd', 'description' => 'dfgfdgfdg'],
2571+
['id' => 'mfzg6d0f', 'hl' => 'gfhgfhgfh', 'description' => 'asddfggfdg'],
2572+
],
2573+
// 'type' => 'category',
2574+
'enabled' => true,
2575+
],
2576+
],
2577+
])
2578+
->assertRedirect();
2579+
2580+
$entry = Entry::find('allo-mate-idee');
2581+
2582+
$this->assertNotNull($entry);
2583+
$this->assertSame($entry->collectionHandle(), 'albums');
2584+
2585+
$this->assertCount(2, $entry->get('category'));
2586+
2587+
$this->assertEquals([
2588+
[
2589+
'id' => 'mfzefa0t',
2590+
'name' => 'Home',
2591+
'checklist' => [
2592+
[
2593+
'id' => 'mfzefj0y',
2594+
'hl' => 'Actionbild',
2595+
'description' => 'Test',
2596+
'is_done' => true,
2597+
],
2598+
[
2599+
'id' => 'mfzg61ls',
2600+
'hl' => 'asdsfgsdg',
2601+
'description' => 'dfdfdfg',
2602+
],
2603+
],
2604+
'type' => 'category',
2605+
'enabled' => true,
2606+
],
2607+
[
2608+
'id' => 'mfzg64m6',
2609+
'name' => 'Leistungen',
2610+
'checklist' => [
2611+
[
2612+
// This one used to have "is_done" set to true, but we unset it in the request.
2613+
'id' => 'mfzg68ik',
2614+
'hl' => 'dfgdfgfd',
2615+
'description' => 'dfgfdgfdg',
2616+
],
2617+
[
2618+
'id' => 'mfzg6d0f',
2619+
'hl' => 'gfhgfhgfh',
2620+
'description' => 'asddfggfdg',
2621+
],
2622+
],
2623+
'type' => 'category',
2624+
'enabled' => true,
2625+
],
2626+
], $entry->get('category'));
2627+
});
2628+
24022629
it('can update entry with replicator field and an assets field inside the replicator', function () {
24032630
Blueprint::make('albums')
24042631
->setNamespace('collections.albums')

0 commit comments

Comments
 (0)