|
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
|
|
|
* @param ArrayList $items |
|
18
|
|
|
* @return void |
|
19
|
|
|
*/ |
|
20
|
|
|
public function updateBreadcrumbs(ArrayList $items) |
|
21
|
|
|
{ |
|
22
|
|
|
/** @var DataObject $record */ |
|
23
|
|
|
$record = $this->getOwner()->record; |
|
24
|
|
|
// Breadcrumb item title |
|
25
|
|
|
$name = _t('SilverStripe\\Forms\\GridField\\GridField.NewRecord', 'New {type}', ['type' => $record->i18n_singular_name()]); |
|
26
|
|
|
|
|
27
|
|
|
// Find item in breadcrumb for data object that is not yet saved that has link |
|
28
|
|
|
// with value of false |
|
29
|
|
|
$find = $items->filterByCallback(function ($item) use ($name) { |
|
30
|
|
|
return !$item->Link && $item->Title === $name; |
|
31
|
|
|
})->first(); |
|
32
|
|
|
|
|
33
|
|
|
// If we found and item, then ensure we have valid link |
|
34
|
|
|
if ($find) { |
|
35
|
|
|
$find->setField('Link', $this->getOwner()->Link()); |
|
36
|
|
|
} |
|
37
|
|
|
} |
|
38
|
|
|
|
|
39
|
|
|
/** |
|
40
|
|
|
* Hook after saving an object |
|
41
|
|
|
* @param mixed $record |
|
42
|
|
|
*/ |
|
43
|
|
|
public function onAfterSave($record) |
|
44
|
|
|
{ |
|
45
|
|
|
// Close saved object and remove the value of the ID |
|
46
|
|
|
$unsavedRecord = clone $record; |
|
47
|
|
|
$unsavedRecord->ID = 0; |
|
48
|
|
|
// Get name of session for unsaved object |
|
49
|
|
|
$unsavedSessionName = GridField::formatSessionName($unsavedRecord); |
|
50
|
|
|
// Current session |
|
51
|
|
|
$session = Controller::curr()->getRequest()->getSession(); |
|
52
|
|
|
|
|
53
|
|
|
// If we have value stored in the session, then clear that value |
|
54
|
|
|
if ($session->get($unsavedSessionName)) { |
|
55
|
|
|
$session->clear($unsavedSessionName); |
|
56
|
|
|
} |
|
57
|
|
|
|
|
58
|
|
|
// Remove unsaved recored |
|
59
|
|
|
unset($unsavedRecord); |
|
60
|
|
|
} |
|
61
|
|
|
} |
|
62
|
|
|
|