Skip to content

Commit 4471c11

Browse files
wip make it taggabble
1 parent 1f03fea commit 4471c11

File tree

1 file changed

+32
-6
lines changed

1 file changed

+32
-6
lines changed

packages/core/src/Traits/Taxonomy/HasResourceTaxonomy.php

Lines changed: 32 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -50,14 +50,23 @@ protected static function createTaxonomyField(string $taxonomy, array $settings,
5050
'createOptionForm' => $createFormClass::getSchema(),
5151
'createOptionUsing' => function (array $data) use ($modelClass) {
5252
$validator = validator($data, [
53-
'slug' => ['required', 'string', 'max:255', 'unique:'.app($modelClass)->getTable().',slug'],
53+
'title' => ['required', 'string', 'max:255'],
54+
'slug' => ['required', 'string', 'max:255'],
5455
]);
5556

5657
if ($validator->fails()) {
57-
return $validator->errors()->first('slug');
58+
return $validator->errors()->first();
5859
}
5960

60-
$newTaxonomy = app($modelClass)::create($data);
61+
$locale = app()->getLocale();
62+
$translations = [
63+
$locale => [
64+
'title' => $data['title'],
65+
'slug' => $data['slug'],
66+
]
67+
];
68+
69+
$newTaxonomy = app($modelClass)::createWithTranslations([], $translations);
6170

6271
return $newTaxonomy->id;
6372
},
@@ -79,11 +88,28 @@ protected static function createTaxonomyField(string $taxonomy, array $settings,
7988

8089
return Select::make($taxonomy)
8190
->multiple()
82-
->options(fn () => app($modelClass)::pluck('title', 'id')->toArray())
91+
->options(fn () => app($modelClass)::with('translations')
92+
->get()
93+
->mapWithKeys(function ($item) {
94+
$locale = request()->query('lang', app()->getLocale());
95+
$translation = $item->translate($locale) ?? $item->translate(app()->getLocale());
96+
return [$item->id => $translation ? $translation->title : 'ID: ' . $item->id];
97+
})
98+
->toArray()
99+
)
83100
->getSearchResultsUsing(
84-
fn (string $search) => app($modelClass)::where('title', 'like', sprintf('%%%s%%', $search))
101+
fn (string $search) => app($modelClass)::with('translations')
102+
->whereHas('translations', function ($query) use ($search) {
103+
$query->where('title', 'like', sprintf('%%%s%%', $search))
104+
->where('locale', app()->getLocale());
105+
})
85106
->limit(50)
86-
->pluck('title', 'id')
107+
->get()
108+
->mapWithKeys(function ($item) {
109+
$locale = app()->getLocale();
110+
$translation = $item->translate($locale);
111+
return [$item->id => $translation ? $translation->title : 'ID: ' . $item->id];
112+
})
87113
->toArray()
88114
)
89115
->createOptionForm($commonConfig['createOptionForm'])

0 commit comments

Comments
 (0)