diff --git a/src/ActionsGridFieldItemRequest.php b/src/ActionsGridFieldItemRequest.php index 3c073b6..62c9599 100644 --- a/src/ActionsGridFieldItemRequest.php +++ b/src/ActionsGridFieldItemRequest.php @@ -700,10 +700,7 @@ protected function forwardActionToRecord($action, $data = [], $form = null) //@phpstan-ignore-next-line if (method_exists($clickedAction, 'getShouldRefresh') && $clickedAction->getShouldRefresh()) { - $controller->getResponse()->addHeader('X-Reload', "true"); - // Requires a ControllerURL as well, see https://github.com/silverstripe/silverstripe-admin/blob/a3aa41cea4c4df82050eef65ad5efcfae7bfde69/client/src/legacy/LeftAndMain.js#L773-L780 - $url = $controller->getReferer(); - $controller->getResponse()->addHeader('X-ControllerURL', $url); + self::addXReload($controller); } // 4xx status makes a red box if ($error) { @@ -722,7 +719,8 @@ protected function forwardActionToRecord($action, $data = [], $form = null) // Custom redirect //@phpstan-ignore-next-line if (method_exists($clickedAction, 'getRedirectURL') && $clickedAction->getRedirectURL()) { - $controller->getResponse()->addHeader('X-Reload', "true"); // we probably need a full ui refresh + // we probably need a full ui refresh + self::addXReload($clickedAction->getRedirectURL()); //@phpstan-ignore-next-line return $controller->redirect($clickedAction->getRedirectURL()); } @@ -731,6 +729,23 @@ protected function forwardActionToRecord($action, $data = [], $form = null) return $this->redirectAfterAction($isNewRecord, $record); } + /** + * Requires a ControllerURL as well, see + * https://github.com/silverstripe/silverstripe-admin/blob/a3aa41cea4c4df82050eef65ad5efcfae7bfde69/client/src/legacy/LeftAndMain.js#L773-L780 + * + * @param Controller $controller + * @param string|null $url + * @return void + */ + public static function addXReload(Controller $controller, ?string $url = null): void + { + if (!$url) { + $url = $controller->getReferer(); + } + $controller->getResponse()->addHeader('X-ControllerURL', $url); + $controller->getResponse()->addHeader('X-Reload', "true"); + } + /** * Handles custom links * diff --git a/src/GridFieldSaveAllButton.php b/src/GridFieldSaveAllButton.php index f1db12a..9b6a6ae 100644 --- a/src/GridFieldSaveAllButton.php +++ b/src/GridFieldSaveAllButton.php @@ -21,6 +21,9 @@ class GridFieldSaveAllButton extends GridFieldTableButton */ protected $noAjax = false; protected ?string $completeMessage = null; + protected ?bool $useHandleSave = true; + protected $allowEmptyResponse = true; + protected bool $shouldReload = false; public function __construct($targetFragment = 'buttons-before-left', $buttonLabel = null) { @@ -44,33 +47,44 @@ public function handle(GridField $gridField, Controller $controller, $arguments if (!$record) { continue; } - $component = $gridField->getConfig()->getComponentByType(\Symbiote\GridFieldExtensions\GridFieldEditableColumns::class); - $component->handleSave($gridField, $record); - // foreach ($values as $k => $v) { - // $record->$k = $v; - // } - // $record->write(); + // You can use the grid field component or a simple loop with write + if ($this->useHandleSave) { + /** @var \Symbiote\GridFieldExtensions\GridFieldEditableColumns $component */ + $component = $gridField->getConfig()->getComponentByType(\Symbiote\GridFieldExtensions\GridFieldEditableColumns::class); + $component->handleSave($gridField, $record); + } else { + foreach ($values as $k => $v) { + $record->$k = $v; + } + $record->write(); + } } $newData = $data[$fieldName]['GridFieldAddNewInlineButton'] ?? []; foreach ($newData as $idx => $values) { - $record = new $model; - foreach ($values as $k => $v) { - $record->$k = $v; + if ($this->useHandleSave) { + /** @var \Symbiote\GridFieldExtensions\GridFieldAddNewInlineButton $component */ + $component = $gridField->getConfig()->getComponentByType(\Symbiote\GridFieldExtensions\GridFieldAddNewInlineButton::class); + $component->handleSave($gridField, $record); + } else { + $record = new $model; + foreach ($values as $k => $v) { + $record->$k = $v; + } + $record->write(); } - $record->write(); } $response = $controller->getResponse(); if (Director::is_ajax()) { if (!$this->completeMessage) { - $this->completeMessage = _t('GridFieldSaveAllButton.DONE', 'ALL SAVED!'); + $this->completeMessage = _t('GridFieldSaveAllButton.DONE', 'All saved'); + } + if ($this->shouldReload) { + ActionsGridFieldItemRequest::addXReload($controller); } - // Reload for now since we mess up with the PJAX fragment - $url = $controller->getReferer(); - $response->addHeader('X-ControllerURL', $url); - $response->addHeader('X-Reload', true); $response->addHeader('X-Status', rawurlencode($this->completeMessage)); + return null; } else { return $controller->redirectBack(); } @@ -94,4 +108,42 @@ public function setCompleteMessage($completeMessage): self $this->completeMessage = $completeMessage; return $this; } + + /** + * Get the value of useHandleSave + */ + public function getUseHandleSave(): bool + { + return $this->useHandleSave; + } + + /** + * Set the value of useHandleSave + * + * @param bool $useHandleSave + */ + public function setUseHandleSave($useHandleSave): self + { + $this->useHandleSave = $useHandleSave; + return $this; + } + + /** + * Get the value of shouldReload + */ + public function getShouldReload(): bool + { + return $this->shouldReload; + } + + /** + * Set the value of shouldReload + * + * @param bool $shouldReload + */ + public function setShouldReload($shouldReload): self + { + $this->shouldReload = $shouldReload; + return $this; + } } diff --git a/src/GridFieldTableButton.php b/src/GridFieldTableButton.php index bd4458c..a038662 100644 --- a/src/GridFieldTableButton.php +++ b/src/GridFieldTableButton.php @@ -231,6 +231,7 @@ public function handleAction(GridField $gridField, $actionName, $arguments, $dat return $result; } + // This can be helpful if you want to refresh the whole form for PJAX requests if ($this->allowEmptyResponse) { return; }