Skip to content

Commit a71715d

Browse files
authored
Merge pull request #4399 from Laravel-Backpack/fix-language
set locale on entry on operations that need them
2 parents ebe01f6 + a44f65e commit a71715d

File tree

3 files changed

+28
-4
lines changed

3 files changed

+28
-4
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: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -65,13 +65,14 @@ public function edit($id)
6565
$this->crud->hasAccessOrFail('update');
6666
// get entry ID from Request (makes sure its the last ID for nested resources)
6767
$id = $this->crud->getCurrentEntryId() ?? $id;
68-
$this->crud->setOperationSetting('fields', $this->crud->getUpdateFields());
6968
// get the info for that entry
70-
$this->data['entry'] = $this->crud->getEntry($id);
69+
70+
$this->data['entry'] = $this->crud->getEntryWithLocale($id);
71+
$this->crud->setOperationSetting('fields', $this->crud->getUpdateFields());
72+
7173
$this->data['crud'] = $this->crud;
7274
$this->data['saveAction'] = $this->crud->getSaveAction();
7375
$this->data['title'] = $this->crud->getTitle() ?? trans('backpack::crud.edit').' '.$this->crud->entity_name;
74-
7576
$this->data['id'] = $id;
7677

7778
// load the view from /resources/views/vendor/backpack/crud/ if it exists, otherwise load the one in the package

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

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,29 @@ 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+
87+
return $this->entry;
88+
}
89+
6790
/**
6891
* Return a Model builder instance with the current crud query applied.
6992
*

0 commit comments

Comments
 (0)