Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ENH Return PJAX responses from gridfield edit forms #11206

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
11 changes: 11 additions & 0 deletions src/Control/RequestHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -678,4 +678,15 @@ public function redirectBack(): HTTPResponse
$url = Director::absoluteURL((string) $url);
return $this->redirect($url);
}

/**
* Convert an array of data to JSON and wrap it in an HTML tag as pjax is used and jQuery will parse this
* as an element on the client side in LeftAndMain.js handleAjaxResponse()
* The attribute type="application/json" denotes this is a data block and won't be processed by a browser
* https://html.spec.whatwg.org/#the-script-element
*/
protected function prepareDataForPjax(array $data): string
{
return '<script type="application/json">' . json_encode($data) . '</script>';
}
}
52 changes: 51 additions & 1 deletion src/Forms/GridField/GridFieldDetailForm_ItemRequest.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
use SilverStripe\Control\Controller;
use SilverStripe\Control\HTTPRequest;
use SilverStripe\Control\HTTPResponse;
use SilverStripe\Control\PjaxResponseNegotiator;
use SilverStripe\Control\RequestHandler;
use SilverStripe\Core\Convert;
use SilverStripe\Core\ClassInfo;
Expand Down Expand Up @@ -170,7 +171,7 @@ public function edit($request)
])->renderWith($this->getTemplates());

if ($request->isAjax()) {
return $return;
return $this->getResponseNegotiator($return)->respond($request);
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Doing this here means it applies for the save and publish actions, with a relatively low chance of affecting custom actions people add.

} else {
// If not requested by ajax, we need to render it within the controller context+template
return $controller->customise([
Expand Down Expand Up @@ -298,6 +299,23 @@ public function ItemEditForm()
}

$form->Backlink = $this->getBackLink();

// Ensure the correct validation response is returned for AJAX requests
$form->setValidationResponseCallback(function (ValidationResult $errors) use ($form) {
$request = $this->getRequest();
if (!$request->isAjax()) {
return null;
}
$negotiator = $this->getResponseNegotiator($form->forTemplate());
return $negotiator->respond($request, [
'ValidationResult' => function () use ($errors) {
return $this->prepareDataForPjax([
'isValid' => $errors->isValid(),
'messages' => $errors->getMessages()
]);
}
]);
});
}

$cb = $this->component->getItemEditFormCallback();
Expand Down Expand Up @@ -945,4 +963,36 @@ private function getModelName(): string
}
return ClassInfo::shortName($this->record);
}

/**
* Get Pjax response negotiator so form submission mirrors other form submission in the CMS.
* See LeftAndMain::getResponseNegotiator()
*/
private function getResponseNegotiator(DBHTMLText $renderedForm): PjaxResponseNegotiator
{
return new PjaxResponseNegotiator([
'default' => function () use ($renderedForm) {
return $renderedForm;
},
'Content' => function () use ($renderedForm) {
return $renderedForm;
},
'CurrentForm' => function () use ($renderedForm) {
return $renderedForm;
},
'Breadcrumbs' => function () {
return $this->renderWith([
'type' => 'Includes',
'SilverStripe\\Admin\\CMSBreadcrumbs'
]);
},
'ValidationResult' => function () {
// Assume valid by default, mirroring LeftAndMain's response negotiator
return $this->prepareDataForPjax([
'isValid' => true,
'messages' => '',
]);
}
], $this->getToplevelController()->getResponse());
}
}