1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* Moo_EditableFieldGroup is a data object class for editable field group. |
5
|
|
|
* |
6
|
|
|
* @author Mohamed Alsharaf <[email protected]> |
7
|
|
|
* |
8
|
|
|
* @method ManyManyList Fields() |
9
|
|
|
*/ |
10
|
|
|
class Moo_EditableFieldGroup extends DataObject |
11
|
|
|
{ |
12
|
|
|
private static $db = [ |
13
|
|
|
'Title' => 'Varchar(255)', |
14
|
|
|
]; |
15
|
|
|
|
16
|
|
|
private static $many_many = [ |
17
|
|
|
'Fields' => 'Moo_EditableField', |
18
|
|
|
]; |
19
|
|
|
|
20
|
|
|
private static $many_many_extraFields = [ |
21
|
|
|
'Fields' => [ |
22
|
|
|
'Sort' => 'Int', |
23
|
|
|
], |
24
|
|
|
]; |
25
|
|
|
|
26
|
|
|
private static $default_sort = '"Title"'; |
27
|
|
|
|
28
|
|
|
private static $singular_name = 'Group'; |
29
|
|
|
|
30
|
|
|
private static $plural_name = 'Groups'; |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* Make sure the title is required field. |
34
|
|
|
* |
35
|
|
|
* @return RequiredFields |
36
|
|
|
*/ |
37
|
1 |
|
public function getCMSValidator() |
38
|
|
|
{ |
39
|
1 |
|
return new RequiredFields('Title'); |
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* Returns form fields for adding/editing the data object. |
44
|
|
|
* |
45
|
|
|
* @return FieldList |
46
|
|
|
*/ |
47
|
1 |
|
public function getCMSFields() |
48
|
|
|
{ |
49
|
1 |
|
$fields = parent::getCMSFields(); |
50
|
|
|
|
51
|
|
|
// Field visible on edit only |
52
|
1 |
|
if ($this->isInDB()) { |
53
|
1 |
|
$config = GridFieldConfig_RelationEditor::create(); |
54
|
|
|
$config |
55
|
1 |
|
->getComponentByType('GridFieldPaginator') |
56
|
1 |
|
->setItemsPerPage(10); |
57
|
1 |
|
$config->removeComponentsByType('GridFieldAddNewButton'); |
58
|
1 |
|
$config->removeComponentsByType('GridFieldEditButton'); |
59
|
|
|
$config |
|
|
|
|
60
|
1 |
|
->getComponentByType('GridFieldDataColumns') |
61
|
1 |
|
->setDisplayFields([ |
62
|
1 |
|
'Name' => _t('Moo_EditableFieldGroup.NAME', 'Name'), |
63
|
1 |
|
'Title' => _t('Moo_EditableFieldGroup.TITLE', 'Title'), |
64
|
1 |
|
'Sort' => _t('Moo_EditableFieldGroup.SORT', 'Sort'), |
65
|
1 |
|
]); |
66
|
1 |
|
$config->addComponent(new GridFieldOrderableRows('Sort')); |
67
|
1 |
|
$field = new GridField('Fields', 'Fields', $this->Fields(), $config); |
68
|
1 |
|
$fields->addFieldToTab('Root.Fields', $field); |
69
|
1 |
|
} |
70
|
|
|
|
71
|
1 |
|
return $fields; |
72
|
|
|
} |
73
|
|
|
} |
74
|
|
|
|
Let’s take a look at an example:
In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different implementation of User which does not have a getDisplayName() method, the code will break.
Available Fixes
Change the type-hint for the parameter:
Add an additional type-check:
Add the method to the interface: