Skip to content

Commit ed9c401

Browse files
authored
Refactor DropdownImageField for improved clarity
1 parent 1624b3c commit ed9c401

File tree

1 file changed

+68
-77
lines changed

1 file changed

+68
-77
lines changed

src/DropdownImageField.php

Lines changed: 68 additions & 77 deletions
Original file line numberDiff line numberDiff line change
@@ -3,128 +3,119 @@
33
namespace FullscreenInteractive\DropdownImageField;
44

55
use SilverStripe\Forms\DropdownField;
6-
use SilverStripe\ORM\SS_List;
7-
use SilverStripe\Forms\FormField;
86
use SilverStripe\Model\ArrayData;
97
use SilverStripe\Model\List\ArrayList;
108

119
class DropdownImageField extends DropdownField
1210
{
11+
protected string $keyField = 'ID';
1312

14-
protected $keyField = 'ID';
13+
protected string $labelField = 'Title';
1514

16-
protected $labelField = 'Title';
15+
protected string $imageField = 'Image';
1716

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+
}
1925

20-
protected $sourceObject;
2126

22-
public function __construct($name, $title, $sourceObject, $keyField = 'ID', $labelField = 'Title', $imageField = 'Image', $value = '', $form = null)
27+
public function setSourceList(ArrayList $sourceList)
2328
{
29+
$this->sourceList = $sourceList;
2430

25-
$this->keyField = $keyField;
26-
$this->labelField = $labelField;
27-
$this->imageField = $imageField;
31+
return $this;
32+
}
2833

29-
parent::__construct($name, ($title === null) ? $name : $title, $sourceObject, $value, $form);
3034

31-
$this->addExtraClass('dropdown');
32-
$this->sourceObject = $sourceObject;
35+
public function setKeyField(string $field)
36+
{
37+
$this->keyField = $field;
38+
39+
return $this;
3340
}
3441

35-
public function setSourceObject($source)
42+
public function setLabelField(string $field)
3643
{
37-
$this->sourceObject = $source;
44+
$this->labelField = $field;
3845

3946
return $this;
4047
}
4148

49+
4250
public function setImageField($field)
4351
{
4452
$this->imageField = $field;
4553

4654
return $this;
4755
}
4856

49-
public function Field($properties = [])
57+
58+
public function getSourceList()
5059
{
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+
}
6262

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-
}
7163

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';
7374
} else {
74-
$value = $k;
75-
$image = null;
76-
$title = $item;
75+
$title = $item->{$this->labelField};
7776
}
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}();
8279
} 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};
9181
}
82+
} else {
83+
$value = $k;
84+
$image = null;
85+
$title = $item;
86+
}
9287

93-
$disabled = false;
94-
if (in_array($value, $this->disabledItems) && $title != $this->emptyString) {
95-
$disabled = 'disabled';
96-
}
88+
$source[$value] = $title . ' (image: ' . $image . ')';
89+
}
9790

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);
106107
}
107108

108109
$properties = array_merge($properties, [
109110
'Options' => ArrayList::create($options)
110111
]);
111112

112-
return FormField::Field($properties);
113+
return parent::Field($properties);
113114
}
114115

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-
}
127116

128-
return $source->map($this->keyField, $this->labelField)->toArray();
117+
public function getSourceValues()
118+
{
119+
return array_keys($this->getSourceEmpty());
129120
}
130121
}

0 commit comments

Comments
 (0)