Skip to content

Add Filament support. #26

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Nov 14, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 22 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ But then you want to make sure each translation is unique for its language.

That's where this package comes in to play.

This package also supports [`spatie/nova-translatable`](https://github.com/spatie/nova-translatable/) in case you are using [Laravel Nova](https://nova.laravel.com/).
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/).

## ✅ Requirements

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

## 📦 Installation

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

### ☑️ Filament

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:

```php
TextInput::make('slug')
->title(__('Slug'))
->rules([
UniqueTranslationRule::for('posts', 'slug')
])
```

```php
TextInput::make('slug')
->title(__('Slug'))
->rules([
fn (Get $get) => UniqueTranslationRule::for('posts', 'slug')->ignore($get('id'))
])
```

## 🖥 Example

Your existing `slug` column (JSON) in a `posts` table:
Expand Down
40 changes: 35 additions & 5 deletions src/UniqueTranslationValidator.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,14 @@ class UniqueTranslationValidator
* @return bool
*/
public function validate($attribute, $value, $parameters, $validator)
{
list ($name, $locale) = $this->isNovaTranslation($attribute)
{
list ($name, $locale) = $this->isNovaTranslation($attribute)
? $this->getNovaAttributeNameAndLocale($attribute)
: $this->getArrayAttributeNameAndLocale($attribute);
: (
$this->isFilamentTranslation($attribute)
? $this->getFilamentAttributeNameAndLocale($attribute)
: $this->getArrayAttributeNameAndLocale($attribute)
);

if ($this->isUnique($value, $name, $locale, $parameters)) {
return true;
Expand Down Expand Up @@ -74,7 +78,7 @@ protected function isNovaTranslation($attribute)
}

/**
* Get the attribute name and locale of a Nova translation field.
* Get the attribute name and locale of a Filament translation field.
*
* @param string $attribute
*
Expand All @@ -87,6 +91,32 @@ protected function getNovaAttributeNameAndLocale($attribute)
return $this->getAttributeNameAndLocale($attribute, '_');
}

/**
* Check if the attribute is a Filament translation field name.
*
* @param string $attribute
*
* @return bool
*/
protected function isFilamentTranslation($attribute)
{
return strpos($attribute, 'data.') === 0;
}

/**
* Get the attribute name and locale of a Filament translation field.
*
* @param string $attribute
*
* @return array
*/
protected function getFilamentAttributeNameAndLocale($attribute)
{
$attribute = str_replace('data.', '', $attribute);
@list($locale, $name) = @explode('.', $attribute);
return [$name, $locale];
}

/**
* Get the attribute name and locale of an array field.
*
Expand Down Expand Up @@ -210,7 +240,7 @@ protected function isUnique($value, $name, $locale, $parameters)
$query = $this->findTranslation($connection, $table, $column, $locale, $value);
$query = $this->ignore($query, $ignoreColumn, $ignoreValue);
$query = $this->addConditions($query, $this->getUniqueExtra($parameters));

$isUnique = $query->count() === 0;

return $isUnique;
Expand Down