1
|
|
|
<?php |
2
|
|
|
namespace Gurucomkz\EagerLoading; |
3
|
|
|
|
4
|
|
|
use SilverStripe\Core\Config\Config; |
5
|
|
|
use SilverStripe\Forms\GridField\GridField; |
6
|
|
|
use SilverStripe\Forms\GridField\GridField_DataManipulator; |
7
|
|
|
use SilverStripe\ORM\DataList; |
8
|
|
|
use SilverStripe\ORM\SS_List; |
9
|
|
|
|
10
|
|
|
/** |
11
|
|
|
* Adds support for $eager_loading config on the gridfields. |
12
|
|
|
* Usage: |
13
|
|
|
* class MyModelAdmin extends ModelAdmin { |
14
|
|
|
* protected function getGridFieldConfig(): GridFieldConfig |
15
|
|
|
* { |
16
|
|
|
* $config->addComponent(new GridFieldLazyLoadManipulator()); |
17
|
|
|
* } |
18
|
|
|
* } |
19
|
|
|
*/ |
20
|
|
|
|
21
|
|
|
class GridFieldEagerLoadManipulator implements GridField_DataManipulator |
22
|
|
|
{ |
23
|
|
|
/** |
24
|
|
|
* Manipulate the {@link SS_List} as needed by this grid modifier. |
25
|
|
|
* |
26
|
|
|
* @param GridField $gridField |
27
|
|
|
* @param SS_List $dataList |
28
|
|
|
* @return SS_List |
29
|
|
|
*/ |
30
|
2 |
|
public function getManipulatedData(GridField $gridField, SS_List $dataList) |
31
|
|
|
{ |
32
|
|
|
/** @var DataList|DataFilterEagerLoadingExtension $dataList */ |
33
|
2 |
|
$class = $dataList->dataClass(); |
34
|
2 |
|
$config = Config::forClass($class); |
35
|
2 |
|
$vars = $gridField->getForm()->getController()->getRequest()->requestVars(); |
36
|
2 |
|
$eager = $config->get('eager_loading'); |
37
|
2 |
|
$exportString = _t('SilverStripe\\Forms\\GridField\\GridField.CSVEXPORT', 'Export to CSV'); |
38
|
2 |
|
if (in_array($exportString, $vars)) { |
39
|
1 |
|
$export_fields = $config->get('export_fields'); |
40
|
1 |
|
if ($export_fields) { |
41
|
1 |
|
foreach ($export_fields as $field => $_title) { |
42
|
1 |
|
$parts = explode('.', $field); |
43
|
1 |
|
if (count($parts) < 2) { |
44
|
1 |
|
continue; |
45
|
|
|
} |
46
|
1 |
|
$main = implode('.', array_slice($parts, 0, -1)); |
47
|
1 |
|
if (!in_array($main, $eager)) { |
48
|
1 |
|
$eager[] = $main; |
49
|
|
|
} |
50
|
|
|
} |
51
|
|
|
} |
52
|
|
|
} |
53
|
2 |
|
if ($eager) { |
54
|
2 |
|
return $dataList->with($eager); |
55
|
|
|
} |
56
|
|
|
return $dataList; |
57
|
|
|
} |
58
|
|
|
} |
59
|
|
|
|