|
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
|
|
|
* @return DataObject |
|
47
|
|
|
*/ |
|
48
|
|
|
public function duplicate($doWrite = true) |
|
49
|
|
|
{ |
|
50
|
|
|
$clonedNode = parent::duplicate(); |
|
51
|
|
|
|
|
52
|
|
|
if ($this->Options()) { |
|
53
|
|
|
foreach ($this->Options() as $field) { |
|
54
|
|
|
$newField = $field->duplicate(); |
|
55
|
|
|
$newField->ParentID = $clonedNode->ID; |
|
56
|
|
|
$newField->write(); |
|
57
|
|
|
} |
|
58
|
|
|
} |
|
59
|
|
|
|
|
60
|
|
|
return $clonedNode; |
|
61
|
|
|
} |
|
62
|
|
|
|
|
63
|
|
|
public function getFieldConfiguration() |
|
64
|
|
|
{ |
|
65
|
|
|
if (!$this->isInDB()) { |
|
66
|
|
|
$field = LiteralField::create('Options', '<p class="message notice">Once you save this field you will be able to add options</p>'); |
|
67
|
|
|
} else { |
|
68
|
|
|
$config = GridFieldConfig_RelationEditor::create() |
|
69
|
|
|
->addComponent(new GridFieldOrderableRows('Sort')); |
|
70
|
|
|
|
|
71
|
|
|
$config |
|
|
|
|
|
|
72
|
|
|
->getComponentByType('GridFieldDataColumns') |
|
73
|
|
|
->setDisplayFields([ |
|
74
|
|
|
'Name' => 'Name', |
|
75
|
|
|
'Title' => 'Title', |
|
76
|
|
|
'Default' => 'Default', |
|
77
|
|
|
]) |
|
78
|
|
|
->setFieldFormatting([ |
|
79
|
|
|
'Default' => function ($_, Moo_EditableFieldOption $option) { |
|
80
|
|
|
return $option->Default ? 'Yes' : 'No'; |
|
81
|
|
|
}, |
|
82
|
|
|
]); |
|
83
|
|
|
$field = GridField::create('Options', 'Options', $this->Options(), $config); |
|
84
|
|
|
} |
|
85
|
|
|
|
|
86
|
|
|
return [ |
|
87
|
|
|
$field, |
|
88
|
|
|
]; |
|
89
|
|
|
} |
|
90
|
|
|
|
|
91
|
|
|
/** |
|
92
|
|
|
* Return the form field for this object in the front end form view. |
|
93
|
|
|
* |
|
94
|
|
|
* @return FormField |
|
95
|
|
|
*/ |
|
96
|
|
|
protected function initFormField() |
|
97
|
|
|
{ |
|
98
|
|
|
$options = $this->Options()->map('EscapedTitle', 'Title'); |
|
99
|
|
|
|
|
100
|
|
|
return new OptionsetField($this->Name, $this->Title, $options); |
|
101
|
|
|
} |
|
102
|
|
|
} |
|
103
|
|
|
|
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: