Skip to content

Commit 294c101

Browse files
DanielDarrenJonesjasonvargaclaude
authored
[6.x] Only send the counts of replicator sets and grid rows to listings (#15811)
Co-authored-by: Jason Varga <jason@pixelfear.com> Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
1 parent cacbb17 commit 294c101

7 files changed

Lines changed: 74 additions & 2 deletions

File tree

‎resources/js/components/fieldtypes/grid/GridIndex.vue‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
<template>
2-
<div v-if="value">
2+
<div v-if="value != null">
33
{{ __n(':count row|:count rows', value) }}
44
</div>
55
</template>

‎resources/js/components/fieldtypes/replicator/ReplicatorIndex.vue‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
<template>
2-
<div v-if="value">
2+
<div v-if="value != null">
33
{{ __n(':count set|:count sets', value) }}
44
</div>
55
</template>

‎src/Fieldtypes/Grid.php‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,11 @@ private function processRow($row, $index)
115115
return Arr::removeNullValues($row);
116116
}
117117

118+
public function preProcessIndex($data)
119+
{
120+
return is_array($data) ? count($data) : $data;
121+
}
122+
118123
public function preProcess($data)
119124
{
120125
$data = collect($data);

‎src/Fieldtypes/Replicator.php‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -124,6 +124,11 @@ protected function processRow($row, $index)
124124
return Arr::removeNullValues($row);
125125
}
126126

127+
public function preProcessIndex($data)
128+
{
129+
return is_array($data) ? count($data) : $data;
130+
}
131+
127132
public function preProcess($data)
128133
{
129134
return collect($data)->map(function ($row, $i) {

‎tests/Feature/Entries/ViewEntryListingTest.php‎

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,11 @@
22

33
namespace Tests\Feature\Entries;
44

5+
use Facades\Statamic\Fields\BlueprintRepository;
56
use Facades\Tests\Factories\EntryFactory;
67
use PHPUnit\Framework\Attributes\Test;
78
use Statamic\Entries\Collection;
9+
use Statamic\Facades\Blueprint;
810
use Statamic\Facades\User;
911
use Tests\FakesRoles;
1012
use Tests\PreventSavingStacheItemsToDisk;
@@ -90,4 +92,44 @@ public function it_shows_only_entries_in_index_for_sites_user_can_access()
9092

9193
$this->assertEquals($expected, $entries->pluck('slug')->all());
9294
}
95+
96+
#[Test]
97+
public function it_only_lists_the_counts_of_replicator_sets_and_grid_rows()
98+
{
99+
$collection = tap(Collection::make('test'))->save();
100+
101+
$blueprint = Blueprint::makeFromFields([
102+
'sets' => ['type' => 'replicator', 'listable' => true, 'sets' => ['main' => ['sets' => [
103+
'text' => ['fields' => [['handle' => 'words', 'field' => ['type' => 'text']]]],
104+
]]]],
105+
'rows' => ['type' => 'grid', 'listable' => true, 'fields' => [
106+
['handle' => 'words', 'field' => ['type' => 'text']],
107+
]],
108+
])->setHandle('test');
109+
110+
BlueprintRepository::partialMock();
111+
BlueprintRepository::shouldReceive('in')->with('collections/test')->andReturn(collect([$blueprint]));
112+
113+
EntryFactory::collection($collection)->slug('one')->data([
114+
'sets' => [
115+
['id' => 'set-a', 'type' => 'text', 'words' => 'Lots of words'],
116+
['id' => 'set-b', 'type' => 'text', 'words' => 'More words', 'enabled' => false],
117+
],
118+
'rows' => [
119+
['id' => 'row-a', 'words' => 'Lots of words'],
120+
['id' => 'row-b', 'words' => 'More words'],
121+
['id' => 'row-c', 'words' => 'Even more words'],
122+
],
123+
])->create();
124+
125+
$response = $this
126+
->actingAs(tap(User::make()->makeSuper())->save())
127+
->get(cp_route('collections.entries.index', ['collection' => 'test', 'columns' => 'title,sets,rows']))
128+
->assertOk();
129+
130+
$entry = $response->json('data.0');
131+
132+
$this->assertSame(2, $entry['sets']);
133+
$this->assertSame(3, $entry['rows']);
134+
}
93135
}

‎tests/Fieldtypes/GridTest.php‎

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -642,4 +642,14 @@ public function augment($value)
642642
$this->assertEquals('test.-1.words', $value['new']['words']['fieldPathPrefix']);
643643
$this->assertEquals('test.-1.words', $value['defaults']['words']);
644644
}
645+
646+
#[Test]
647+
public function it_preprocesses_for_the_index_as_a_row_count()
648+
{
649+
$fieldtype = new Grid;
650+
651+
$this->assertSame(2, $fieldtype->preProcessIndex([['id' => 'a', 'words' => 'Hello'], ['words' => 'Hi']]));
652+
$this->assertNull($fieldtype->preProcessIndex(null));
653+
$this->assertSame('not an array', $fieldtype->preProcessIndex('not an array'));
654+
}
645655
}

‎tests/Fieldtypes/ReplicatorTest.php‎

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1581,6 +1581,16 @@ private function fieldWithSet(string $set)
15811581
]);
15821582
}
15831583

1584+
#[Test]
1585+
public function it_preprocesses_for_the_index_as_a_set_count()
1586+
{
1587+
$fieldtype = new Replicator;
1588+
1589+
$this->assertSame(2, $fieldtype->preProcessIndex([['id' => 'a', 'type' => 'text', 'words' => 'Hello', 'enabled' => false], ['type' => 'text', 'words' => 'Hi']]));
1590+
$this->assertNull($fieldtype->preProcessIndex(null));
1591+
$this->assertSame('not an array', $fieldtype->preProcessIndex('not an array'));
1592+
}
1593+
15841594
public static function groupedSetsProvider()
15851595
{
15861596
return [

0 commit comments

Comments
 (0)