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

[2.x] fix: Call beforeStateDehydrated hook by default to handle image uploads #118

Merged
merged 4 commits into from
Jan 30, 2024
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
.idea
.phpunit.result.cache
.phpunit.cache/test-results
build
composer.lock
coverage
Expand Down
30 changes: 24 additions & 6 deletions src/Pages/Concerns/HasPreviewModal.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ trait HasPreviewModal

protected bool $shouldCallHooksBeforePreview = false;

protected bool $shouldDehydrateBeforePreview = true;

protected function getPreviewModalUrl(): ?string
{
return null;
Expand All @@ -43,6 +45,16 @@ protected function mutatePreviewModalData(array $data): array
return $data;
}

protected function getShouldCallHooksBeforePreview(): bool
{
return $this->shouldCallHooksBeforePreview;
}

protected function getShouldDehydrateBeforePreview(): bool
{
return $this->shouldDehydrateBeforePreview;
}

/** @internal */
public static function renderPreviewModalView(?string $view, array $data): string
{
Expand All @@ -54,19 +66,25 @@ public static function renderPreviewModalView(?string $view, array $data): strin
/** @internal */
protected function preparePreviewModalData(): array
{
$shouldCallHooks = $this->getShouldCallHooksBeforePreview();
$shouldDehydrate = $this->getShouldDehydrateBeforePreview();
$record = null;

if ($this->previewableRecord) {
$record = $this->previewableRecord;
} elseif (method_exists($this, 'mutateFormDataBeforeCreate')) {
$data = $this->mutateFormDataBeforeCreate(
$this->form->getState($this->shouldCallHooksBeforePreview)
);
if (! $shouldCallHooks && $shouldDehydrate) {
$this->form->validate();
$this->form->callBeforeStateDehydrated();
}
$data = $this->mutateFormDataBeforeCreate($this->form->getState($shouldCallHooks));
$record = $this->getModel()::make($data);
} elseif (method_exists($this, 'mutateFormDataBeforeSave')) {
$data = $this->mutateFormDataBeforeSave(
$this->form->getState($this->shouldCallHooksBeforePreview)
);
if (! $shouldCallHooks && $shouldDehydrate) {
$this->form->validate();
$this->form->callBeforeStateDehydrated();
}
$data = $this->mutateFormDataBeforeSave($this->form->getState($shouldCallHooks));
$record = $this->getRecord();
$record->fill($data);
} elseif (method_exists($this, 'getRecord')) {
Expand Down
Loading