Skip to content

Commit af40c9e

Browse files
FIX Get GridFieldConfigurablePaginator working again. (#438)
1 parent 7ee36bc commit af40c9e

5 files changed

Lines changed: 159 additions & 7 deletions

File tree

src/GridFieldConfigurablePaginator.php

Lines changed: 19 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
use Exception;
66
use SilverStripe\Core\Config\Configurable;
7+
use SilverStripe\Dev\Deprecation;
78
use SilverStripe\Forms\GridField\GridField;
89
use SilverStripe\Forms\GridField\GridField_FormAction;
910
use SilverStripe\Forms\GridField\GridFieldPaginator;
@@ -45,6 +46,8 @@ class GridFieldConfigurablePaginator extends GridFieldPaginator
4546
*/
4647
protected $pageSizes = array();
4748

49+
private bool $haveCheckedCount = false;
50+
4851
/**
4952
* @param int $itemsPerPage How many items should be displayed per page
5053
* @param int $pageSizes The page sizes to show in the dropdown
@@ -64,10 +67,21 @@ public function __construct($itemsPerPage = null, $pageSizes = null)
6467
* Get the total number of records in the list
6568
*
6669
* @return int
70+
* @deprecated 4.1.1 Use getTotalItems() instead.
6771
*/
6872
public function getTotalRecords()
6973
{
70-
return (int) $this->getGridField()->getList()->count();
74+
Deprecation::notice('4.1.1', 'Use getTotalItems() instead.');
75+
return $this->getTotalItems();
76+
}
77+
78+
public function getTotalItems(): int
79+
{
80+
if (!$this->haveCheckedCount) {
81+
$this->totalItems = (int) $this->getGridField()->getList()->count();
82+
$this->haveCheckedCount = true;
83+
}
84+
return $this->totalItems;
7185
}
7286

7387
/**
@@ -79,7 +93,7 @@ public function getFirstShown()
7993
{
8094
$firstShown = $this->getGridPagerState()->firstShown ?: 1;
8195
// Prevent visiting a page with an offset higher than the total number of items
82-
if ($firstShown > $this->getTotalRecords()) {
96+
if ($firstShown > $this->getTotalItems()) {
8397
$this->getGridPagerState()->firstShown = $firstShown = 1;
8498
}
8599
return $firstShown;
@@ -104,7 +118,7 @@ public function setFirstShown($firstShown = 1)
104118
*/
105119
public function getLastShown()
106120
{
107-
return min($this->getTotalRecords(), $this->getFirstShown() + $this->getItemsPerPage() - 1);
121+
return min($this->getTotalItems(), $this->getFirstShown() + $this->getItemsPerPage() - 1);
108122
}
109123

110124
/**
@@ -123,7 +137,7 @@ public function getTotalPages()
123137
$pages++;
124138

125139
// Pages after
126-
$pages += ceil(($this->getTotalRecords() - $this->getLastShown()) / $this->getItemsPerPage());
140+
$pages += ceil(($this->getTotalItems() - $this->getLastShown()) / $this->getItemsPerPage());
127141

128142
return (int) $pages;
129143
}
@@ -370,7 +384,7 @@ public function getHTMLFragments($gridField)
370384
protected function getPagerArguments()
371385
{
372386
return array(
373-
'total-rows' => $this->getTotalRecords(),
387+
'total-rows' => $this->getTotalItems(),
374388
'total-pages' => $this->getTotalPages(),
375389
'items-per-page' => $this->getItemsPerPage(),
376390
'first-shown' => $this->getFirstShown(),

tests/GridFieldConfigurablePaginatorTest.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,12 +29,12 @@ protected function setUp(): void
2929
$this->gridField = GridField::create('Mock', null, $data);
3030
}
3131

32-
public function testGetTotalRecords()
32+
public function testGetTotalItems()
3333
{
3434
$paginator = new GridFieldConfigurablePaginator;
3535
$paginator->setGridField($this->gridField);
3636

37-
$this->assertSame(130, $paginator->getTotalRecords());
37+
$this->assertSame(130, $paginator->getTotalItems());
3838
}
3939

4040
public function testGetFirstShown()
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
<?php
2+
3+
namespace Symbiote\GridFieldExtensions\Tests\Stub\Extension;
4+
5+
use SilverStripe\Core\Extension;
6+
use SilverStripe\Dev\TestOnly;
7+
use SilverStripe\Forms\FieldList;
8+
use SilverStripe\Forms\GridField\GridField;
9+
use SilverStripe\Forms\GridField\GridFieldPaginator;
10+
use Symbiote\GridFieldExtensions\GridFieldConfigurablePaginator;
11+
12+
class ConfigurablePaginatorExtension extends Extension implements TestOnly
13+
{
14+
protected function updateCMSFields(FieldList $fields): void
15+
{
16+
/** @var GridField $gridField */
17+
$gridField = $fields->dataFieldByName('Employees');
18+
$config = $gridField->getConfig();
19+
$config->removeComponentsByType(GridFieldPaginator::class);
20+
$config->addComponent(new GridFieldConfigurablePaginator());
21+
}
22+
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
<?php
2+
3+
namespace Symbiote\GridFieldExtensions\Tests\Stub\Extension;
4+
5+
use SilverStripe\Core\Extension;
6+
use SilverStripe\Dev\TestOnly;
7+
8+
/**
9+
* Removes 'ColleagueNames' field from summary fields so we can accurately tell which records are being displayed
10+
*/
11+
class NoCoworkersExtension extends Extension implements TestOnly
12+
{
13+
protected function updateSummaryFields(array &$fields): void
14+
{
15+
unset($fields['ColleagueNames']);
16+
}
17+
}
Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
1+
Feature: Using GridFieldConfigurablePaginator in place of GridFieldPaginator
2+
As a content editor
3+
I want to use the functionality in GridFieldConfigurablePaginator
4+
5+
Background:
6+
Given I add an extension "Symbiote\GridFieldExtensions\Tests\Stub\Extension\ConfigurablePaginatorExtension" to the "SilverStripe\FrameworkTest\Model\Company" class
7+
And I add an extension "Symbiote\GridFieldExtensions\Tests\Stub\Extension\NoCoworkersExtension" to the "SilverStripe\FrameworkTest\Model\Employee" class
8+
And there are the following SilverStripe\FrameworkTest\Model\Employee records
9+
"""
10+
employee1:
11+
Name: "Adam"
12+
employee2:
13+
Name: "Jim"
14+
employee3:
15+
Name: "Tom"
16+
employee4:
17+
Name: "James"
18+
employee5:
19+
Name: "Jane"
20+
employee6:
21+
Name: "Chloe"
22+
employee7:
23+
Name: "Amy"
24+
employee8:
25+
Name: "Sarah"
26+
employee9:
27+
Name: "Sam"
28+
employee10:
29+
Name: "Ashleigh"
30+
employee11:
31+
Name: "Tiago"
32+
employee12:
33+
Name: "Catalina"
34+
employee13:
35+
Name: "Oscar"
36+
employee14:
37+
Name: "Lima"
38+
employee15:
39+
Name: "Danie"
40+
employee16:
41+
Name: "Lucia"
42+
"""
43+
Given there are the following SilverStripe\FrameworkTest\Model\Company records
44+
"""
45+
company-1:
46+
Name: "Company One"
47+
Employees:
48+
- =>SilverStripe\FrameworkTest\Model\Employee.employee1
49+
- =>SilverStripe\FrameworkTest\Model\Employee.employee2
50+
- =>SilverStripe\FrameworkTest\Model\Employee.employee3
51+
- =>SilverStripe\FrameworkTest\Model\Employee.employee4
52+
- =>SilverStripe\FrameworkTest\Model\Employee.employee5
53+
- =>SilverStripe\FrameworkTest\Model\Employee.employee6
54+
- =>SilverStripe\FrameworkTest\Model\Employee.employee7
55+
- =>SilverStripe\FrameworkTest\Model\Employee.employee8
56+
- =>SilverStripe\FrameworkTest\Model\Employee.employee9
57+
- =>SilverStripe\FrameworkTest\Model\Employee.employee10
58+
- =>SilverStripe\FrameworkTest\Model\Employee.employee11
59+
- =>SilverStripe\FrameworkTest\Model\Employee.employee12
60+
- =>SilverStripe\FrameworkTest\Model\Employee.employee13
61+
- =>SilverStripe\FrameworkTest\Model\Employee.employee14
62+
- =>SilverStripe\FrameworkTest\Model\Employee.employee15
63+
- =>SilverStripe\FrameworkTest\Model\Employee.employee16
64+
"""
65+
And the "group" "EDITOR" has permissions "Access to 'Pages' section" and "Access to 'GridField Test Navigation' section" and "TEST_DATAOBJECT_EDIT"
66+
And I am logged in as a member of "EDITOR" group
67+
And I go to "/admin/gridfield-test-navigation"
68+
And I click "Company One" in the "#Form_EditForm" element
69+
And I click the "Employees" CMS tab
70+
71+
Scenario: I can navigate through a paginated gridfield
72+
When I should see "Company One" in the ".breadcrumbs-wrapper" element
73+
# Check we have the first and last expected values for page 1
74+
Then I should see "Adam" in the "#Form_ItemEditForm" element
75+
And I should see "Danie" in the "#Form_ItemEditForm" element
76+
# Check we don't have the first expected value for page 2
77+
And I should not see "Lucia" in the "#Form_ItemEditForm" element
78+
# Check the paginator elements render correctly
79+
And I should see "1–15 of 16" in the ".grid-field__paginator_numbers" element
80+
And the ".pagination-page-number" element should contain "<input class=\"text no-change-track\" value=\"1\" data-skip-autofocus=\"true\">"
81+
And I should see "15" in the ".pagination-page-size-select" element
82+
# Go to page 2 using buttons
83+
Given I press the "Next" button
84+
Then I should not see "Adam" in the "#Form_ItemEditForm" element
85+
And I should not see "Danie" in the "#Form_ItemEditForm" element
86+
And I should see "Lucia" in the "#Form_ItemEditForm" element
87+
And I should see "16–16 of 16" in the ".grid-field__paginator_numbers" element
88+
And the ".pagination-page-number" element should contain "<input class=\"text no-change-track\" value=\"2\" data-skip-autofocus=\"true\">"
89+
# Go to page 1 using the page number input field
90+
Given I click on the ".pagination-page-number input" element
91+
And I press the "Backspace" key globally
92+
And I type "1" in the field
93+
And I press the "Enter" key globally
94+
Then I should see "Adam" in the "#Form_ItemEditForm" element
95+
And I should see "Danie" in the "#Form_ItemEditForm" element
96+
And I should not see "Lucia" in the "#Form_ItemEditForm" element
97+
And I should see "1–15 of 16" in the ".grid-field__paginator_numbers" element
98+
And the ".pagination-page-number" element should contain "<input class=\"text no-change-track\" value=\"1\" data-skip-autofocus=\"true\">"
99+
And I should see "15" in the ".pagination-page-size-select" element

0 commit comments

Comments
 (0)