Skip to content
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

Update NovaTabTranslatable.php #47

Merged
merged 9 commits into from
Jan 8, 2025
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
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
# Making Laravel Nova Tab Translatable
# Making Laravel Nova Tab Translatable Nova 5 ready
[![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.
This package contains a `NovaTabTranslatable` class you can use to make any Nova field type translatable with tabs. Use the master branch for Nova 5 and the 1.x branch for Nova 4.

Imagine you have this `fields` method in a Post Nova resource:

Expand Down
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
"require": {
"php": "^8.0",
"spatie/laravel-translatable": "^5.0|^6.0",
"laravel/nova": "^4.0"
"laravel/nova": "^5.0"
},
"autoload": {
"psr-4": {
Expand Down
1 change: 0 additions & 1 deletion nova.mix.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ class NovaExtension {
webpackPlugins() {
return new webpack.ProvidePlugin({
_: 'lodash',
Errors: 'form-backend-validation',
})
}

Expand Down
26 changes: 15 additions & 11 deletions src/NovaTabTranslatable.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,13 @@
use Laravel\Nova\Fields\Image;
use Laravel\Nova\Fields\Slug;
use Laravel\Nova\Http\Requests\NovaRequest;
use Laravel\Nova\Fields\SupportsDependentFields;
use Throwable;

class NovaTabTranslatable extends Field
{
use SupportsDependentFields;

/**
* The field's component.
*
Expand All @@ -40,19 +43,20 @@ class NovaTabTranslatable extends Field

public $panel;

public function __construct(array $fields = [])
public function __construct(array $fields = [], array $locales = [])
{
parent::__construct($this->name);
$config = config('tab-translatable');
if ($config['source'] == 'database')
if ($config['source'] == 'database') {
$this->locales = $config['database']['model']::query()
->when(isset($config['database']['sort_by']), function ($query) use ($config) {
$query->orderBy($config['database']['sort_by'], $config['database']['sort_direction']);
})
->pluck($config['database']['code_field'])
->toArray();
else
$this->locales = $config['locales'];
} else {
$this->locales = count($locales) > 0 ? $locales : $config['locales'];
}

$this->displayLocalizedNameUsingCallback = self::$displayLocalizedNameByDefaultUsingCallback ?? function (Field $field, string $locale) {
return ucfirst($field->name) . " [{$locale}]";
Expand Down Expand Up @@ -85,7 +89,7 @@ public function saveLastSelectedLang(bool $state = true): self
]);
}

protected function createTranslatableFields()
protected function createTranslatableFields(): void
{
collect($this->locales)
->crossJoin($this->originalFields)
Expand Down Expand Up @@ -243,21 +247,21 @@ protected function compatibilityWithOtherPlugins($translatedField)
return $translatedField;
}

public function resolve($resource, $attribute = null)
public function resolve($resource, $attribute = null): void
{
foreach ($this->data as $field) {
$field->resolve($resource, $attribute);
}
}

public function fillInto($request, $model, $attribute, $requestAttribute = null)
public function fillInto($request, $model, $attribute, $requestAttribute = null): void
{
foreach ($this->data as $field) {
$field->fill($request, $model);
}
}

public function getCreationRules(NovaRequest $request)
public function getCreationRules(NovaRequest $request): array
{
$fieldsRules = $this->getSituationalRulesSet($request, 'creationRules');

Expand All @@ -267,7 +271,7 @@ public function getCreationRules(NovaRequest $request)
);
}

protected function getSituationalRulesSet(NovaRequest $request, string $propertyName = 'rules')
protected function getSituationalRulesSet(NovaRequest $request, string $propertyName = 'rules'): array
{
$fieldsRules = [$this->attribute => []];

Expand All @@ -280,7 +284,7 @@ protected function getSituationalRulesSet(NovaRequest $request, string $property
return $fieldsRules;
}

public function getUpdateRules(NovaRequest $request)
public function getUpdateRules(NovaRequest $request): array
{
$fieldsRules = $this->getSituationalRulesSet($request, 'updateRules');

Expand All @@ -290,7 +294,7 @@ public function getUpdateRules(NovaRequest $request)
);
}

public function getRules(NovaRequest $request)
public function getRules(NovaRequest $request): array
{
return $this->getSituationalRulesSet($request);
}
Expand Down