Skip to content

Commit f703e6c

Browse files
committed
Store original source object, SelectField will convert to an array
1 parent 96b2e5f commit f703e6c

File tree

2 files changed

+38
-11
lines changed

2 files changed

+38
-11
lines changed

javascript/ImageSelect.jquery.js

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,6 @@
3232

3333
// Attach Ready event before continue with chose
3434
this.each(function(input_field) {
35-
3635
$this = $(this);
3736

3837
$this.on("chosen:ready", function change(e, chosen){
@@ -52,7 +51,6 @@
5251

5352
var options = form_field.options;
5453
var spans = $(chosen.container).find('.chosen-choices span');
55-
5654
if(options && options.length){
5755

5856
for(var i = 0 ; i < options.length; i++){

src/DropdownImageField.php

Lines changed: 38 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,18 @@
66
use SilverStripe\View\Requirements;
77
use SilverStripe\View\ArrayData;
88
use SilverStripe\ORM\ArrayList;
9+
use SilverStripe\ORM\SS_List;
910
use SilverStripe\Forms\FormField;
1011

1112
class DropdownImageField extends DropdownField {
1213

13-
protected $keyField, $labelField, $imageField;
14+
protected $keyField = 'ID';
15+
16+
protected $labelField = 'Title';
17+
18+
protected $imageField = 'Image';
19+
20+
protected $sourceObject;
1421

1522
public function __construct($name, $title, $sourceObject, $keyField = 'ID', $labelField = 'Title', $imageField = 'Image', $value = '', $form = null) {
1623

@@ -21,13 +28,29 @@ public function __construct($name, $title, $sourceObject, $keyField = 'ID', $lab
2128
parent::__construct($name, ($title === null) ? $name : $title, $sourceObject, $value, $form);
2229

2330
$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;
2446
}
2547

2648
public function Field($properties = array()) {
2749

2850
$dirName = basename(dirname(dirname(__FILE__)));
29-
$source = $this->getSource();
51+
$source = $this->sourceObject;
3052
$options = array();
53+
3154
if ($source) {
3255
if (is_object($source) && $this->hasEmptyDefault) {
3356
$options[] = new ArrayData([
@@ -37,16 +60,22 @@ public function Field($properties = array()) {
3760
]);
3861
}
3962

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}();
4473
} else {
45-
$title = $item->{$this->labelField};
74+
$value = $k;
75+
$image = null;
76+
$title = $item;
4677
}
4778

48-
$image = $item->{$this->imageField}();
49-
5079
$selected = false;
5180
if ($value === '' && ($this->value === '' || $this->value === null)) {
5281
$selected = true;

0 commit comments

Comments
 (0)