|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace SilverStripe\Forms; |
|
4
|
|
|
|
|
5
|
|
|
use SilverStripe\Core\Convert; |
|
6
|
|
|
use SilverStripe\ORM\DataObjectInterface; |
|
7
|
|
|
use SilverStripe\ORM\Map; |
|
8
|
|
|
use SilverStripe\ORM\FieldType\DBField; |
|
9
|
|
|
|
|
10
|
|
|
/** |
|
11
|
|
|
* Read-only complement of {@link DropdownField}. |
|
12
|
|
|
* |
|
13
|
|
|
* Shows the "human value" of the dropdown field for the currently selected |
|
14
|
|
|
* value. |
|
15
|
|
|
*/ |
|
16
|
|
|
class SingleLookupField extends SingleSelectField |
|
17
|
|
|
{ |
|
18
|
|
|
/** |
|
19
|
|
|
* @var bool |
|
20
|
|
|
*/ |
|
21
|
|
|
protected $readonly = true; |
|
22
|
|
|
|
|
23
|
|
|
/** |
|
24
|
|
|
* @return mixed|null |
|
25
|
|
|
*/ |
|
26
|
|
|
protected function valueToLabel() |
|
27
|
|
|
{ |
|
28
|
|
|
$value = $this->value; |
|
29
|
|
|
$source = $this->getSource(); |
|
30
|
|
|
$source = ($source instanceof Map) ? $source->toArray() : $source; |
|
31
|
|
|
|
|
32
|
|
|
if (array_key_exists($value, $source)) { |
|
|
|
|
|
|
33
|
|
|
return $source[$value]; |
|
34
|
|
|
} |
|
35
|
|
|
|
|
36
|
|
|
return null; |
|
37
|
|
|
} |
|
38
|
|
|
|
|
39
|
|
|
/** |
|
40
|
|
|
* Ignore validation as the field is readonly |
|
41
|
|
|
* |
|
42
|
|
|
* @param Validator $validator |
|
43
|
|
|
* @return bool |
|
44
|
|
|
*/ |
|
45
|
|
|
public function validate($validator) |
|
46
|
|
|
{ |
|
47
|
|
|
return true; |
|
48
|
|
|
} |
|
49
|
|
|
|
|
50
|
|
|
/** |
|
51
|
|
|
* Stubbed so invalid data doesn't save into the DB |
|
52
|
|
|
* |
|
53
|
|
|
* @param DataObjectInterface $record DataObject to save data into |
|
54
|
|
|
*/ |
|
55
|
|
|
public function saveInto(DataObjectInterface $record) |
|
56
|
|
|
{ |
|
57
|
|
|
} |
|
58
|
|
|
|
|
59
|
|
|
/** |
|
60
|
|
|
* @return SingleLookupField |
|
61
|
|
|
*/ |
|
62
|
|
|
public function performReadonlyTransformation() |
|
63
|
|
|
{ |
|
64
|
|
|
$clone = clone $this; |
|
65
|
|
|
|
|
66
|
|
|
return $clone; |
|
67
|
|
|
} |
|
68
|
|
|
|
|
69
|
|
|
/** |
|
70
|
|
|
* @return bool |
|
71
|
|
|
*/ |
|
72
|
|
|
public function getHasEmptyDefault() |
|
73
|
|
|
{ |
|
74
|
|
|
return false; |
|
75
|
|
|
} |
|
76
|
|
|
|
|
77
|
|
|
/** |
|
78
|
|
|
* @return string |
|
79
|
|
|
*/ |
|
80
|
|
|
public function Type() |
|
81
|
|
|
{ |
|
82
|
|
|
return 'single-lookup readonly'; |
|
83
|
|
|
} |
|
84
|
|
|
|
|
85
|
|
|
/** |
|
86
|
|
|
* Note: we need to transform value in here becaue React fields do not use Field() to display value |
|
87
|
|
|
* |
|
88
|
|
|
* @return mixed |
|
89
|
|
|
*/ |
|
90
|
|
|
public function Value() |
|
91
|
|
|
{ |
|
92
|
|
|
$label = $this->valueToLabel(); |
|
93
|
|
|
if (!is_null($label)) { |
|
94
|
|
|
return $label; |
|
95
|
|
|
} |
|
96
|
|
|
|
|
97
|
|
|
return $value; |
|
|
|
|
|
|
98
|
|
|
} |
|
99
|
|
|
|
|
100
|
|
|
/** |
|
101
|
|
|
* @return string |
|
102
|
|
|
*/ |
|
103
|
|
|
public function getTemplate() |
|
104
|
|
|
{ |
|
105
|
|
|
// this field uses the same default template as LookupField |
|
106
|
|
|
return parent::getTemplate() ?: LookupField::class; |
|
107
|
|
|
} |
|
108
|
|
|
|
|
109
|
|
|
/** |
|
110
|
|
|
* Returns a readonly span containing the correct value. |
|
111
|
|
|
* |
|
112
|
|
|
* @param array $properties |
|
113
|
|
|
* |
|
114
|
|
|
* @return string |
|
115
|
|
|
*/ |
|
116
|
|
|
public function Field($properties = []) |
|
117
|
|
|
{ |
|
118
|
|
|
$label = $this->valueToLabel(); |
|
119
|
|
|
if (!is_null($label)) { |
|
120
|
|
|
$attrValue = Convert::raw2xml($label); |
|
121
|
|
|
$inputValue = $this->value; |
|
122
|
|
|
} else { |
|
123
|
|
|
$attrValue = '<i>(' . _t('SilverStripe\\Forms\\FormField.NONE', 'none') . ')</i>'; |
|
124
|
|
|
$inputValue = ''; |
|
125
|
|
|
} |
|
126
|
|
|
|
|
127
|
|
|
$properties = array_merge($properties, array( |
|
128
|
|
|
'AttrValue' => DBField::create_field('HTMLFragment', $attrValue), |
|
129
|
|
|
'InputValue' => $inputValue |
|
130
|
|
|
)); |
|
131
|
|
|
|
|
132
|
|
|
return parent::Field($properties); |
|
133
|
|
|
} |
|
134
|
|
|
} |
|
135
|
|
|
|