Skip to content

Commit 082ab44

Browse files
authored
Merge pull request #5236 from Laravel-Backpack/fix-translatable-uploads
Fix translatable uploads
2 parents 496b81c + 2a94a86 commit 082ab44

File tree

2 files changed

+23
-2
lines changed

2 files changed

+23
-2
lines changed

src/app/Library/Uploaders/Support/RegisterUploadEvents.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -121,6 +121,12 @@ private function setupModelEvents(string $model, UploaderInterface $uploader): v
121121
app('crud')->entry = $uploader->retrieveUploadedFiles(app('crud')->entry);
122122
} else {
123123
$model::retrieved(function ($entry) use ($uploader) {
124+
if ($entry->translationEnabled()) {
125+
$locale = request('_locale', \App::getLocale());
126+
if (in_array($locale, array_keys($entry->getAvailableLocales()))) {
127+
$entry->setLocale($locale);
128+
}
129+
}
124130
$entry = $uploader->retrieveUploadedFiles($entry);
125131
});
126132
}

src/app/Library/Uploaders/Uploader.php

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -160,10 +160,10 @@ public function canHandleMultipleFiles(): bool
160160
public function getPreviousFiles(Model $entry): mixed
161161
{
162162
if (! $this->attachedToFakeField) {
163-
return $entry->getOriginal($this->getName());
163+
return $this->getOriginalValue($entry);
164164
}
165165

166-
$value = $entry->getOriginal($this->attachedToFakeField);
166+
$value = $this->getOriginalValue($entry, $this->attachedToFakeField);
167167
$value = is_string($value) ? json_decode($value, true) : (array) $value;
168168

169169
return $value[$this->getName()] ?? null;
@@ -260,4 +260,19 @@ private function getPathFromConfiguration(array $crudObject, array $configuratio
260260

261261
return empty($this->path) ? $this->path : Str::of($this->path)->finish('/')->value();
262262
}
263+
264+
private function getOriginalValue(Model $entry, $field = null)
265+
{
266+
$previousValue = $entry->getOriginal($field ?? $this->getName());
267+
268+
if (! $previousValue) {
269+
return $previousValue;
270+
}
271+
272+
if ($entry->translationEnabled()) {
273+
return $previousValue[$entry->getLocale()] ?? null;
274+
}
275+
276+
return $previousValue;
277+
}
263278
}

0 commit comments

Comments
 (0)