|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace DNADesign\ElementalVirtual\Forms; |
|
4
|
|
|
|
|
5
|
|
|
use DNADesign\ElementalVirtual\Model\ElementVirtual; |
|
6
|
|
|
use SilverStripe\Core\Convert; |
|
7
|
|
|
use SilverStripe\Forms\GridField\GridField; |
|
8
|
|
|
use SilverStripe\Forms\GridField\GridFieldAddExistingAutocompleter; |
|
9
|
|
|
use SilverStripe\ORM\DataObject; |
|
10
|
|
|
use SilverStripe\ORM\SS_List; |
|
11
|
|
|
|
|
12
|
|
|
/** |
|
13
|
|
|
* This class is is responsible for adding objects to another object's has_many |
|
14
|
|
|
* and many_many relation, as defined by the {@link RelationList} passed to the |
|
15
|
|
|
* {@link GridField} constructor. |
|
16
|
|
|
* |
|
17
|
|
|
* Objects can be searched through an input field (partially matching one or |
|
18
|
|
|
* more fields). |
|
19
|
|
|
* |
|
20
|
|
|
* Selecting from the results will add the object to the relation. |
|
21
|
|
|
* |
|
22
|
|
|
* Often used alongside {@link GridFieldDeleteAction} for detaching existing |
|
23
|
|
|
* records from a relationship. |
|
24
|
|
|
* |
|
25
|
|
|
* For easier setup, have a look at a sample configuration in |
|
26
|
|
|
* {@link GridFieldConfig_RelationEditor}. |
|
27
|
|
|
* |
|
28
|
|
|
* @package forms |
|
29
|
|
|
* @subpackage fields-gridfield |
|
30
|
|
|
*/ |
|
31
|
|
|
class ElementalGridFieldAddExistingAutocompleter extends GridFieldAddExistingAutocompleter |
|
32
|
|
|
{ |
|
33
|
|
|
|
|
34
|
|
|
/** |
|
35
|
|
|
* If an object ID is set, add the object to the list |
|
36
|
|
|
* |
|
37
|
|
|
* @param GridField $gridField |
|
38
|
|
|
* @param SS_List $dataList |
|
39
|
|
|
* @return SS_List |
|
40
|
|
|
*/ |
|
41
|
|
|
public function getManipulatedData(GridField $gridField, SS_List $dataList) |
|
42
|
|
|
{ |
|
43
|
|
|
if (!$gridField->State->GridFieldAddRelation) { |
|
44
|
|
|
return $dataList; |
|
45
|
|
|
} |
|
46
|
|
|
|
|
47
|
|
|
$objectID = Convert::raw2sql($gridField->State->GridFieldAddRelation); |
|
48
|
|
|
|
|
49
|
|
|
if ($objectID) { |
|
50
|
|
|
$object = DataObject::get_by_id($gridField->getModelClass(), $objectID); |
|
|
|
|
|
|
51
|
|
|
|
|
52
|
|
|
if ($object) { |
|
|
|
|
|
|
53
|
|
|
// if the object is currently not linked to either a page or another list then we want to link to |
|
54
|
|
|
// the original, otherwise link to a clone |
|
55
|
|
|
if (!$object->ParentID) { |
|
56
|
|
|
$dataList->add($object); |
|
57
|
|
|
} else { |
|
58
|
|
|
$virtual = ElementVirtual::create(); |
|
59
|
|
|
$virtual->LinkedElementID = $object->ID; |
|
60
|
|
|
$virtual->write(); |
|
61
|
|
|
|
|
62
|
|
|
$dataList->add($virtual); |
|
63
|
|
|
} |
|
64
|
|
|
} |
|
65
|
|
|
} |
|
66
|
|
|
|
|
67
|
|
|
$gridField->State->GridFieldAddRelation = null; |
|
68
|
|
|
|
|
69
|
|
|
return $dataList; |
|
70
|
|
|
} |
|
71
|
|
|
} |
|
72
|
|
|
|