Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

ENHANCEMENT: Fixes #7010. Move DisplayFields, FieldCasting and FieldF…

…ormatting functions from GridField to GridFieldDataColumns.
  • Loading branch information...
commit 842784c8aabac3a08bd4810b3ac2e3bc18dc9968 1 parent b8faa41
@robert-h-curry robert-h-curry authored
View
8 admin/code/SecurityAdmin.php
@@ -52,14 +52,16 @@ public function getEditForm($id = null, $fields = null) {
)->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(
View
3  forms/HtmlEditorField.php
@@ -371,7 +371,8 @@ function MediaForm() {
$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'),
));
View
85 forms/gridfield/GridField.php
@@ -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;
@@ -56,13 +50,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 @@ function index($request) {
* this modelclass $summary_fields
*
* @param string $modelClassName
- * @see GridField::getDisplayFields()
+ * @see GridFieldDataColumns::getDisplayFields()
*/
public function setModelClass($modelClassName) {
$this->modelClassName = $modelClassName;
@@ -144,19 +131,6 @@ protected function setComponents(GridFieldConfig $config) {
}
/**
- * 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
*
* @return GridFieldConfig
@@ -166,63 +140,6 @@ public function getConfig() {
}
/**
- * 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
*
* @param $value
View
2  forms/gridfield/GridFieldComponent.php
@@ -36,7 +36,7 @@ function getHTMLFragments($gridField);
/**
* 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.
View
95 forms/gridfield/GridFieldDataColumns.php
@@ -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 @@ public function getColumnContent($gridField, $item, $column) {
$tmpItem = $tmpItem->$relationMethod();
}
}
- }
+ }
}
$value = $this->castValue($gridField, $column, $value);
@@ -63,7 +142,7 @@ public function getColumnAttributes($gridField, $item, $column) {
}
public function getColumnMetadata($gridField, $column) {
- $columns = $gridField->getDisplayFields();
+ $columns = $this->getDisplayFields($gridField);
return array(
'title' => $columns[$column],
);
@@ -76,8 +155,8 @@ public function getColumnMetadata($gridField, $column) {
* @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 @@ protected function castValue($gridField, $fieldName, $value) {
* @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 {
View
2  forms/gridfield/GridFieldExportButton.php
@@ -99,7 +99,7 @@ public function handleExport($gridField, $request = null) {
*/
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();
View
2  forms/gridfield/GridFieldPrintButton.php
@@ -94,7 +94,7 @@ public function handlePrint($gridField, $request = null) {
* 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();
View
56 tests/forms/GridFieldTest.php
@@ -85,40 +85,6 @@ public function testGridFieldModelClassThrowsException() {
}
/**
- * @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
*/
@@ -285,28 +251,6 @@ public function testHandleAction() {
}
/**
- * @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
*/
public function testGetCastedValue() {
View
64 tests/forms/gridfield/GridFieldDataColumnsTest.php
@@ -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());
+ }
+}
Please sign in to comment.
Something went wrong with that request. Please try again.