diff --git a/src/Fieldtypes/Grid.php b/src/Fieldtypes/Grid.php index cd4926c0b39..0eb40b4036f 100644 --- a/src/Fieldtypes/Grid.php +++ b/src/Fieldtypes/Grid.php @@ -115,6 +115,21 @@ private function processRow($row, $index) return Arr::removeNullValues($row); } + /** + * Listings only show how many rows there are, so only each row's id + * is sent rather than everything in it. + */ + public function preProcessIndex($data) + { + if (! is_array($data)) { + return $data; + } + + return array_map(function ($row) { + return is_array($row) ? Arr::only($row, [RowId::handle()]) : $row; + }, $data); + } + public function preProcess($data) { $data = collect($data); diff --git a/src/Fieldtypes/Replicator.php b/src/Fieldtypes/Replicator.php index 73e0bc0f48b..428fd1e9739 100644 --- a/src/Fieldtypes/Replicator.php +++ b/src/Fieldtypes/Replicator.php @@ -124,6 +124,21 @@ protected function processRow($row, $index) return Arr::removeNullValues($row); } + /** + * Listings only show how many sets there are, so only each set's id, + * type and enabled state are sent rather than everything in them. + */ + public function preProcessIndex($data) + { + if (! is_array($data)) { + return $data; + } + + return array_map(function ($set) { + return is_array($set) ? Arr::only($set, [RowId::handle(), 'type', 'enabled']) : $set; + }, $data); + } + public function preProcess($data) { return collect($data)->map(function ($row, $i) { diff --git a/tests/Feature/Entries/ViewEntryListingTest.php b/tests/Feature/Entries/ViewEntryListingTest.php index 0d36b634ddc..d1b36f00299 100644 --- a/tests/Feature/Entries/ViewEntryListingTest.php +++ b/tests/Feature/Entries/ViewEntryListingTest.php @@ -2,9 +2,11 @@ namespace Tests\Feature\Entries; +use Facades\Statamic\Fields\BlueprintRepository; use Facades\Tests\Factories\EntryFactory; use PHPUnit\Framework\Attributes\Test; use Statamic\Entries\Collection; +use Statamic\Facades\Blueprint; use Statamic\Facades\User; use Tests\FakesRoles; use Tests\PreventSavingStacheItemsToDisk; @@ -90,4 +92,48 @@ public function it_shows_only_entries_in_index_for_sites_user_can_access() $this->assertEquals($expected, $entries->pluck('slug')->all()); } + + #[Test] + public function it_only_lists_the_ids_and_types_of_replicator_sets_and_grid_rows() + { + $collection = tap(Collection::make('test'))->save(); + + $blueprint = Blueprint::makeFromFields([ + 'sets' => ['type' => 'replicator', 'listable' => true, 'sets' => ['main' => ['sets' => [ + 'text' => ['fields' => [['handle' => 'words', 'field' => ['type' => 'text']]]], + ]]]], + 'rows' => ['type' => 'grid', 'listable' => true, 'fields' => [ + ['handle' => 'words', 'field' => ['type' => 'text']], + ]], + ])->setHandle('test'); + + BlueprintRepository::partialMock(); + BlueprintRepository::shouldReceive('in')->with('collections/test')->andReturn(collect([$blueprint])); + + EntryFactory::collection($collection)->slug('one')->data([ + 'sets' => [ + ['id' => 'set-a', 'type' => 'text', 'words' => 'Lots of words'], + ['id' => 'set-b', 'type' => 'text', 'words' => 'More words', 'enabled' => false], + ], + 'rows' => [ + ['id' => 'row-a', 'words' => 'Lots of words'], + ['id' => 'row-b', 'words' => 'More words'], + ['id' => 'row-c', 'words' => 'Even more words'], + ], + ])->create(); + + $response = $this + ->actingAs(tap(User::make()->makeSuper())->save()) + ->get(cp_route('collections.entries.index', ['collection' => 'test', 'columns' => 'title,sets,rows'])) + ->assertOk(); + + $entry = $response->json('data.0'); + + $this->assertSame([ + ['id' => 'set-a', 'type' => 'text'], + ['id' => 'set-b', 'type' => 'text', 'enabled' => false], + ], $entry['sets']); + + $this->assertSame([['id' => 'row-a'], ['id' => 'row-b'], ['id' => 'row-c']], $entry['rows']); + } } diff --git a/tests/Fieldtypes/GridTest.php b/tests/Fieldtypes/GridTest.php index 97c0604f366..6d8da518df1 100644 --- a/tests/Fieldtypes/GridTest.php +++ b/tests/Fieldtypes/GridTest.php @@ -642,4 +642,14 @@ public function augment($value) $this->assertEquals('test.-1.words', $value['new']['words']['fieldPathPrefix']); $this->assertEquals('test.-1.words', $value['defaults']['words']); } + + #[Test] + public function it_preprocesses_for_the_index_without_the_contents() + { + $fieldtype = new Grid; + + $this->assertSame([['id' => 'a'], []], $fieldtype->preProcessIndex([['id' => 'a', 'words' => 'Hello'], ['words' => 'Hi']])); + $this->assertNull($fieldtype->preProcessIndex(null)); + $this->assertSame('not an array', $fieldtype->preProcessIndex('not an array')); + } } diff --git a/tests/Fieldtypes/ReplicatorTest.php b/tests/Fieldtypes/ReplicatorTest.php index 1f9b45c2da8..b621339ab09 100644 --- a/tests/Fieldtypes/ReplicatorTest.php +++ b/tests/Fieldtypes/ReplicatorTest.php @@ -1581,6 +1581,16 @@ private function fieldWithSet(string $set) ]); } + #[Test] + public function it_preprocesses_for_the_index_without_the_contents() + { + $fieldtype = new Replicator; + + $this->assertSame([['id' => 'a', 'type' => 'text', 'enabled' => false], ['type' => 'text']], $fieldtype->preProcessIndex([['id' => 'a', 'type' => 'text', 'words' => 'Hello', 'enabled' => false], ['type' => 'text', 'words' => 'Hi']])); + $this->assertNull($fieldtype->preProcessIndex(null)); + $this->assertSame('not an array', $fieldtype->preProcessIndex('not an array')); + } + public static function groupedSetsProvider() { return [