Skip to content
Merged
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
2 changes: 1 addition & 1 deletion resources/js/components/fieldtypes/grid/GridIndex.vue
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<template>
<div v-if="value">
<div v-if="value != null">
{{ __n(':count row|:count rows', value) }}
</div>
</template>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<template>
<div v-if="value">
<div v-if="value != null">
{{ __n(':count set|:count sets', value) }}
</div>
</template>
Expand Down
5 changes: 5 additions & 0 deletions src/Fieldtypes/Grid.php
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,11 @@ private function processRow($row, $index)
return Arr::removeNullValues($row);
}

public function preProcessIndex($data)
{
return is_array($data) ? count($data) : $data;
}

public function preProcess($data)
{
$data = collect($data);
Expand Down
5 changes: 5 additions & 0 deletions src/Fieldtypes/Replicator.php
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,11 @@ protected function processRow($row, $index)
return Arr::removeNullValues($row);
}

public function preProcessIndex($data)
{
return is_array($data) ? count($data) : $data;
}

public function preProcess($data)
{
return collect($data)->map(function ($row, $i) {
Expand Down
42 changes: 42 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,44 @@ 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_counts_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(2, $entry['sets']);
$this->assertSame(3, $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_as_a_row_count()
{
$fieldtype = new Grid;

$this->assertSame(2, $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_as_a_set_count()
{
$fieldtype = new Replicator;

$this->assertSame(2, $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