mirror of
https://github.com/silverstripe/silverstripe-framework
synced 2024-10-22 12:05:37 +00:00
Merge pull request #347 from robert-h-curry/7010-gridfield-set-display-fields
ENHANCEMENT: Fixes #7010. Move DisplayFields, FieldCasting and FieldForm...
This commit is contained in:
commit
7dc5eb0cdc
@ -52,14 +52,16 @@ class SecurityAdmin extends LeftAndMain implements PermissionProvider {
|
||||
)->addExtraClass("members_grid");
|
||||
$memberListConfig->getComponentByType('GridFieldDetailForm')->setValidator(new Member_Validator());
|
||||
|
||||
$groupList = GridField::create( 'Groups',
|
||||
$groupList = GridField::create(
|
||||
'Groups',
|
||||
false,
|
||||
DataList::create('Group'),
|
||||
GridFieldConfig_RecordEditor::create()
|
||||
)->setDisplayFields(array(
|
||||
);
|
||||
$columns = $groupList->getConfig()->getComponentByType('GridFieldDataColumns');
|
||||
$columns->setDisplayFields(array(
|
||||
'Breadcrumbs' => singleton('Group')->fieldLabel('Title')
|
||||
));
|
||||
|
||||
|
||||
$fields = new FieldList(
|
||||
$root = new TabSet(
|
||||
|
@ -371,7 +371,8 @@ class HtmlEditorField_Toolbar extends RequestHandler {
|
||||
$fileField->setList($this->getFiles($parentID));
|
||||
$fileField->setAttribute('data-selectable', true);
|
||||
$fileField->setAttribute('data-multiselect', true);
|
||||
$fileField->setDisplayFields(array(
|
||||
$columns = $fileField->getConfig()->getComponentByType('GridFieldDataColumns');
|
||||
$columns->setDisplayFields(array(
|
||||
'CMSThumbnail' => false,
|
||||
'Name' => _t('File.Name'),
|
||||
));
|
||||
|
@ -34,12 +34,6 @@ class GridField extends FormField {
|
||||
|
||||
/** @var string - the classname of the DataObject that the GridField will display. Defaults to the value of $this->list->dataClass */
|
||||
protected $modelClassName = '';
|
||||
|
||||
/** @var array */
|
||||
public $fieldCasting = array();
|
||||
|
||||
/** @var array */
|
||||
public $fieldFormatting = array();
|
||||
|
||||
/** @var GridState - the current state of the GridField */
|
||||
protected $state = null;
|
||||
@ -55,13 +49,6 @@ class GridField extends FormField {
|
||||
*/
|
||||
protected $components = array();
|
||||
|
||||
/**
|
||||
* This is the columns that will be visible
|
||||
*
|
||||
* @var array
|
||||
*/
|
||||
protected $displayFields = array();
|
||||
|
||||
/**
|
||||
* Internal dispatcher for column handlers.
|
||||
* Keys are column names and values are GridField_ColumnProvider objects
|
||||
@ -109,7 +96,7 @@ class GridField extends FormField {
|
||||
* this modelclass $summary_fields
|
||||
*
|
||||
* @param string $modelClassName
|
||||
* @see GridField::getDisplayFields()
|
||||
* @see GridFieldDataColumns::getDisplayFields()
|
||||
*/
|
||||
public function setModelClass($modelClassName) {
|
||||
$this->modelClassName = $modelClassName;
|
||||
@ -143,19 +130,6 @@ class GridField extends FormField {
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the DisplayFields
|
||||
*
|
||||
* @return array
|
||||
* @see GridField::setDisplayFields
|
||||
*/
|
||||
public function getDisplayFields() {
|
||||
if(!$this->displayFields) {
|
||||
return singleton($this->getModelClass())->summaryFields();
|
||||
}
|
||||
return $this->displayFields;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the GridFieldConfig
|
||||
*
|
||||
@ -165,63 +139,6 @@ class GridField extends FormField {
|
||||
return $this->config;
|
||||
}
|
||||
|
||||
/**
|
||||
* Override the default behaviour of showing the models summaryFields with
|
||||
* these fields instead
|
||||
* Example: array( 'Name' => 'Members name', 'Email' => 'Email address')
|
||||
*
|
||||
* @param array $fields
|
||||
*/
|
||||
public function setDisplayFields($fields) {
|
||||
if(!is_array($fields)) {
|
||||
throw new InvalidArgumentException('Arguments passed to GridField::setDisplayFields() must be an array');
|
||||
}
|
||||
$this->displayFields = $fields;
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Specify castings with fieldname as the key, and the desired casting as value.
|
||||
* Example: array("MyCustomDate"=>"Date","MyShortText"=>"Text->FirstSentence")
|
||||
*
|
||||
* @param array $casting
|
||||
* @todo refactor this into GridFieldComponent
|
||||
*/
|
||||
public function setFieldCasting($casting) {
|
||||
$this->fieldCasting = $casting;
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Specify custom formatting for fields, e.g. to render a link instead of pure text.
|
||||
* Caution: Make sure to escape special php-characters like in a normal php-statement.
|
||||
* Example: "myFieldName" => '<a href=\"custom-admin/$ID\">$ID</a>'.
|
||||
* Alternatively, pass a anonymous function, which takes one parameter: The list item.
|
||||
*
|
||||
* @param array $casting
|
||||
* @todo refactor this into GridFieldComponent
|
||||
*/
|
||||
public function getFieldCasting() {
|
||||
return $this->fieldCasting;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param array $casting
|
||||
* @todo refactor this into GridFieldComponent
|
||||
*/
|
||||
public function setFieldFormatting($formatting) {
|
||||
$this->fieldFormatting = $formatting;
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param array $casting
|
||||
* @todo refactor this into GridFieldComponent
|
||||
*/
|
||||
public function getFieldFormatting() {
|
||||
return $this->fieldFormatting;
|
||||
}
|
||||
|
||||
/**
|
||||
* Cast a arbitrary value with the help of a castingDefintion
|
||||
*
|
||||
|
@ -36,7 +36,7 @@ interface GridField_ColumnProvider extends GridFieldComponent {
|
||||
|
||||
/**
|
||||
* Modify the list of columns displayed in the table.
|
||||
* See {@link GridField->getDisplayFields()} and {@link GridFieldDataColumns}.
|
||||
* See {@link GridFieldDataColumns->getDisplayFields()} and {@link GridFieldDataColumns}.
|
||||
*
|
||||
* @param GridField
|
||||
* @param Array List reference of all column names.
|
||||
|
@ -8,13 +8,92 @@
|
||||
*/
|
||||
class GridFieldDataColumns implements GridField_ColumnProvider {
|
||||
|
||||
/** @var array */
|
||||
public $fieldCasting = array();
|
||||
|
||||
/** @var array */
|
||||
public $fieldFormatting = array();
|
||||
|
||||
/**
|
||||
* This is the columns that will be visible
|
||||
*
|
||||
* @var array
|
||||
*/
|
||||
protected $displayFields = array();
|
||||
|
||||
public function augmentColumns($gridField, &$columns) {
|
||||
$baseColumns = array_keys($gridField->getDisplayFields());
|
||||
$baseColumns = array_keys($this->getDisplayFields($gridField));
|
||||
foreach($baseColumns as $col) $columns[] = $col;
|
||||
}
|
||||
|
||||
public function getColumnsHandled($gridField) {
|
||||
return array_keys($gridField->getDisplayFields());
|
||||
return array_keys($this->getDisplayFields($gridField));
|
||||
}
|
||||
|
||||
/**
|
||||
* Override the default behaviour of showing the models summaryFields with
|
||||
* these fields instead
|
||||
* Example: array( 'Name' => 'Members name', 'Email' => 'Email address')
|
||||
*
|
||||
* @param array $fields
|
||||
*/
|
||||
public function setDisplayFields($fields) {
|
||||
if(!is_array($fields)) {
|
||||
throw new InvalidArgumentException('Arguments passed to GridFieldDataColumns::setDisplayFields() must be an array');
|
||||
}
|
||||
$this->displayFields = $fields;
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the DisplayFields
|
||||
*
|
||||
* @return array
|
||||
* @see GridFieldDataColumns::setDisplayFields
|
||||
*/
|
||||
public function getDisplayFields($gridField) {
|
||||
if(!$this->displayFields) {
|
||||
return singleton($gridField->getModelClass())->summaryFields();
|
||||
}
|
||||
return $this->displayFields;
|
||||
}
|
||||
|
||||
/**
|
||||
* Specify castings with fieldname as the key, and the desired casting as value.
|
||||
* Example: array("MyCustomDate"=>"Date","MyShortText"=>"Text->FirstSentence")
|
||||
*
|
||||
* @param array $casting
|
||||
*/
|
||||
public function setFieldCasting($casting) {
|
||||
$this->fieldCasting = $casting;
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Specify custom formatting for fields, e.g. to render a link instead of pure text.
|
||||
* Caution: Make sure to escape special php-characters like in a normal php-statement.
|
||||
* Example: "myFieldName" => '<a href=\"custom-admin/$ID\">$ID</a>'.
|
||||
* Alternatively, pass a anonymous function, which takes one parameter: The list item.
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
public function getFieldCasting() {
|
||||
return $this->fieldCasting;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param array $formatting
|
||||
*/
|
||||
public function setFieldFormatting($formatting) {
|
||||
$this->fieldFormatting = $formatting;
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return array
|
||||
*/
|
||||
public function getFieldFormatting() {
|
||||
return $this->fieldFormatting;
|
||||
}
|
||||
|
||||
/**
|
||||
@ -48,7 +127,7 @@ class GridFieldDataColumns implements GridField_ColumnProvider {
|
||||
$tmpItem = $tmpItem->$relationMethod();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
$value = $this->castValue($gridField, $column, $value);
|
||||
@ -63,7 +142,7 @@ class GridFieldDataColumns implements GridField_ColumnProvider {
|
||||
}
|
||||
|
||||
public function getColumnMetadata($gridField, $column) {
|
||||
$columns = $gridField->getDisplayFields();
|
||||
$columns = $this->getDisplayFields($gridField);
|
||||
return array(
|
||||
'title' => $columns[$column],
|
||||
);
|
||||
@ -76,8 +155,8 @@ class GridFieldDataColumns implements GridField_ColumnProvider {
|
||||
* @return type
|
||||
*/
|
||||
protected function castValue($gridField, $fieldName, $value) {
|
||||
if(array_key_exists($fieldName, $gridField->FieldCasting)) {
|
||||
return $gridField->getCastedValue($value, $gridField->FieldCasting[$fieldName]);
|
||||
if(array_key_exists($fieldName, $this->fieldCasting)) {
|
||||
return $gridField->getCastedValue($value, $this->fieldCasting[$fieldName]);
|
||||
} elseif(is_object($value) && method_exists($value, 'Nice')) {
|
||||
return $value->Nice();
|
||||
}
|
||||
@ -91,11 +170,11 @@ class GridFieldDataColumns implements GridField_ColumnProvider {
|
||||
* @return type
|
||||
*/
|
||||
protected function formatValue($gridField, $item, $fieldName, $value) {
|
||||
if(!array_key_exists($fieldName, $gridField->FieldFormatting)) {
|
||||
if(!array_key_exists($fieldName, $this->fieldFormatting)) {
|
||||
return $value;
|
||||
}
|
||||
|
||||
$spec = $gridField->FieldFormatting[$fieldName];
|
||||
$spec = $this->fieldFormatting[$fieldName];
|
||||
if(is_callable($spec)) {
|
||||
return $spec($item);
|
||||
} else {
|
||||
|
@ -99,7 +99,7 @@ class GridFieldExportButton implements GridField_HTMLProvider, GridField_ActionP
|
||||
*/
|
||||
function generateExportFileData($gridField) {
|
||||
$separator = $this->csvSeparator;
|
||||
$csvColumns = ($this->exportColumns) ? $this->exportColumns : $gridField->getDisplayFields();
|
||||
$csvColumns = ($this->exportColumns) ? $this->exportColumns : singleton($gridField->getModelClass())->summaryFields();
|
||||
$fileData = '';
|
||||
$columnData = array();
|
||||
$fieldItems = new ArrayList();
|
||||
|
@ -94,7 +94,7 @@ class GridFieldPrintButton implements GridField_HTMLProvider, GridField_ActionPr
|
||||
* Export core.
|
||||
*/
|
||||
function generatePrintData($gridField) {
|
||||
$printColumns = ($this->printColumns) ? $this->printColumns : $gridField->getDisplayFields();
|
||||
$printColumns = ($this->printColumns) ? $this->printColumns : singleton($gridField->getModelClass())->summaryFields();
|
||||
$header = null;
|
||||
if($this->printHasHeader){
|
||||
$header = new ArrayList();
|
||||
|
@ -84,40 +84,6 @@ class GridFieldTest extends SapphireTest {
|
||||
$obj->getModelClass();
|
||||
}
|
||||
|
||||
/**
|
||||
* @covers GridField::getDisplayFields
|
||||
*/
|
||||
public function testGridFieldGetDefaultDisplayFields() {
|
||||
$obj = new GridField('testfield', 'testfield', DataList::create('Member'));
|
||||
$expected = array(
|
||||
'FirstName' => 'First Name',
|
||||
'Surname' => 'Last Name',
|
||||
'Email' => 'Email',
|
||||
);
|
||||
$this->assertEquals($expected, $obj->getDisplayFields());
|
||||
}
|
||||
|
||||
/**
|
||||
* @covers GridField::setDisplayFields
|
||||
* @covers GridField::getDisplayFields
|
||||
*/
|
||||
public function testGridFieldCustomDisplayFields() {
|
||||
$obj = new GridField('testfield', 'testfield', DataList::create('Member'));
|
||||
$expected = array('Email' => 'Email');
|
||||
$obj->setDisplayFields($expected);
|
||||
$this->assertEquals($expected, $obj->getDisplayFields());
|
||||
}
|
||||
|
||||
/**
|
||||
* @covers GridField::setDisplayFields
|
||||
* @covers GridField::getDisplayFields
|
||||
*/
|
||||
public function testGridFieldDisplayFieldsWithBadArguments() {
|
||||
$this->setExpectedException('InvalidArgumentException');
|
||||
$obj = new GridField('testfield', 'testfield', DataList::create('Member'));
|
||||
$obj->setDisplayFields(new stdClass());
|
||||
}
|
||||
|
||||
/**
|
||||
* @covers GridField::setList
|
||||
* @covers GridField::getList
|
||||
@ -284,28 +250,6 @@ class GridFieldTest extends SapphireTest {
|
||||
$this->assertEquals('handledAction is executed', $obj->handleAction('jump', array(), array()));
|
||||
}
|
||||
|
||||
/**
|
||||
* @covers GridField::getFieldCasting
|
||||
* @covers GridField::setFieldCasting
|
||||
*/
|
||||
public function testFieldCasting() {
|
||||
$obj = new GridField('testfield', 'testfield');
|
||||
$this->assertEquals(array(), $obj->getFieldCasting());
|
||||
$obj->setFieldCasting(array("MyShortText"=>"Text->FirstSentence"));
|
||||
$this->assertEquals(array("MyShortText"=>"Text->FirstSentence"), $obj->getFieldCasting());
|
||||
}
|
||||
|
||||
/**
|
||||
* @covers GridField::getFieldFormatting
|
||||
* @covers GridField::setFieldFormatting
|
||||
*/
|
||||
public function testFieldFormatting() {
|
||||
$obj = new GridField('testfield', 'testfield');
|
||||
$this->assertEquals(array(), $obj->getFieldFormatting());
|
||||
$obj->setFieldFormatting(array("myFieldName" => '<a href=\"custom-admin/$ID\">$ID</a>'));
|
||||
$this->assertEquals(array("myFieldName" => '<a href=\"custom-admin/$ID\">$ID</a>'), $obj->getFieldFormatting());
|
||||
}
|
||||
|
||||
/**
|
||||
* @covers GridField::getCastedValue
|
||||
*/
|
||||
|
64
tests/forms/gridfield/GridFieldDataColumnsTest.php
Normal file
64
tests/forms/gridfield/GridFieldDataColumnsTest.php
Normal file
@ -0,0 +1,64 @@
|
||||
<?php
|
||||
class GridFieldDataColumnsTest extends SapphireTest {
|
||||
|
||||
/**
|
||||
* @covers GridFieldDataColumns::getDisplayFields
|
||||
*/
|
||||
public function testGridFieldGetDefaultDisplayFields() {
|
||||
$obj = new GridField('testfield', 'testfield', DataList::create('Member'));
|
||||
$expected = array(
|
||||
'FirstName' => 'First Name',
|
||||
'Surname' => 'Last Name',
|
||||
'Email' => 'Email',
|
||||
);
|
||||
$columns = $obj->getConfig()->getComponentByType('GridFieldDataColumns');
|
||||
$this->assertEquals($expected, $columns->getDisplayFields($obj));
|
||||
}
|
||||
|
||||
/**
|
||||
* @covers GridFieldDataColumns::setDisplayFields
|
||||
* @covers GridFieldDataColumns::getDisplayFields
|
||||
*/
|
||||
public function testGridFieldCustomDisplayFields() {
|
||||
$obj = new GridField('testfield', 'testfield', DataList::create('Member'));
|
||||
$expected = array('Email' => 'Email');
|
||||
$columns = $obj->getConfig()->getComponentByType('GridFieldDataColumns');
|
||||
$columns->setDisplayFields($expected);
|
||||
$this->assertEquals($expected, $columns->getDisplayFields($obj));
|
||||
}
|
||||
|
||||
/**
|
||||
* @covers GridFieldDataColumns::setDisplayFields
|
||||
* @covers GridFieldDataColumns::getDisplayFields
|
||||
*/
|
||||
public function testGridFieldDisplayFieldsWithBadArguments() {
|
||||
$this->setExpectedException('InvalidArgumentException');
|
||||
$obj = new GridField('testfield', 'testfield', DataList::create('Member'));
|
||||
$columns = $obj->getConfig()->getComponentByType('GridFieldDataColumns');
|
||||
$columns->setDisplayFields(new stdClass());
|
||||
}
|
||||
|
||||
/**
|
||||
* @covers GridFieldDataColumns::getFieldCasting
|
||||
* @covers GridFieldDataColumns::setFieldCasting
|
||||
*/
|
||||
public function testFieldCasting() {
|
||||
$obj = new GridField('testfield', 'testfield');
|
||||
$columns = $obj->getConfig()->getComponentByType('GridFieldDataColumns');
|
||||
$this->assertEquals(array(), $columns->getFieldCasting());
|
||||
$columns->setFieldCasting(array("MyShortText"=>"Text->FirstSentence"));
|
||||
$this->assertEquals(array("MyShortText"=>"Text->FirstSentence"), $columns->getFieldCasting());
|
||||
}
|
||||
|
||||
/**
|
||||
* @covers GridFieldDataColumns::getFieldFormatting
|
||||
* @covers GridFieldDataColumns::setFieldFormatting
|
||||
*/
|
||||
public function testFieldFormatting() {
|
||||
$obj = new GridField('testfield', 'testfield');
|
||||
$columns = $obj->getConfig()->getComponentByType('GridFieldDataColumns');
|
||||
$this->assertEquals(array(), $columns->getFieldFormatting());
|
||||
$columns->setFieldFormatting(array("myFieldName" => '<a href=\"custom-admin/$ID\">$ID</a>'));
|
||||
$this->assertEquals(array("myFieldName" => '<a href=\"custom-admin/$ID\">$ID</a>'), $columns->getFieldFormatting());
|
||||
}
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user