Merge pull request #10759 from creative-commoners/pulls/4.12/gridfieldprint-canview

Check canView before printing from GridField
This commit is contained in:
Sabina Talipova 2023-04-26 11:24:46 +12:00 committed by GitHub
commit 961499f640
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 34 additions and 13 deletions

View File

@ -228,21 +228,23 @@ class GridFieldPrintButton extends AbstractGridFieldComponent implements GridFie
/** @var DataObject $item */ /** @var DataObject $item */
foreach ($items->limit(null) as $item) { foreach ($items->limit(null) as $item) {
$itemRow = new ArrayList(); if (!$item->hasMethod('canView') || $item->canView()) {
$itemRow = new ArrayList();
foreach ($printColumns as $field => $label) { foreach ($printColumns as $field => $label) {
$value = $gridFieldColumnsComponent $value = $gridFieldColumnsComponent
? strip_tags($gridFieldColumnsComponent->getColumnContent($gridField, $item, $field)) ? strip_tags($gridFieldColumnsComponent->getColumnContent($gridField, $item, $field))
: $gridField->getDataFieldValue($item, $field); : $gridField->getDataFieldValue($item, $field);
$itemRow->push(new ArrayData([ $itemRow->push(new ArrayData([
"CellString" => $value, "CellString" => $value,
]));
}
$itemRows->push(new ArrayData([
"ItemRow" => $itemRow
])); ]));
} }
$itemRows->push(new ArrayData([
"ItemRow" => $itemRow
]));
if ($item->hasMethod('destroy')) { if ($item->hasMethod('destroy')) {
$item->destroy(); $item->destroy();
} }

View File

@ -32,6 +32,19 @@ class GridFieldPrintButtonTest extends SapphireTest
} }
public function testLimit() public function testLimit()
{
$this->assertEquals(42, $this->getTestableRows()->count());
}
public function testCanViewIsRespected()
{
$orig = TestObject::$canView;
TestObject::$canView = false;
$this->assertEquals(0, $this->getTestableRows()->count());
TestObject::$canView = $orig;
}
private function getTestableRows()
{ {
$list = TestObject::get(); $list = TestObject::get();
@ -48,7 +61,6 @@ class GridFieldPrintButtonTest extends SapphireTest
// Printed data should ignore pagination limit // Printed data should ignore pagination limit
$printData = $button->generatePrintData($gridField); $printData = $button->generatePrintData($gridField);
$rows = $printData->ItemRows; return $printData->ItemRows;
$this->assertEquals(42, $rows->count());
} }
} }

View File

@ -12,4 +12,11 @@ class TestObject extends DataObject implements TestOnly
private static $db = [ private static $db = [
'Name' => 'Varchar' 'Name' => 'Varchar'
]; ];
public static bool $canView = true;
public function canView($member = null)
{
return static::$canView;
}
} }