1 | <?php |
||
7 | class HasOneSelectorField extends GridField |
||
8 | { |
||
9 | /** |
||
10 | * Name of the list data class |
||
11 | * |
||
12 | * @var string |
||
13 | */ |
||
14 | protected $dataClass; |
||
15 | |||
16 | /** |
||
17 | * Instance of data object that contains the has one relation |
||
18 | * |
||
19 | * @var DataObject |
||
20 | */ |
||
21 | protected $owner; |
||
22 | |||
23 | /** |
||
24 | * Text to display when no record selected |
||
25 | * |
||
26 | * @var string |
||
27 | */ |
||
28 | protected $emptyString = 'No item selected'; |
||
29 | |||
30 | /** |
||
31 | * HasOneSelectorField constructor. |
||
32 | * @param string $name |
||
33 | * @param string|null $title |
||
34 | * @param DataObject $owner |
||
35 | * @param string $dataClass |
||
36 | */ |
||
37 | 3 | public function __construct($name, $title = null, DataObject $owner, $dataClass = DataObject::class) |
|
38 | { |
||
39 | // Include styles |
||
40 | 3 | Requirements::css('hasoneselector/client/styles/hasoneselector.css'); |
|
41 | |||
42 | // Initiate grid field configuration based on relation editor |
||
43 | 3 | $config = GridFieldConfig_RelationEditor::create(); |
|
44 | 3 | $config->removeComponentsByType('GridFieldToolbarHeader'); |
|
45 | 3 | $config->removeComponentsByType('GridFieldSortableHeader'); |
|
46 | 3 | $config->removeComponentsByType('GridFieldFilterHeader'); |
|
47 | 3 | $config->removeComponentsByType('GridFieldPageCount'); |
|
48 | 3 | $config->removeComponentsByType('GridFieldPaginator'); |
|
49 | |||
50 | // Set the data class of the list |
||
51 | 3 | $this->setDataClass($dataClass); |
|
52 | // Set the owner data object that contains the has one relation |
||
53 | 3 | $this->setOwner($owner); |
|
54 | |||
55 | // Instance of data list that manages the grid field data |
||
56 | 3 | $dataList = HasOneSelectorDataList::create($this); |
|
57 | |||
58 | // Set empty string based on the data class |
||
59 | 3 | $this->setEmptyString(sprintf('No %s selected', strtolower(singleton($dataClass)->singular_name()))); |
|
60 | |||
61 | 3 | parent::__construct($name . 'ID', $title, $dataList, $config); |
|
62 | 3 | } |
|
63 | |||
64 | /** |
||
65 | * Set empty string when no record selected |
||
66 | * |
||
67 | * @param string $string |
||
68 | * @return $this |
||
69 | */ |
||
70 | 3 | public function setEmptyString($string) |
|
76 | |||
77 | /** |
||
78 | * set the name of the data class for current list |
||
79 | * |
||
80 | * @param string $class |
||
81 | * @return $this |
||
82 | */ |
||
83 | 3 | public function setDataClass($class) |
|
89 | |||
90 | /** |
||
91 | * Get the name of the data class for current list |
||
92 | * |
||
93 | * @return string |
||
94 | */ |
||
95 | 3 | public function getDataClass() |
|
99 | |||
100 | /** |
||
101 | * Get the record of the has one relation for current owner object |
||
102 | * |
||
103 | * @return DataObject|null |
||
104 | * @throws Exception |
||
105 | */ |
||
106 | 3 | public function getRecord() |
|
110 | |||
111 | /** |
||
112 | * Set the record of the has one relation for current owner object |
||
113 | * |
||
114 | * @param DataObject|null $object |
||
115 | * @return void |
||
116 | * @throws ValidationException |
||
117 | */ |
||
118 | 1 | public function setRecord($object) |
|
119 | { |
||
120 | 1 | $owner = $this->getOwner(); |
|
121 | 1 | $recordName = $this->getName(); |
|
122 | |||
123 | 1 | $owner->{$recordName} = is_null($object) ? 0 : $object->ID; |
|
124 | 1 | $owner->write(); |
|
125 | 1 | } |
|
126 | |||
127 | /** |
||
128 | * Set instance of data object that has the has one relation |
||
129 | * |
||
130 | * @param DataObject $owner |
||
131 | * @return $this |
||
132 | */ |
||
133 | 3 | public function setOwner(DataObject $owner) |
|
139 | |||
140 | /** |
||
141 | * Get instance of data object that has the has one relation |
||
142 | * |
||
143 | * @return DataObject |
||
144 | */ |
||
145 | 3 | public function getOwner() |
|
149 | |||
150 | /** |
||
151 | * Get the data source. |
||
152 | * |
||
153 | * @return SS_List |
||
154 | */ |
||
155 | 3 | public function getList() |
|
163 | |||
164 | /** |
||
165 | * Get the data source after applying every {@link GridField_DataManipulator} to it. |
||
166 | * |
||
167 | * @return SS_List |
||
168 | */ |
||
169 | 3 | public function getManipulatedList() |
|
177 | |||
178 | /** |
||
179 | * @param array $content |
||
180 | * @return string |
||
181 | */ |
||
182 | 2 | protected function getOptionalTableBody(array $content) |
|
194 | } |
||
195 |