6
6
use SilverStripe \View \Requirements ;
7
7
use SilverStripe \View \ArrayData ;
8
8
use SilverStripe \ORM \ArrayList ;
9
+ use SilverStripe \ORM \SS_List ;
9
10
use SilverStripe \Forms \FormField ;
10
11
11
12
class DropdownImageField extends DropdownField {
12
13
13
- protected $ keyField , $ labelField , $ imageField ;
14
+ protected $ keyField = 'ID ' ;
15
+
16
+ protected $ labelField = 'Title ' ;
17
+
18
+ protected $ imageField = 'Image ' ;
19
+
20
+ protected $ sourceObject ;
14
21
15
22
public function __construct ($ name , $ title , $ sourceObject , $ keyField = 'ID ' , $ labelField = 'Title ' , $ imageField = 'Image ' , $ value = '' , $ form = null ) {
16
23
@@ -21,13 +28,29 @@ public function __construct($name, $title, $sourceObject, $keyField = 'ID', $lab
21
28
parent ::__construct ($ name , ($ title === null ) ? $ name : $ title , $ sourceObject , $ value , $ form );
22
29
23
30
$ this ->addExtraClass ('dropdown ' );
31
+ $ this ->sourceObject = $ sourceObject ;
32
+ }
33
+
34
+ public function setSourceObject (SS_List $ source )
35
+ {
36
+ $ this ->sourceObject = $ source ;
37
+
38
+ return $ this ;
39
+ }
40
+
41
+ public function setImageField ($ field )
42
+ {
43
+ $ this ->imageField = $ field ;
44
+
45
+ return $ this ;
24
46
}
25
47
26
48
public function Field ($ properties = array ()) {
27
49
28
50
$ dirName = basename (dirname (dirname (__FILE__ )));
29
- $ source = $ this ->getSource () ;
51
+ $ source = $ this ->sourceObject ;
30
52
$ options = array ();
53
+
31
54
if ($ source ) {
32
55
if (is_object ($ source ) && $ this ->hasEmptyDefault ) {
33
56
$ options [] = new ArrayData ([
@@ -37,16 +60,22 @@ public function Field($properties = array()) {
37
60
]);
38
61
}
39
62
40
- foreach ($ source as $ item ) {
41
- $ value = $ item ->{$ this ->keyField };
42
- if (empty ($ this ->labelField )) {
43
- $ title = '--nbsp ' ;
63
+ foreach ($ source as $ k => $ item ) {
64
+ if (is_object ($ item )) {
65
+ $ value = $ item ->{$ this ->keyField };
66
+ if (empty ($ this ->labelField )) {
67
+ $ title = '--nbsp ' ;
68
+ } else {
69
+ $ title = $ item ->{$ this ->labelField };
70
+ }
71
+
72
+ $ image = $ item ->{$ this ->imageField }();
44
73
} else {
45
- $ title = $ item ->{$ this ->labelField };
74
+ $ value = $ k ;
75
+ $ image = null ;
76
+ $ title = $ item ;
46
77
}
47
78
48
- $ image = $ item ->{$ this ->imageField }();
49
-
50
79
$ selected = false ;
51
80
if ($ value === '' && ($ this ->value === '' || $ this ->value === null )) {
52
81
$ selected = true ;
0 commit comments