Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 15 additions & 0 deletions src/Fieldtypes/Grid.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
15 changes: 15 additions & 0 deletions src/Fieldtypes/Replicator.php
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
46 changes: 46 additions & 0 deletions tests/Feature/Entries/ViewEntryListingTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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']);
}
}
10 changes: 10 additions & 0 deletions tests/Fieldtypes/GridTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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'));
}
}
10 changes: 10 additions & 0 deletions tests/Fieldtypes/ReplicatorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 [
Expand Down
Loading