diff --git a/README.md b/README.md index 94c72dd..3d56a8a 100644 --- a/README.md +++ b/README.md @@ -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. @@ -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 @@ -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) diff --git a/composer.json b/composer.json index 62e3dff..fc4ecda 100644 --- a/composer.json +++ b/composer.json @@ -16,7 +16,6 @@ ], "require": { "php": ">=7.1.0", - "laravel/nova": "^3.0", "spatie/laravel-translatable": "^4.0" }, "autoload": { diff --git a/src/NovaTabTranslatable.php b/src/NovaTabTranslatable.php index bbb6392..8174b0e 100644 --- a/src/NovaTabTranslatable.php +++ b/src/NovaTabTranslatable.php @@ -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; @@ -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; + } }); } @@ -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:'));