Skip to content

Commit 4c44849

Browse files
authored
Add Filament support (#26)
1 parent eadaa83 commit 4c44849

File tree

2 files changed

+57
-6
lines changed

2 files changed

+57
-6
lines changed

README.md

+22-1
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ But then you want to make sure each translation is unique for its language.
3535

3636
That's where this package comes in to play.
3737

38-
This package also supports [`spatie/nova-translatable`](https://github.com/spatie/nova-translatable/) in case you are using [Laravel Nova](https://nova.laravel.com/).
38+
This package also supports [`spatie/nova-translatable`](https://github.com/spatie/nova-translatable/) in case you are using [Laravel Nova](https://nova.laravel.com/) and [`filamentphp/spatie-laravel-translatable-plugin`](https://github.com/filamentphp/spatie-laravel-translatable-plugin) in case you are using [Filament](https://filamentphp.com/).
3939

4040
## ✅ Requirements
4141

@@ -44,6 +44,7 @@ This package also supports [`spatie/nova-translatable`](https://github.com/spati
4444
- [Laravel](https://laravel.com/) >= 6
4545
- [spatie/laravel-translatable](https://github.com/spatie/laravel-translatable) ^4.4|^5.0
4646
- [spatie/nova-translatable](https://github.com/spatie/nova-translatable/) ^3.0
47+
- [filamentphp/spatie-laravel-translatable-plugin](https://github.com/filamentphp/spatie-laravel-translatable-plugin) ^3.0
4748

4849
## 📦 Installation
4950

@@ -205,6 +206,26 @@ Text::make(__('Slug'), 'slug')
205206
->updateRules('unique_translation:posts,slug,{{resourceId}}');
206207
```
207208

209+
### ☑️ Filament
210+
211+
If you are using [Filament](https://filamentphp.com/) in combination with [`filamentphp/spatie-laravel-translatable-plugin`](https://github.com/filamentphp/spatie-laravel-translatable-plugin), then you can add the validation rule like this:
212+
213+
```php
214+
TextInput::make('slug')
215+
->title(__('Slug'))
216+
->rules([
217+
UniqueTranslationRule::for('posts', 'slug')
218+
])
219+
```
220+
221+
```php
222+
TextInput::make('slug')
223+
->title(__('Slug'))
224+
->rules([
225+
fn (Get $get) => UniqueTranslationRule::for('posts', 'slug')->ignore($get('id'))
226+
])
227+
```
228+
208229
## 🖥 Example
209230

210231
Your existing `slug` column (JSON) in a `posts` table:

src/UniqueTranslationValidator.php

+35-5
Original file line numberDiff line numberDiff line change
@@ -20,10 +20,14 @@ class UniqueTranslationValidator
2020
* @return bool
2121
*/
2222
public function validate($attribute, $value, $parameters, $validator)
23-
{
24-
list ($name, $locale) = $this->isNovaTranslation($attribute)
23+
{
24+
list ($name, $locale) = $this->isNovaTranslation($attribute)
2525
? $this->getNovaAttributeNameAndLocale($attribute)
26-
: $this->getArrayAttributeNameAndLocale($attribute);
26+
: (
27+
$this->isFilamentTranslation($attribute)
28+
? $this->getFilamentAttributeNameAndLocale($attribute)
29+
: $this->getArrayAttributeNameAndLocale($attribute)
30+
);
2731

2832
if ($this->isUnique($value, $name, $locale, $parameters)) {
2933
return true;
@@ -74,7 +78,7 @@ protected function isNovaTranslation($attribute)
7478
}
7579

7680
/**
77-
* Get the attribute name and locale of a Nova translation field.
81+
* Get the attribute name and locale of a Filament translation field.
7882
*
7983
* @param string $attribute
8084
*
@@ -87,6 +91,32 @@ protected function getNovaAttributeNameAndLocale($attribute)
8791
return $this->getAttributeNameAndLocale($attribute, '_');
8892
}
8993

94+
/**
95+
* Check if the attribute is a Filament translation field name.
96+
*
97+
* @param string $attribute
98+
*
99+
* @return bool
100+
*/
101+
protected function isFilamentTranslation($attribute)
102+
{
103+
return strpos($attribute, 'data.') === 0;
104+
}
105+
106+
/**
107+
* Get the attribute name and locale of a Filament translation field.
108+
*
109+
* @param string $attribute
110+
*
111+
* @return array
112+
*/
113+
protected function getFilamentAttributeNameAndLocale($attribute)
114+
{
115+
$attribute = str_replace('data.', '', $attribute);
116+
@list($locale, $name) = @explode('.', $attribute);
117+
return [$name, $locale];
118+
}
119+
90120
/**
91121
* Get the attribute name and locale of an array field.
92122
*
@@ -210,7 +240,7 @@ protected function isUnique($value, $name, $locale, $parameters)
210240
$query = $this->findTranslation($connection, $table, $column, $locale, $value);
211241
$query = $this->ignore($query, $ignoreColumn, $ignoreValue);
212242
$query = $this->addConditions($query, $this->getUniqueExtra($parameters));
213-
243+
214244
$isUnique = $query->count() === 0;
215245

216246
return $isUnique;

0 commit comments

Comments
 (0)