From 17ae52ea5d1ffbafbc0e10b519622d2e9d9705e4 Mon Sep 17 00:00:00 2001 From: Sabina Talipova Date: Tue, 31 Oct 2023 14:11:06 +1300 Subject: [PATCH] FIX ModelAdmin toast elements --- lang/en.yml | 2 ++ .../GridFieldDetailForm_ItemRequest.php | 20 ++++++++++++++++++- 2 files changed, 21 insertions(+), 1 deletion(-) diff --git a/lang/en.yml b/lang/en.yml index 9eecf2ddc94..f0fad8a894a 100644 --- a/lang/en.yml +++ b/lang/en.yml @@ -111,6 +111,8 @@ en: NEXT: 'Go to next record' PREVIOUS: 'Go to previous record' ViewPermissionsFailure: 'It seems you don''t have the necessary permissions to view "{ObjectTitle}"' + SAVETOASTMESSAGE: 'Saved {name} {title}' + SAVEDUP: 'Saved' SilverStripe\Forms\GridField\GridFieldEditButton: EDIT: Edit SilverStripe\Forms\GridField\GridFieldFilterHeader: diff --git a/src/Forms/GridField/GridFieldDetailForm_ItemRequest.php b/src/Forms/GridField/GridFieldDetailForm_ItemRequest.php index fef944f0c7d..88acf95f591 100644 --- a/src/Forms/GridField/GridFieldDetailForm_ItemRequest.php +++ b/src/Forms/GridField/GridFieldDetailForm_ItemRequest.php @@ -8,6 +8,7 @@ use SilverStripe\Control\HTTPRequest; use SilverStripe\Control\HTTPResponse; use SilverStripe\Control\RequestHandler; +use SilverStripe\Core\Convert; use SilverStripe\Forms\CompositeField; use SilverStripe\Forms\FieldList; use SilverStripe\Forms\Form; @@ -543,6 +544,18 @@ public function doSave($data, $form) $form->sessionMessage($message, 'good', ValidationResult::CAST_HTML); + $message = _t( + __CLASS__ . '.SAVETOASTMESSAGE', + 'Saved {name} {title}', + [ + 'name' => $this->record->i18n_singular_name(), + 'title' => Convert::raw2xml($this->record->Title) + ] + ); + + $controller = $this->getToplevelController(); + $controller->getRequest()->addHeader('X-Status', $message); + // Redirect after save return $this->redirectAfterSave($isNewRecord); } @@ -706,7 +719,10 @@ protected function redirectAfterSave($isNewRecord) } elseif ($this->gridField->getList()->byID($this->record->ID)) { // Return new view, as we can't do a "virtual redirect" via the CMS Ajax // to the same URL (it assumes that its content is already current, and doesn't reload) - return $this->edit($controller->getRequest()); + $message = $controller->getRequest()->getHeader('X-Status') ?? rawurlencode(_t(__CLASS__ . '.SAVEDUP', 'Saved') ?? ''); + $response = new HTTPResponse($this->edit($controller->getRequest())); + $response->addHeader('X-Status', $message); + return $response; } else { // We might be able to redirect to open the record in a different view if ($redirectDest = $this->component->getLostRecordRedirection($this->gridField, $controller->getRequest(), $this->record->ID)) { @@ -718,6 +734,7 @@ protected function redirectAfterSave($isNewRecord) $url = $controller->getRequest()->getURL(); $noActionURL = $controller->removeAction($url); $controller->getRequest()->addHeader('X-Pjax', 'Content'); + $controller->getRequest()->addHeader('X-Status', rawurlencode(_t(__CLASS__ . '.SAVEDUP', 'Saved') ?? '')); return $controller->redirect($noActionURL, 302); } } @@ -800,6 +817,7 @@ public function doDelete($data, $form) //when an item is deleted, redirect to the parent controller $controller = $this->getToplevelController(); $controller->getRequest()->addHeader('X-Pjax', 'Content'); // Force a content refresh + $controller->getRequest()->addHeader('X-Status', $message); return $controller->redirect($this->getBackLink(), 302); //redirect back to admin section }