1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* Moo_EditableFieldMultipleOption is a base class for multiple option fields to extend. |
5
|
|
|
* |
6
|
|
|
* @see Moo_EditableFieldCheckboxGroup, Moo_EditableFieldDropdown |
7
|
|
|
* |
8
|
|
|
* @package editablefield |
9
|
|
|
* |
10
|
|
|
* @author Mohamed Alsharaf <[email protected]> |
11
|
|
|
* |
12
|
|
|
* @method HasManyList Options() |
13
|
|
|
* |
14
|
|
|
* @property string $Name |
15
|
|
|
* @property string $Title |
16
|
|
|
*/ |
17
|
|
|
class Moo_EditableFieldMultipleOption extends Moo_EditableField |
18
|
|
|
{ |
19
|
|
|
private static $has_many = [ |
20
|
|
|
'Options' => 'Moo_EditableFieldOption', |
21
|
|
|
]; |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* Deletes all the options attached to this field before deleting the |
25
|
|
|
* field. Keeps stray options from floating around. |
26
|
|
|
* |
27
|
|
|
* @return void |
28
|
|
|
*/ |
29
|
|
|
public function delete() |
30
|
|
|
{ |
31
|
|
|
$options = $this->Options(); |
32
|
|
|
|
33
|
|
|
if ($options) { |
34
|
|
|
foreach ($options as $option) { |
35
|
|
|
$option->delete(); |
36
|
|
|
} |
37
|
|
|
} |
38
|
|
|
|
39
|
|
|
parent::delete(); |
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* Duplicate a pages content. We need to make sure all the fields attached |
44
|
|
|
* to that page go with it. |
45
|
|
|
* |
46
|
|
|
* @param bool $doWrite |
47
|
|
|
* |
48
|
|
|
* @return DataObject |
49
|
|
|
*/ |
50
|
1 |
|
public function duplicate($doWrite = true) |
51
|
|
|
{ |
52
|
1 |
|
$clonedNode = parent::duplicate($doWrite); |
53
|
|
|
|
54
|
1 |
|
if ($this->Options()) { |
55
|
1 |
|
foreach ($this->Options() as $field) { |
56
|
1 |
|
$newField = $field->duplicate(); |
57
|
1 |
|
$newField->ParentID = $clonedNode->ID; |
58
|
1 |
|
$newField->write(); |
59
|
1 |
|
} |
60
|
1 |
|
} |
61
|
|
|
|
62
|
1 |
|
return $clonedNode; |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
/** |
66
|
|
|
* Get extra configuration fields. |
67
|
|
|
* |
68
|
|
|
* @return array |
69
|
|
|
*/ |
70
|
|
|
public function getFieldConfiguration() |
71
|
|
|
{ |
72
|
|
|
if (!$this->isInDB()) { |
73
|
|
|
$field = LiteralField::create('Options', '<p class="message notice">Once you save this field you will be able to add options</p>'); |
74
|
|
|
} else { |
75
|
|
|
$config = GridFieldConfig_RelationEditor::create() |
76
|
|
|
->addComponent(new GridFieldOrderableRows('Sort')); |
77
|
|
|
|
78
|
|
|
$config |
|
|
|
|
79
|
|
|
->getComponentByType('GridFieldDataColumns') |
80
|
|
|
->setDisplayFields([ |
81
|
|
|
'Name' => 'Name', |
82
|
|
|
'Title' => 'Title', |
83
|
|
|
'Default' => 'Default', |
84
|
|
|
]) |
85
|
|
|
->setFieldFormatting([ |
86
|
|
|
'Default' => function ($_, Moo_EditableFieldOption $option) { |
87
|
|
|
return $option->Default ? 'Yes' : 'No'; |
88
|
|
|
}, |
89
|
|
|
]); |
90
|
|
|
$field = GridField::create('Options', 'Options', $this->Options(), $config); |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
return [ |
94
|
|
|
$field, |
95
|
|
|
]; |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
/** |
99
|
|
|
* Return the form field for this object in the front end form view. |
100
|
|
|
* |
101
|
|
|
* @return FormField |
102
|
|
|
*/ |
103
|
1 |
|
protected function initFormField() |
104
|
|
|
{ |
105
|
1 |
|
$options = $this->Options()->map('EscapedTitle', 'Title'); |
106
|
|
|
|
107
|
1 |
|
return new OptionsetField($this->Name, $this->Title, $options); |
108
|
|
|
} |
109
|
|
|
} |
110
|
|
|
|
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: