Skip to content

Implement translatable collection. #360

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

Open
wants to merge 4 commits into
base: 3.x
Choose a base branch
from
Open
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
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,20 @@

namespace LaraZeus\Bolt\Filament\Resources\CollectionResource\Pages;

use Filament\Actions\LocaleSwitcher;
use Filament\Resources\Pages\CreateRecord;
use LaraZeus\Bolt\Filament\Resources\CollectionResource;

class CreateCollection extends CreateRecord
{
use CreateRecord\Concerns\Translatable;

protected static string $resource = CollectionResource::class;

protected function getHeaderActions(): array
{
return [
LocaleSwitcher::make(),
];
}
}
10 changes: 10 additions & 0 deletions src/Filament/Resources/CollectionResource/Pages/EditCollection.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,15 @@

namespace LaraZeus\Bolt\Filament\Resources\CollectionResource\Pages;

use Filament\Actions\LocaleSwitcher;
use Filament\Resources\Pages\EditRecord;
use LaraZeus\Bolt\Filament\Resources\CollectionResource;
use LaraZeus\Bolt\Filament\Resources\CollectionResource\Widgets\EditCollectionWarning;

class EditCollection extends EditRecord
{
use EditRecord\Concerns\Translatable;

protected static string $resource = CollectionResource::class;

protected function getHeaderWidgets(): array
Expand All @@ -16,4 +19,11 @@ protected function getHeaderWidgets(): array
EditCollectionWarning::class,
];
}

protected function getHeaderActions(): array
{
return [
LocaleSwitcher::make(),
];
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,15 @@

class ListCollections extends ListRecords
{
use ListRecords\Concerns\Translatable;

protected static string $resource = CollectionResource::class;

protected function getHeaderActions(): array
{
return [
Actions\CreateAction::make(),
Actions\LocaleSwitcher::make(),
];
}
}
30 changes: 27 additions & 3 deletions src/Models/Collection.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
use Illuminate\Database\Eloquent\SoftDeletes;
use LaraZeus\Bolt\Concerns\HasUpdates;
use LaraZeus\Bolt\Database\Factories\CollectionFactory;
use Spatie\Translatable\HasTranslations;

/**
* @property string $updated_at
Expand All @@ -19,12 +20,11 @@ class Collection extends Model
use HasFactory;
use HasUpdates;
use SoftDeletes;
use HasTranslations;

protected $guarded = [];

protected $casts = [
'values' => 'collection',
];
public $translatable = ['name', 'values'];

public function getTable(): string
{
Expand All @@ -47,6 +47,30 @@ public function getValuesListAttribute(): ?string
return null;
}

/**
* Returns the values as a collection. Translatable variables are always cast as an array. This function transforms
* it to a collection.
* Note: The newer Attribute approach does not seem to be compatible with laravel-translatable ;-(.
* @param $value
* @return \Illuminate\Support\Collection
*/
public function getValuesAttribute($value)
{
if($value instanceof \Illuminate\Support\Collection){
return $value;
}
if(empty($value)){
return collect();
}
if(is_array($value)){
return collect($value);
}
if(is_string($value)){
return collect(json_encode($value));
}
return collect($value);
}

protected static function newFactory(): Factory
{
return CollectionFactory::new();
Expand Down