1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Dynamic\Products\Extension; |
4
|
|
|
|
5
|
|
|
use Dynamic\Products\Page\Product; |
6
|
|
|
use SilverStripe\Dev\Debug; |
7
|
|
|
use SilverStripe\Forms\FieldList; |
8
|
|
|
use SilverStripe\Forms\GridField\GridField; |
9
|
|
|
use SilverStripe\Forms\GridField\GridFieldAddExistingAutocompleter; |
10
|
|
|
use SilverStripe\Forms\GridField\GridFieldAddNewButton; |
11
|
|
|
use SilverStripe\Forms\GridField\GridFieldConfig_RelationEditor; |
12
|
|
|
use SilverStripe\ORM\DataExtension; |
13
|
|
|
use SilverStripe\Versioned\GridFieldArchiveAction; |
14
|
|
|
use Symbiote\GridFieldExtensions\GridFieldAddExistingSearchButton; |
15
|
|
|
use Symbiote\GridFieldExtensions\GridFieldOrderableRows; |
16
|
|
|
|
17
|
|
|
class RelatedProductsDataExtension extends DataExtension |
18
|
|
|
{ |
19
|
|
|
/** |
20
|
|
|
* @var array |
21
|
|
|
*/ |
22
|
|
|
private static $many_many = [ |
|
|
|
|
23
|
|
|
'RelatedProducts' => Product::class, |
24
|
|
|
]; |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* @var array |
28
|
|
|
*/ |
29
|
|
|
private static $many_many_extraFields = [ |
|
|
|
|
30
|
|
|
'RelatedProducts' => [ |
31
|
|
|
'SortOrder' => 'Int', |
32
|
|
|
], |
33
|
|
|
]; |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* @param FieldList $fields |
37
|
|
|
*/ |
38
|
|
|
public function updateCMSFields(FieldList $fields) |
39
|
|
|
{ |
40
|
|
|
if ($this->owner->ID) { |
41
|
|
|
$fields->addFieldToTab( |
42
|
|
|
'Root.Related', |
43
|
|
|
GridField::create( |
44
|
|
|
'RelatedProducts', |
45
|
|
|
'Related Products', |
46
|
|
|
$this->owner->RelatedProducts()->sort('SortOrder'), |
47
|
|
|
$productConfig = GridFieldConfig_RelationEditor::create() |
48
|
|
|
) |
49
|
|
|
); |
50
|
|
|
$productConfig |
51
|
|
|
->removeComponentsByType([ |
52
|
|
|
GridFieldAddNewButton::class, |
53
|
|
|
GridFieldAddExistingAutocompleter::class, |
54
|
|
|
GridFieldArchiveAction::class |
55
|
|
|
]) |
56
|
|
|
->addComponents( |
57
|
|
|
new GridFieldOrderableRows('SortOrder'), |
58
|
|
|
$addnew = new GridFieldAddExistingSearchButton() |
59
|
|
|
); |
60
|
|
|
|
61
|
|
|
$addnew->setSearchList(Product::get()->exclude('ID', $this->owner->ID)); |
62
|
|
|
} |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
/** |
66
|
|
|
* @return mixed |
67
|
|
|
*/ |
68
|
|
|
public function getRelatedProductsList() |
69
|
|
|
{ |
70
|
|
|
return $this->owner->RelatedProducts()->sort('SortOrder'); |
71
|
|
|
} |
72
|
|
|
} |
73
|
|
|
|