-
Notifications
You must be signed in to change notification settings - Fork 27
/
HasOnePickerGridField.php
39 lines (24 loc) · 1.04 KB
/
HasOnePickerGridField.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
<?php
class HasOnePickerGridField extends PickerGridField {
protected $childObject;
public function __construct($childObject, $name, $title = null, $hasOneRelationMethod= null, $linkExistingTitle = 'Select Existing') {
$this->childObject = $childObject;
$modelClass = preg_replace('/ID$/','',$name);
if(!$hasOneRelationMethod)
{
$hasOneRelationMethod = $modelClass;
}
$this->setModelClass($modelClass);
$parent = $childObject->$hasOneRelationMethod();
$dataList = $modelClass::get()->filter(array('ID' => $parent->ID));
// construct a PickerGridField
parent::__construct($name, $title, $dataList);
// remove components non-applicable to has_one relationships
$this->getConfig()->removeComponentsByType('GridFieldPaginator');
$this->getConfig()->removeComponentsByType('GridFieldAddExistingSearchButton');
// add custom has_one handling
$component = new HasOneGridFieldAddExistingSearchButton();
$component->setTitle($linkExistingTitle);
$this->getConfig()->addComponent($component);
}
}