Skip to content
This repository
Browse code

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

…ormatting functions from GridField to GridFieldDataColumns.
  • Loading branch information...
commit 842784c8aabac3a08bd4810b3ac2e3bc18dc9968 1 parent b8faa41
Robert Curry authored April 19, 2012
8  admin/code/SecurityAdmin.php
@@ -52,14 +52,16 @@ public function getEditForm($id = null, $fields = null) {
52 52
 		)->addExtraClass("members_grid");
53 53
 		$memberListConfig->getComponentByType('GridFieldDetailForm')->setValidator(new Member_Validator());
54 54
 
55  
-		$groupList = GridField::create(			'Groups',
  55
+		$groupList = GridField::create(
  56
+			'Groups',
56 57
 			false,
57 58
 			DataList::create('Group'),
58 59
 			GridFieldConfig_RecordEditor::create()
59  
-		)->setDisplayFields(array(
  60
+		);
  61
+		$columns = $groupList->getConfig()->getComponentByType('GridFieldDataColumns');
  62
+		$columns->setDisplayFields(array(
60 63
 			'Breadcrumbs' => singleton('Group')->fieldLabel('Title')
61 64
 		));
62  
-
63 65
 		
64 66
 		$fields = new FieldList(
65 67
 			$root = new TabSet(
3  forms/HtmlEditorField.php
@@ -371,7 +371,8 @@ function MediaForm() {
371 371
 		$fileField->setList($this->getFiles($parentID));
372 372
 		$fileField->setAttribute('data-selectable', true);
373 373
 		$fileField->setAttribute('data-multiselect', true);
374  
-		$fileField->setDisplayFields(array(
  374
+		$columns = $fileField->getConfig()->getComponentByType('GridFieldDataColumns');
  375
+		$columns->setDisplayFields(array(
375 376
 			'CMSThumbnail' => false,
376 377
 			'Name' => _t('File.Name'),
377 378
 		));
85  forms/gridfield/GridField.php
@@ -34,12 +34,6 @@ class GridField extends FormField {
34 34
 
35 35
 	/** @var string - the classname of the DataObject that the GridField will display. Defaults to the value of $this->list->dataClass */
36 36
 	protected $modelClassName = '';
37  
-	
38  
-	/** @var array */
39  
-	public $fieldCasting = array();
40  
-
41  
-	/** @var array */
42  
-	public $fieldFormatting = array();
43 37
 
44 38
 	/** @var GridState - the current state of the GridField */
45 39
 	protected $state = null;
@@ -56,13 +50,6 @@ class GridField extends FormField {
56 50
 	protected $components = array();
57 51
 	
58 52
 	/**
59  
-	 * This is the columns that will be visible
60  
-	 *
61  
-	 * @var array
62  
-	 */
63  
-	protected $displayFields = array();
64  
-	
65  
-	/**
66 53
 	 * Internal dispatcher for column handlers.
67 54
 	 * Keys are column names and values are GridField_ColumnProvider objects
68 55
 	 * 
@@ -109,7 +96,7 @@ function index($request) {
109 96
 	 * this modelclass $summary_fields
110 97
 	 * 
111 98
 	 * @param string $modelClassName
112  
-	 * @see GridField::getDisplayFields()
  99
+	 * @see GridFieldDataColumns::getDisplayFields()
113 100
 	 */
114 101
 	public function setModelClass($modelClassName) {
115 102
 		$this->modelClassName = $modelClassName;
@@ -144,19 +131,6 @@ protected function setComponents(GridFieldConfig $config) {
144 131
 	}
145 132
 
146 133
 	/**
147  
-	 * Get the DisplayFields
148  
-	 * 
149  
-	 * @return array
150  
-	 * @see GridField::setDisplayFields
151  
-	 */
152  
-	public function getDisplayFields() {
153  
-		if(!$this->displayFields) {
154  
-			return singleton($this->getModelClass())->summaryFields();
155  
-		}
156  
-		return $this->displayFields;
157  
-	}
158  
-	
159  
-	/**
160 134
 	 * Get the GridFieldConfig
161 135
 	 *
162 136
 	 * @return GridFieldConfig
@@ -166,63 +140,6 @@ public function getConfig() {
166 140
 	}
167 141
 	
168 142
 	/**
169  
-	 * Override the default behaviour of showing the models summaryFields with
170  
-	 * these fields instead
171  
-	 * Example: array( 'Name' => 'Members name', 'Email' => 'Email address')
172  
-	 *
173  
-	 * @param array $fields 
174  
-	 */
175  
-	public function setDisplayFields($fields) {
176  
-		if(!is_array($fields)) {
177  
-			throw new InvalidArgumentException('Arguments passed to GridField::setDisplayFields() must be an array');
178  
-		}
179  
-		$this->displayFields = $fields;
180  
-		return $this;
181  
-	}
182  
-
183  
-	/**
184  
-	 * Specify castings with fieldname as the key, and the desired casting as value.
185  
-	 * Example: array("MyCustomDate"=>"Date","MyShortText"=>"Text->FirstSentence")
186  
-	 *
187  
-	 * @param array $casting
188  
-	 * @todo refactor this into GridFieldComponent
189  
-	 */
190  
-	public function setFieldCasting($casting) {
191  
-		$this->fieldCasting = $casting;
192  
-		return $this;
193  
-	}
194  
-
195  
-	/**
196  
-	 * Specify custom formatting for fields, e.g. to render a link instead of pure text.
197  
-	 * Caution: Make sure to escape special php-characters like in a normal php-statement.
198  
-	 * Example:	"myFieldName" => '<a href=\"custom-admin/$ID\">$ID</a>'.
199  
-	 * Alternatively, pass a anonymous function, which takes one parameter: The list item.
200  
-	 *
201  
-	 * @param array $casting
202  
-	 * @todo refactor this into GridFieldComponent
203  
-	 */
204  
-	public function getFieldCasting() {
205  
-		return $this->fieldCasting;
206  
-	}
207  
-
208  
-	/**
209  
-	 * @param array $casting
210  
-	 * @todo refactor this into GridFieldComponent
211  
-	 */
212  
-	public function setFieldFormatting($formatting) {
213  
-		$this->fieldFormatting = $formatting;
214  
-		return $this;
215  
-	}
216  
-
217  
-	/**
218  
-	 * @param array $casting
219  
-	 * @todo refactor this into GridFieldComponent
220  
-	 */
221  
-	public function getFieldFormatting() {
222  
-		return $this->fieldFormatting;
223  
-	}
224  
-	
225  
-	/**
226 143
 	 * Cast a arbitrary value with the help of a castingDefintion
227 144
 	 * 
228 145
 	 * @param $value 
2  forms/gridfield/GridFieldComponent.php
@@ -36,7 +36,7 @@ function getHTMLFragments($gridField);
36 36
 
37 37
 	/**
38 38
 	 * Modify the list of columns displayed in the table.
39  
-	 * See {@link GridField->getDisplayFields()} and {@link GridFieldDataColumns}.
  39
+	 * See {@link GridFieldDataColumns->getDisplayFields()} and {@link GridFieldDataColumns}.
40 40
 	 * 
41 41
 	 * @param  GridField
42 42
 	 * @param  Array List reference of all column names.
95  forms/gridfield/GridFieldDataColumns.php
@@ -8,13 +8,92 @@
8 8
  */
9 9
 class GridFieldDataColumns implements GridField_ColumnProvider {
10 10
 
  11
+	/** @var array */
  12
+	public $fieldCasting = array();
  13
+
  14
+	/** @var array */
  15
+	public $fieldFormatting = array();
  16
+	
  17
+	/**
  18
+	 * This is the columns that will be visible
  19
+	 *
  20
+	 * @var array
  21
+	 */
  22
+	protected $displayFields = array();
  23
+
11 24
 	public function augmentColumns($gridField, &$columns) {
12  
-		$baseColumns = array_keys($gridField->getDisplayFields());
  25
+		$baseColumns = array_keys($this->getDisplayFields($gridField));
13 26
 		foreach($baseColumns as $col) $columns[] = $col;
14 27
 	}
15 28
 
16 29
 	public function getColumnsHandled($gridField) {
17  
-		return array_keys($gridField->getDisplayFields());
  30
+		return array_keys($this->getDisplayFields($gridField));
  31
+	}
  32
+	
  33
+	/**
  34
+	 * Override the default behaviour of showing the models summaryFields with
  35
+	 * these fields instead
  36
+	 * Example: array( 'Name' => 'Members name', 'Email' => 'Email address')
  37
+	 *
  38
+	 * @param array $fields 
  39
+	 */
  40
+	public function setDisplayFields($fields) {
  41
+		if(!is_array($fields)) {
  42
+			throw new InvalidArgumentException('Arguments passed to GridFieldDataColumns::setDisplayFields() must be an array');
  43
+		}
  44
+		$this->displayFields = $fields;
  45
+		return $this;
  46
+	}
  47
+
  48
+	/**
  49
+	 * Get the DisplayFields
  50
+	 * 
  51
+	 * @return array
  52
+	 * @see GridFieldDataColumns::setDisplayFields
  53
+	 */
  54
+	public function getDisplayFields($gridField) {
  55
+		if(!$this->displayFields) {
  56
+			return singleton($gridField->getModelClass())->summaryFields();
  57
+		}
  58
+		return $this->displayFields;
  59
+	}
  60
+
  61
+	/**
  62
+	 * Specify castings with fieldname as the key, and the desired casting as value.
  63
+	 * Example: array("MyCustomDate"=>"Date","MyShortText"=>"Text->FirstSentence")
  64
+	 *
  65
+	 * @param array $casting
  66
+	 */
  67
+	public function setFieldCasting($casting) {
  68
+		$this->fieldCasting = $casting;
  69
+		return $this;
  70
+	}
  71
+
  72
+	/**
  73
+	 * Specify custom formatting for fields, e.g. to render a link instead of pure text.
  74
+	 * Caution: Make sure to escape special php-characters like in a normal php-statement.
  75
+	 * Example:	"myFieldName" => '<a href=\"custom-admin/$ID\">$ID</a>'.
  76
+	 * Alternatively, pass a anonymous function, which takes one parameter: The list item.
  77
+	 *
  78
+	 * @return array
  79
+	 */
  80
+	public function getFieldCasting() {
  81
+		return $this->fieldCasting;
  82
+	}
  83
+
  84
+	/**
  85
+	 * @param array $formatting
  86
+	 */
  87
+	public function setFieldFormatting($formatting) {
  88
+		$this->fieldFormatting = $formatting;
  89
+		return $this;
  90
+	}
  91
+
  92
+	/**
  93
+	 * @return array
  94
+	 */
  95
+	public function getFieldFormatting() {
  96
+		return $this->fieldFormatting;
18 97
 	}
19 98
 
20 99
 	/**
@@ -48,7 +127,7 @@ public function getColumnContent($gridField, $item, $column) {
48 127
 						$tmpItem = $tmpItem->$relationMethod();
49 128
 					}
50 129
 				}
51  
-			}			
  130
+			}
52 131
 		}
53 132
 
54 133
 		$value = $this->castValue($gridField, $column, $value);
@@ -63,7 +142,7 @@ public function getColumnAttributes($gridField, $item, $column) {
63 142
 	}
64 143
 	
65 144
 	public function getColumnMetadata($gridField, $column) {
66  
-		$columns = $gridField->getDisplayFields();
  145
+		$columns = $this->getDisplayFields($gridField);
67 146
 		return array(
68 147
 			'title' => $columns[$column],
69 148
 		);
@@ -76,8 +155,8 @@ public function getColumnMetadata($gridField, $column) {
76 155
 	 * @return type 
77 156
 	 */
78 157
 	protected function castValue($gridField, $fieldName, $value) {
79  
-		if(array_key_exists($fieldName, $gridField->FieldCasting)) {
80  
-			return $gridField->getCastedValue($value, $gridField->FieldCasting[$fieldName]);
  158
+		if(array_key_exists($fieldName, $this->fieldCasting)) {
  159
+			return $gridField->getCastedValue($value, $this->fieldCasting[$fieldName]);
81 160
 		} elseif(is_object($value) && method_exists($value, 'Nice')) {
82 161
 			return $value->Nice();
83 162
 		}
@@ -91,11 +170,11 @@ protected function castValue($gridField, $fieldName, $value) {
91 170
 	 * @return type 
92 171
 	 */
93 172
 	protected function formatValue($gridField, $item, $fieldName, $value) {
94  
-		if(!array_key_exists($fieldName, $gridField->FieldFormatting)) {
  173
+		if(!array_key_exists($fieldName, $this->fieldFormatting)) {
95 174
 			return $value;
96 175
 		}
97 176
 
98  
-		$spec = $gridField->FieldFormatting[$fieldName];
  177
+		$spec = $this->fieldFormatting[$fieldName];
99 178
 		if(is_callable($spec)) {
100 179
 			return $spec($item);
101 180
 		} else {
2  forms/gridfield/GridFieldExportButton.php
@@ -99,7 +99,7 @@ public function handleExport($gridField, $request = null) {
99 99
  	 */
100 100
 	function generateExportFileData($gridField) {
101 101
 		$separator = $this->csvSeparator;
102  
-		$csvColumns = ($this->exportColumns) ? $this->exportColumns : $gridField->getDisplayFields();
  102
+		$csvColumns = ($this->exportColumns) ? $this->exportColumns : singleton($gridField->getModelClass())->summaryFields();
103 103
 		$fileData = '';
104 104
 		$columnData = array();
105 105
 		$fieldItems = new ArrayList();
2  forms/gridfield/GridFieldPrintButton.php
@@ -94,7 +94,7 @@ public function handlePrint($gridField, $request = null) {
94 94
 	 * Export core.
95 95
  	 */
96 96
 	function generatePrintData($gridField) {
97  
-		$printColumns = ($this->printColumns) ? $this->printColumns : $gridField->getDisplayFields();
  97
+		$printColumns = ($this->printColumns) ? $this->printColumns : singleton($gridField->getModelClass())->summaryFields();
98 98
 		$header = null;
99 99
 		if($this->printHasHeader){
100 100
 			$header = new ArrayList();
56  tests/forms/GridFieldTest.php
@@ -85,40 +85,6 @@ public function testGridFieldModelClassThrowsException() {
85 85
 	}
86 86
 
87 87
 	/**
88  
-	 * @covers GridField::getDisplayFields
89  
-	 */
90  
-	public function testGridFieldGetDefaultDisplayFields() {
91  
-		$obj = new GridField('testfield', 'testfield', DataList::create('Member'));
92  
-		$expected = array(
93  
-			'FirstName' => 'First Name',
94  
-			'Surname' => 'Last Name',
95  
-			'Email' => 'Email',
96  
-		);
97  
-		$this->assertEquals($expected, $obj->getDisplayFields());
98  
-	}
99  
-
100  
-	/**
101  
-	 * @covers GridField::setDisplayFields
102  
-	 * @covers GridField::getDisplayFields
103  
-	 */
104  
-	public function testGridFieldCustomDisplayFields() {
105  
-		$obj = new GridField('testfield', 'testfield', DataList::create('Member'));
106  
-		$expected = array('Email' => 'Email');
107  
-		$obj->setDisplayFields($expected);
108  
-		$this->assertEquals($expected, $obj->getDisplayFields());
109  
-	}
110  
-
111  
-	/**
112  
-	 * @covers GridField::setDisplayFields
113  
-	 * @covers GridField::getDisplayFields
114  
-	 */
115  
-	public function testGridFieldDisplayFieldsWithBadArguments() {
116  
-		$this->setExpectedException('InvalidArgumentException');
117  
-		$obj = new GridField('testfield', 'testfield', DataList::create('Member'));
118  
-		$obj->setDisplayFields(new stdClass());
119  
-	}
120  
-
121  
-	/**
122 88
 	 * @covers GridField::setList
123 89
 	 * @covers GridField::getList
124 90
 	 */
@@ -285,28 +251,6 @@ public function testHandleAction() {
285 251
 	}
286 252
 
287 253
 	/**
288  
-	 * @covers GridField::getFieldCasting
289  
-	 * @covers GridField::setFieldCasting
290  
-	 */
291  
-	public function testFieldCasting() {
292  
-		$obj = new GridField('testfield', 'testfield');
293  
-		$this->assertEquals(array(), $obj->getFieldCasting());
294  
-		$obj->setFieldCasting(array("MyShortText"=>"Text->FirstSentence"));
295  
-		$this->assertEquals(array("MyShortText"=>"Text->FirstSentence"), $obj->getFieldCasting());
296  
-	}
297  
-	
298  
-	/**
299  
-	 * @covers GridField::getFieldFormatting
300  
-	 * @covers GridField::setFieldFormatting
301  
-	 */
302  
-	public function testFieldFormatting() {
303  
-		$obj = new GridField('testfield', 'testfield');
304  
-		$this->assertEquals(array(), $obj->getFieldFormatting());
305  
-		$obj->setFieldFormatting(array("myFieldName" => '<a href=\"custom-admin/$ID\">$ID</a>'));
306  
-		$this->assertEquals(array("myFieldName" => '<a href=\"custom-admin/$ID\">$ID</a>'), $obj->getFieldFormatting());
307  
-	}
308  
-
309  
-	/**
310 254
 	 * @covers GridField::getCastedValue
311 255
 	 */
312 256
 	public function testGetCastedValue() {
64  tests/forms/gridfield/GridFieldDataColumnsTest.php
... ...
@@ -0,0 +1,64 @@
  1
+<?php
  2
+class GridFieldDataColumnsTest extends SapphireTest {
  3
+
  4
+	/**
  5
+	 * @covers GridFieldDataColumns::getDisplayFields
  6
+	 */
  7
+	public function testGridFieldGetDefaultDisplayFields() {
  8
+		$obj = new GridField('testfield', 'testfield', DataList::create('Member'));
  9
+		$expected = array(
  10
+			'FirstName' => 'First Name',
  11
+			'Surname' => 'Last Name',
  12
+			'Email' => 'Email',
  13
+		);
  14
+		$columns = $obj->getConfig()->getComponentByType('GridFieldDataColumns');
  15
+		$this->assertEquals($expected, $columns->getDisplayFields($obj));
  16
+	}
  17
+
  18
+	/**
  19
+	 * @covers GridFieldDataColumns::setDisplayFields
  20
+	 * @covers GridFieldDataColumns::getDisplayFields
  21
+	 */
  22
+	public function testGridFieldCustomDisplayFields() {
  23
+		$obj = new GridField('testfield', 'testfield', DataList::create('Member'));
  24
+		$expected = array('Email' => 'Email');
  25
+		$columns = $obj->getConfig()->getComponentByType('GridFieldDataColumns');
  26
+		$columns->setDisplayFields($expected);
  27
+		$this->assertEquals($expected, $columns->getDisplayFields($obj));
  28
+	}
  29
+
  30
+	/**
  31
+	 * @covers GridFieldDataColumns::setDisplayFields
  32
+	 * @covers GridFieldDataColumns::getDisplayFields
  33
+	 */
  34
+	public function testGridFieldDisplayFieldsWithBadArguments() {
  35
+		$this->setExpectedException('InvalidArgumentException');
  36
+		$obj = new GridField('testfield', 'testfield', DataList::create('Member'));
  37
+		$columns = $obj->getConfig()->getComponentByType('GridFieldDataColumns');
  38
+		$columns->setDisplayFields(new stdClass());
  39
+	}
  40
+
  41
+	/**
  42
+	 * @covers GridFieldDataColumns::getFieldCasting
  43
+	 * @covers GridFieldDataColumns::setFieldCasting
  44
+	 */
  45
+	public function testFieldCasting() {
  46
+		$obj = new GridField('testfield', 'testfield');
  47
+		$columns = $obj->getConfig()->getComponentByType('GridFieldDataColumns');
  48
+		$this->assertEquals(array(), $columns->getFieldCasting());
  49
+		$columns->setFieldCasting(array("MyShortText"=>"Text->FirstSentence"));
  50
+		$this->assertEquals(array("MyShortText"=>"Text->FirstSentence"), $columns->getFieldCasting());
  51
+	}
  52
+
  53
+	/**
  54
+	 * @covers GridFieldDataColumns::getFieldFormatting
  55
+	 * @covers GridFieldDataColumns::setFieldFormatting
  56
+	 */
  57
+	public function testFieldFormatting() {
  58
+		$obj = new GridField('testfield', 'testfield');
  59
+		$columns = $obj->getConfig()->getComponentByType('GridFieldDataColumns');
  60
+		$this->assertEquals(array(), $columns->getFieldFormatting());
  61
+		$columns->setFieldFormatting(array("myFieldName" => '<a href=\"custom-admin/$ID\">$ID</a>'));
  62
+		$this->assertEquals(array("myFieldName" => '<a href=\"custom-admin/$ID\">$ID</a>'), $columns->getFieldFormatting());
  63
+	}
  64
+}

0 notes on commit 842784c

Please sign in to comment.
Something went wrong with that request. Please try again.