Skip to content

Commit 8f2ac32

Browse files
committed
feat: enhance translation route handling for create action
1 parent ccfbc55 commit 8f2ac32

3 files changed

Lines changed: 15 additions & 2 deletions

File tree

composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@
5353
},
5454
"extra": {
5555
"branch-alias": {
56-
"dev-main": "1.0-dev"
56+
"dev-main": "2.0-dev"
5757
},
5858
"laravel": {
5959
"providers": [

resources/views/table-cells.blade.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
</a>
99
@else
1010
<a class="actions__item text-decoration-none {{ config('translatable.styles.icons.add') }}"
11-
href="{{ $getUrl('edit', $language->code) }}"
11+
href="{{ $getUrl($isModelDbTranslatable() ? 'create' : 'edit', $language->code) }}"
1212
title="{{ _d('Add Translation') }}">
1313
</a>
1414
@endif

src/Views/Components/TableCells.php

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
use Illuminate\Contracts\View\View;
66
use Illuminate\Support\Collection;
77
use Illuminate\View\Component;
8+
use Javaabu\Translatable\Contracts\DbTranslatable;
89
use Javaabu\Translatable\Contracts\Translatable;
910
use Javaabu\Translatable\Facades\Languages;
1011

@@ -25,17 +26,29 @@ public function getUrl(string $action, string $locale): string
2526
{
2627
$params = filled($this->route_params) ? $this->route_params : $this->model->getRouteParams();
2728

29+
// A big assumption here is that the last parameter in the route params is the model ID.
30+
// If the action is 'create', we don't need the last parameter (usually the ID)
2831
if ($action === 'create') {
2932
$params = array_slice($params, 0, -1); // Remove the last parameter if it exists
3033
}
3134

35+
// If the model is we need to add "lang_parent" to the params
36+
if ($this->isModelDbTranslatable()) {
37+
$params['lang_parent'] = $this->model->id;
38+
}
39+
3240
return translate_route(
3341
filled($this->route_name) ? $this->route_name : $this->model->getRouteName() . '.' . $action,
3442
$params,
3543
locale: $locale,
3644
);
3745
}
3846

47+
public function isModelDbTranslatable(): bool
48+
{
49+
return $this->model instanceof DbTranslatable;
50+
}
51+
3952
public function render(): View
4053
{
4154
return view('translatable::table-cells');

0 commit comments

Comments
 (0)