Skip to content

Commit 552c60e

Browse files
committed
set locale on entry on operations that need them
1 parent ebe01f6 commit 552c60e

File tree

3 files changed

+24
-2
lines changed

3 files changed

+24
-2
lines changed

src/app/Http/Controllers/Operations/ShowOperation.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ public function show($id)
7979
if ($this->crud->get('show.softDeletes') && in_array('Illuminate\Database\Eloquent\SoftDeletes', class_uses($this->crud->model))) {
8080
$this->data['entry'] = $this->crud->getModel()->withTrashed()->findOrFail($id);
8181
} else {
82-
$this->data['entry'] = $this->crud->getEntry($id);
82+
$this->data['entry'] = $this->crud->getEntryWithLocale($id);
8383
}
8484

8585
$this->data['crud'] = $this->crud;

src/app/Http/Controllers/Operations/UpdateOperation.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ public function edit($id)
6767
$id = $this->crud->getCurrentEntryId() ?? $id;
6868
$this->crud->setOperationSetting('fields', $this->crud->getUpdateFields());
6969
// get the info for that entry
70-
$this->data['entry'] = $this->crud->getEntry($id);
70+
$this->data['entry'] = $this->crud->getEntryWithLocale($id);
7171
$this->data['crud'] = $this->crud;
7272
$this->data['saveAction'] = $this->crud->getSaveAction();
7373
$this->data['title'] = $this->crud->getTitle() ?? trans('backpack::crud.edit').' '.$this->crud->entity_name;

src/app/Library/CrudPanel/Traits/Read.php

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,28 @@ public function getEntry($id)
6464
return $this->entry;
6565
}
6666

67+
/**
68+
* Find and retrieve an entry in the database or fail.
69+
* When found, make sure we set the Locale on it.
70+
*
71+
* @param int The id of the row in the db to fetch.
72+
* @return \Illuminate\Database\Eloquent\Model The row in the db.
73+
*/
74+
public function getEntryWithLocale($id)
75+
{
76+
if (! $this->entry) {
77+
$this->entry = $this->getEntry($id);
78+
}
79+
80+
if ($this->entry->translationEnabled()) {
81+
$locale = request('_locale', \App::getLocale());
82+
if (in_array($locale, array_keys($this->entry->getAvailableLocales()))) {
83+
$this->entry->setLocale($locale);
84+
}
85+
}
86+
return $this->entry;
87+
}
88+
6789
/**
6890
* Return a Model builder instance with the current crud query applied.
6991
*

0 commit comments

Comments
 (0)