Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

FIX Use the manipulated datalist for counting total items #776

Merged
merged 2 commits into from Sep 6, 2012
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
22 changes: 16 additions & 6 deletions forms/gridfield/GridField.php
Expand Up @@ -192,6 +192,21 @@ public function setList(SS_List $list) {
public function getList() {
return $this->list;
}

/**
* Get the datasource after applying the {@link GridField_DataManipulator}s to it.
*
* @return SS_List
*/
public function getManipulatedList() {
$list = $this->getList();
foreach($this->getComponents() as $item) {
if($item instanceof GridField_DataManipulator) {
$list = $item->getManipulatedData($this, $list);
}
}
return $list;
}

/**
* Get the current GridState_Data or the GridState
Expand Down Expand Up @@ -227,12 +242,7 @@ public function FieldHolder($properties = array()) {
$columns = $this->getColumns();

// Get data
$list = $this->getList();
foreach($this->getComponents() as $item) {
if($item instanceof GridField_DataManipulator) {
$list = $item->getManipulatedData($this, $list);
}
}
$list = $this->getManipulatedList();

// Render headers, footers, etc
$content = array(
Expand Down
2 changes: 1 addition & 1 deletion forms/gridfield/GridFieldPaginator.php
Expand Up @@ -108,7 +108,7 @@ public function handleAction(GridField $gridField, $actionName, $arguments, $dat
public function getManipulatedData(GridField $gridField, SS_List $dataList) {
if(!$this->checkDataType($dataList)) return $dataList;

$this->totalItems = $gridField->getList()->count();
$this->totalItems = $dataList->count();

$state = $gridField->State->GridFieldPaginator;
if(!is_int($state->currentPage)) {
Expand Down
24 changes: 24 additions & 0 deletions tests/forms/GridFieldTest.php
Expand Up @@ -392,6 +392,22 @@ public function testCanViewOnlyOddIDs() {
$this->assertEquals((string)$members[1]->td[0], 'Otto Fischer', 'Second object Name should be Otto Fischer');
$this->assertEquals((string)$members[1]->td[1], 'otto.fischer@example.org', 'Second object Email should be otto.fischer@example.org');
}

public function testChainedDataManipulators() {
$config = new GridFieldConfig();
$data = new ArrayList(array(1, 2, 3, 4, 5, 6));
$gridField = new GridField('testfield', 'testfield', $data, $config);
$endList = $gridField->getManipulatedList();
$this->assertEquals($endList->Count(), 6);

$config->addComponent(new GridFieldTest_Component2);
$endList = $gridField->getManipulatedList();
$this->assertEquals($endList->Count(), 12);

$config->addComponent(new GridFieldPaginator(10));
$endList = $gridField->getManipulatedList();
$this->assertEquals($endList->Count(), 10);
}
}

class GridFieldTest_Component implements GridField_ColumnProvider, GridField_ActionProvider, TestOnly{
Expand Down Expand Up @@ -430,6 +446,14 @@ public function handleAction(GridField $gridField, $actionName, $arguments, $dat

}

class GridFieldTest_Component2 implements GridField_DataManipulator, TestOnly {
function getManipulatedData(GridField $gridField, SS_List $dataList) {
$dataList = clone $dataList;
$dataList->merge(new ArrayList(array(7, 8, 9, 10, 11, 12)));
return $dataList;
}
}

class GridFieldTest_Team extends DataObject implements TestOnly {
static $db = array(
'Name' => 'Varchar',
Expand Down