1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Moo\HasOneSelector\Extension; |
4
|
|
|
|
5
|
|
|
use Moo\HasOneSelector\Form\GridField; |
6
|
|
|
use SilverStripe\Control\Controller; |
7
|
|
|
use SilverStripe\ORM\ArrayList; |
8
|
|
|
use SilverStripe\ORM\DataExtension; |
9
|
|
|
use SilverStripe\ORM\DataObject; |
10
|
|
|
|
11
|
|
|
class GridFieldItemRequest extends DataExtension |
12
|
|
|
{ |
13
|
|
|
/** |
14
|
|
|
* Hook after update breadcrumbs in grid field to ensure not saved item in breadcrumb have |
15
|
|
|
* correct URL to adding new item. |
16
|
|
|
*/ |
17
|
1 |
|
public function updateBreadcrumbs(ArrayList $items): void |
18
|
|
|
{ |
19
|
|
|
/** @var DataObject $record */ |
20
|
1 |
|
$record = $this->getOwner()->record; |
21
|
|
|
// Breadcrumb item title |
22
|
1 |
|
$name = _t('SilverStripe\\Forms\\GridField\\GridField.NewRecord', 'New {type}', ['type' => $record->i18n_singular_name()]); |
23
|
|
|
|
24
|
|
|
// Find item in breadcrumb for data object that is not yet saved that has link |
25
|
|
|
// with value of false |
26
|
1 |
|
$find = $items->filterByCallback(function ($item) use ($name) { |
27
|
1 |
|
return !$item->Link && $item->Title === $name; |
28
|
1 |
|
})->first(); |
29
|
|
|
|
30
|
|
|
// If we found and item, then ensure we have valid link |
31
|
1 |
|
if ($find) { |
32
|
1 |
|
$find->setField('Link', $this->getOwner()->Link()); |
33
|
|
|
} |
34
|
|
|
} |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* Hook after saving an object. |
38
|
|
|
*/ |
39
|
1 |
|
public function onAfterSave(DataObject $record): void |
40
|
|
|
{ |
41
|
|
|
// Close saved object and remove the value of the ID |
42
|
1 |
|
$unsavedRecord = clone $record; |
43
|
1 |
|
$unsavedRecord->ID = 0; |
44
|
|
|
// Get name of session for unsaved object |
45
|
1 |
|
$unsavedSessionName = GridField::formatSessionName($unsavedRecord); |
46
|
|
|
// Current session |
47
|
1 |
|
$session = Controller::curr()->getRequest()->getSession(); |
48
|
|
|
|
49
|
|
|
// If we have value stored in the session, then clear that value |
50
|
1 |
|
if ($session->get($unsavedSessionName)) { |
51
|
1 |
|
$session->clear($unsavedSessionName); |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
// Remove unsaved recored |
55
|
1 |
|
unset($unsavedRecord); |
56
|
|
|
} |
57
|
|
|
} |
58
|
|
|
|