Skip to content

Commit

Permalink
Merge branch 'master' of github.com:kongulov/nova-tab-translatable
Browse files Browse the repository at this point in the history
  • Loading branch information
kongulov committed Mar 10, 2021
2 parents 639879c + 96a21bb commit d1a8d42
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 6 deletions.
22 changes: 21 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
# Making Nova Tab Translatable
[![Latest Version on Packagist](https://img.shields.io/packagist/v/kongulov/nova-tab-translatable?style=flat-square)](https://packagist.org/packages/kongulov/nova-tab-translatable)
![Licence](https://img.shields.io/github/license/kongulov/nova-tab-translatable?style=flat-square)
[![Total Downloads](https://poser.pugx.org/kongulov/nova-tab-translatable/downloads?format=flat-square)](https://packagist.org/packages/kongulov/nova-tab-translatable)


This package contains a `NovaTabTranslatable` class you can use to make any Nova field type translatable with tabs.

Expand Down Expand Up @@ -32,7 +36,7 @@ That Post Nova resource will be rendered like this.

## Requirements

- `laravel/nova: ^2.9`
- `php: >=7.1.0`
- `spatie/laravel-translatable: ^4.0`

## Installation
Expand Down Expand Up @@ -122,6 +126,22 @@ NovaTabTranslatable::make([
]),
```

* Replace field name
```php
NovaTabTranslatable::make([
Text::make('Title')->rules('required'),
])->setTitle('Own Title'),
```

* If on the index and detail pages you want to turn off the tab and show it each as a row, use trait `TranslatableTabToRowTrait` in your resource
```php
class YourResource extends Resource
{
use TranslatableTabToRowTrait;
...
}
```

## Credits

- [Ramiz Kongulov](https://github.com/kongulov)
Expand Down
1 change: 0 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@
],
"require": {
"php": ">=7.1.0",
"laravel/nova": "^3.0",
"spatie/laravel-translatable": "^4.0"
},
"autoload": {
Expand Down
13 changes: 9 additions & 4 deletions src/NovaTabTranslatable.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use Drobee\NovaSluggable\SluggableText;
use Epartment\NovaDependencyContainer\NovaDependencyContainer;
use Illuminate\Contracts\Validation\Rule;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Str;
use Laravel\Nova\Fields\Field;
Expand Down Expand Up @@ -71,10 +72,12 @@ protected function createTranslatableFields()
collect($this->locales)
->crossJoin($this->originalFields)
->eachSpread(function (string $locale, Field $field) {
$translatedField = $this->createTranslatedField($field, $locale);
if($field->authorizedToSee(request())) {
$translatedField = $this->createTranslatedField($field, $locale);

$this->data[] = $translatedField;
$this->translatedFieldsByLocale[$locale][] = $translatedField;
$this->data[] = $translatedField;
$this->translatedFieldsByLocale[$locale][] = $translatedField;
}
});
}

Expand Down Expand Up @@ -121,12 +124,14 @@ protected function createTranslatedField(Field $originalField, string $locale):
return $translatedField;
}

public function setRules($translatedField){
public function setRules($translatedField) {

$translatedField->creationRules = $this->setUnique($translatedField->creationRules, $translatedField->meta['locale']);
$translatedField->updateRules = $this->setUnique($translatedField->updateRules, $translatedField->meta['locale']);

foreach ($translatedField->rules as $key => &$rule) {
if ($rule instanceof Rule) continue;

if (strpos($rule, 'required_lang') !== false){
$langs = explode(',', Str::after($rule,'required_lang:'));

Expand Down

0 comments on commit d1a8d42

Please sign in to comment.