|
3 | 3 | namespace FullscreenInteractive\DropdownImageField; |
4 | 4 |
|
5 | 5 | use SilverStripe\Forms\DropdownField; |
6 | | -use SilverStripe\ORM\SS_List; |
7 | | -use SilverStripe\Forms\FormField; |
8 | 6 | use SilverStripe\Model\ArrayData; |
9 | 7 | use SilverStripe\Model\List\ArrayList; |
10 | 8 |
|
11 | 9 | class DropdownImageField extends DropdownField |
12 | 10 | { |
| 11 | + protected string $keyField = 'ID'; |
13 | 12 |
|
14 | | - protected $keyField = 'ID'; |
| 13 | + protected string $labelField = 'Title'; |
15 | 14 |
|
16 | | - protected $labelField = 'Title'; |
| 15 | + protected string $imageField = 'Image'; |
17 | 16 |
|
18 | | - protected $imageField = 'Image'; |
| 17 | + protected ArrayList $sourceList; |
| 18 | + |
| 19 | + public function __construct($name, $title = null, $source = [], $value = null) |
| 20 | + { |
| 21 | + parent::__construct($name, $title, $source, $value); |
| 22 | + |
| 23 | + $this->addExtraClass('dropdown'); |
| 24 | + } |
19 | 25 |
|
20 | | - protected $sourceObject; |
21 | 26 |
|
22 | | - public function __construct($name, $title, $sourceObject, $keyField = 'ID', $labelField = 'Title', $imageField = 'Image', $value = '', $form = null) |
| 27 | + public function setSourceList(ArrayList $sourceList) |
23 | 28 | { |
| 29 | + $this->sourceList = $sourceList; |
24 | 30 |
|
25 | | - $this->keyField = $keyField; |
26 | | - $this->labelField = $labelField; |
27 | | - $this->imageField = $imageField; |
| 31 | + return $this; |
| 32 | + } |
28 | 33 |
|
29 | | - parent::__construct($name, ($title === null) ? $name : $title, $sourceObject, $value, $form); |
30 | 34 |
|
31 | | - $this->addExtraClass('dropdown'); |
32 | | - $this->sourceObject = $sourceObject; |
| 35 | + public function setKeyField(string $field) |
| 36 | + { |
| 37 | + $this->keyField = $field; |
| 38 | + |
| 39 | + return $this; |
33 | 40 | } |
34 | 41 |
|
35 | | - public function setSourceObject($source) |
| 42 | + public function setLabelField(string $field) |
36 | 43 | { |
37 | | - $this->sourceObject = $source; |
| 44 | + $this->labelField = $field; |
38 | 45 |
|
39 | 46 | return $this; |
40 | 47 | } |
41 | 48 |
|
| 49 | + |
42 | 50 | public function setImageField($field) |
43 | 51 | { |
44 | 52 | $this->imageField = $field; |
45 | 53 |
|
46 | 54 | return $this; |
47 | 55 | } |
48 | 56 |
|
49 | | - public function Field($properties = []) |
| 57 | + |
| 58 | + public function getSourceList() |
50 | 59 | { |
51 | | - $source = $this->sourceObject; |
52 | | - $options = array(); |
53 | | - |
54 | | - if ($source) { |
55 | | - if (is_object($source) && $this->hasEmptyDefault) { |
56 | | - $options[] = ArrayData::create([ |
57 | | - 'Value' => '', |
58 | | - 'Title' => $this->emptyString, |
59 | | - 'Image' => '' |
60 | | - ]); |
61 | | - } |
| 60 | + return $this->sourceList; |
| 61 | + } |
62 | 62 |
|
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 | 63 |
|
72 | | - $image = $item->{$this->imageField}(); |
| 64 | + public function getSourceEmpty() |
| 65 | + { |
| 66 | + $source = []; |
| 67 | + |
| 68 | + foreach ($this->getSourceList() as $k => $item) { |
| 69 | + if ($item instanceof ArrayData) { |
| 70 | + $value = $item->{$this->keyField}; |
| 71 | + |
| 72 | + if (empty($this->labelField)) { |
| 73 | + $title = '--nbsp'; |
73 | 74 | } else { |
74 | | - $value = $k; |
75 | | - $image = null; |
76 | | - $title = $item; |
| 75 | + $title = $item->{$this->labelField}; |
77 | 76 | } |
78 | | - |
79 | | - $selected = false; |
80 | | - if ($value === '' && ($this->value === '' || $this->value === null)) { |
81 | | - $selected = true; |
| 77 | + if (method_exists($item, $this->imageField)) { |
| 78 | + $image = $item->{$this->imageField}(); |
82 | 79 | } else { |
83 | | - // check against value, fallback to a type check comparison when !value |
84 | | - if ($value) { |
85 | | - $selected = ($value == $this->value); |
86 | | - } else { |
87 | | - $selected = ($value === $this->value) || (((string) $value) === ((string) $this->value)); |
88 | | - } |
89 | | - |
90 | | - $this->isSelected = $selected; |
| 80 | + $image = $item->{$this->imageField}; |
91 | 81 | } |
| 82 | + } else { |
| 83 | + $value = $k; |
| 84 | + $image = null; |
| 85 | + $title = $item; |
| 86 | + } |
92 | 87 |
|
93 | | - $disabled = false; |
94 | | - if (in_array($value, $this->disabledItems) && $title != $this->emptyString) { |
95 | | - $disabled = 'disabled'; |
96 | | - } |
| 88 | + $source[$value] = $title . ' (image: ' . $image . ')'; |
| 89 | + } |
97 | 90 |
|
98 | | - $options[] = ArrayData::create([ |
99 | | - 'Title' => $title, |
100 | | - 'Value' => $value, |
101 | | - 'Image' => $image, |
102 | | - 'Selected' => $selected, |
103 | | - 'Disabled' => $disabled, |
104 | | - ]); |
105 | | - } |
| 91 | + return $source; |
| 92 | + } |
| 93 | + |
| 94 | + |
| 95 | + |
| 96 | + /** |
| 97 | + * @param array $properties |
| 98 | + * @return string |
| 99 | + */ |
| 100 | + public function Field($properties = []) |
| 101 | + { |
| 102 | + $options = []; |
| 103 | + |
| 104 | + // Add all options |
| 105 | + foreach ($this->getSourceEmpty() as $value => $title) { |
| 106 | + $options[] = $this->getFieldOption($value, $title); |
106 | 107 | } |
107 | 108 |
|
108 | 109 | $properties = array_merge($properties, [ |
109 | 110 | 'Options' => ArrayList::create($options) |
110 | 111 | ]); |
111 | 112 |
|
112 | | - return FormField::Field($properties); |
| 113 | + return parent::Field($properties); |
113 | 114 | } |
114 | 115 |
|
115 | | - /** |
116 | | - * Get the source of this field as an array |
117 | | - * Transform the source DataList to an key => value array |
118 | | - * |
119 | | - * @return array |
120 | | - */ |
121 | | - public function getSourceAsArray() |
122 | | - { |
123 | | - $source = $this->getSource(); |
124 | | - if (is_array($source)) { |
125 | | - return $source; |
126 | | - } |
127 | 116 |
|
128 | | - return $source->map($this->keyField, $this->labelField)->toArray(); |
| 117 | + public function getSourceValues() |
| 118 | + { |
| 119 | + return array_keys($this->getSourceEmpty()); |
129 | 120 | } |
130 | 121 | } |
0 commit comments