Skip to content

Commit

Permalink
Feature - Translated column (#1501)
Browse files Browse the repository at this point in the history
This PR add new custom TranslatedTextColumn use for translated fields
with some options :

- limitedToolip : display tooltip if the column contents exceeds the
length limit.
- attributeData : Render from attribute_data field

---------

Co-authored-by: Glenn Jacobs <glenn@neondigital.co.uk>
  • Loading branch information
lguichard and glennjacobs authored Mar 11, 2024
1 parent d7eea77 commit 1f38915
Show file tree
Hide file tree
Showing 8 changed files with 99 additions and 51 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
use Lunar\Admin\Filament\Resources\AttributeGroupResource\Pages;
use Lunar\Admin\Filament\Resources\AttributeGroupResource\RelationManagers;
use Lunar\Admin\Support\Resources\BaseResource;
use Lunar\Admin\Support\Tables\Columns\TranslatedTextColumn;
use Lunar\Models\AttributeGroup;

class AttributeGroupResource extends BaseResource
Expand Down Expand Up @@ -103,7 +104,7 @@ public static function getDefaultTable(Table $table): Table
->columns([
Tables\Columns\TextColumn::make('attributable_type')
->label(__('lunarpanel::attributegroup.table.attributable_type.label')),
Tables\Columns\TextColumn::make('name.en') // TODO: Need to determine correct way to localise, maybe custom column type?
TranslatedTextColumn::make('name')
->label(__('lunarpanel::attributegroup.table.name.label')),
Tables\Columns\TextColumn::make('handle')
->label(__('lunarpanel::attributegroup.table.handle.label')),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
use Illuminate\Support\Str;
use Illuminate\Validation\Rules\Unique;
use Lunar\Admin\Support\Facades\AttributeData;
use Lunar\Admin\Support\Tables\Columns\TranslatedTextColumn;
use Lunar\Admin\Support\Forms\Components\TranslatedText;
use Lunar\Models\Language;

Expand Down Expand Up @@ -103,7 +104,7 @@ public function table(Table $table): Table
{
return $table
->columns([
Tables\Columns\TextColumn::make('name.en')->label(
TranslatedTextColumn::make('name')->label(
__('lunarpanel::attribute.table.name.label')
),
Tables\Columns\TextColumn::make('description.en')->label(
Expand Down
12 changes: 4 additions & 8 deletions packages/admin/src/Filament/Resources/ProductOptionResource.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
use Lunar\Admin\Filament\Resources\ProductOptionResource\RelationManagers;
use Lunar\Admin\Support\Forms\Components\TranslatedText;
use Lunar\Admin\Support\Resources\BaseResource;
use Lunar\Admin\Support\Tables\Columns\TranslatedTextColumn;
use Lunar\Models\ProductOption;

class ProductOptionResource extends BaseResource
Expand Down Expand Up @@ -80,14 +81,9 @@ public static function getDefaultTable(Table $table): Table
{
return $table
->columns([
Tables\Columns\TextColumn::make('name')
->formatStateUsing(
fn (ProductOption $option) => $option->translate('name'),
)->label(__('lunarpanel::productoption.table.name.label')),
Tables\Columns\TextColumn::make('label')
->formatStateUsing(
fn (ProductOption $option) => $option->translate('label'),
)
TranslatedTextColumn::make('name')
->label(__('lunarpanel::productoption.table.name.label')),
TranslatedTextColumn::make('label')
->label(__('lunarpanel::productoption.table.label.label')),
Tables\Columns\TextColumn::make('handle')
->label(__('lunarpanel::productoption.table.handle.label')),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@
use Filament\Tables;
use Filament\Tables\Table;
use Illuminate\Database\Eloquent\Model;
use Lunar\Admin\Support\Tables\Columns\TranslatedTextColumn;
use Lunar\Admin\Support\Forms\Components\TranslatedText;
use Lunar\Models\ProductOptionValue;

class ValuesRelationManager extends RelationManager
{
Expand All @@ -34,10 +34,7 @@ public function table(Table $table): Table
return $table

->columns([
Tables\Columns\TextColumn::make('name')
->formatStateUsing(
fn (ProductOptionValue $productOption) => $productOption->translate('name')
),
TranslatedTextColumn::make('name'),
Tables\Columns\TextColumn::make('position'),
])
->filters([
Expand Down
15 changes: 4 additions & 11 deletions packages/admin/src/Filament/Resources/ProductResource.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
use Lunar\Admin\Support\RelationManagers\MediaRelationManager;
use Lunar\Admin\Support\RelationManagers\PriceRelationManager;
use Lunar\Admin\Support\Resources\BaseResource;
use Lunar\Admin\Support\Tables\Columns\TranslatedTextColumn;
use Lunar\Models\Currency;
use Lunar\Models\Product;
use Lunar\Models\ProductVariant;
Expand Down Expand Up @@ -247,18 +248,10 @@ public static function getTableColumns(): array
->limit(1)
->square()
->label(''),
Tables\Columns\TextColumn::make('attribute_data.name')
->formatStateUsing(fn (Model $record): string => $record->translateAttribute('name'))
TranslatedTextColumn::make('attribute_data.name')
->attributeData()
->limitedTooltip()
->limit(50)
->tooltip(function (Tables\Columns\TextColumn $column, Model $record): ?string {

if (strlen($record->translateAttribute('name')) <= $column->getCharacterLimit()) {
return null;
}

// Only render the tooltip if the column contents exceeds the length limit.
return $record->translateAttribute('name');
})
->label(__('lunarpanel::product.table.name.label')),
Tables\Columns\TextColumn::make('brand.name')
->label(__('lunarpanel::product.table.brand.label'))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
use Filament\Tables;
use Filament\Tables\Table;
use Lunar\Admin\Filament\Resources\ProductResource;
use Lunar\Admin\Support\Tables\Columns\TranslatedTextColumn;
use Lunar\Admin\Support\Pages\BaseManageRelatedRecords;
use Lunar\Models\Collection;

Expand All @@ -33,20 +34,10 @@ public function table(Table $table): Table
return $table
->recordTitleAttribute('name')
->columns([
Tables\Columns\TextColumn::make('attribute_data.name')
->formatStateUsing(fn (Collection $record): string => $record->translateAttribute('name'))
TranslatedTextColumn::make('attribute_data.name')
->attributeData()
->limitedTooltip()
->limit(50)
->tooltip(function (Tables\Columns\TextColumn $column, Collection $record): ?string {
$state = $column->getState();

if (strlen($record->translateAttribute('name')) <= $column->getCharacterLimit()) {
return null;
}

// Only render the tooltip if the column contents exceeds the length limit.
return $record->translateAttribute('name');
})
->description(fn (Collection $record): string => $record->breadcrumb->join(' > '))
->label(__('lunarpanel::product.table.name.label')),
])
->filters([
Expand Down
77 changes: 77 additions & 0 deletions packages/admin/src/Support/Tables/Columns/TranslatedTextColumn.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
<?php

namespace Lunar\Admin\Support\Tables\Columns;

use Filament\Tables\Columns\TextColumn;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Str;

class TranslatedTextColumn extends TextColumn
{
protected bool $attributeData = false;

protected string $fieldHydrated = '';

protected function setUp(): void
{
parent::setUp();

$name = $this->getName();

$this->formatStateUsing(static function (Model $record) use ($name) {
return $record->translate($name);
});
}

public function fieldHydrated(string $fieldHydrated): static
{
$this->fieldHydrated = $fieldHydrated;

return $this;
}

public function limitedTooltip(): static
{
$attributeData = $this->getAttributeData();

$name = $this->getFieldHydrated();

$this->tooltip(function (TextColumn $column, Model $record) use ($name, $attributeData): ?string {
$state = $attributeData ? $record->translateAttribute($name) : $record->translate($name);

if (strlen($state) <= $column->getCharacterLimit()) {
return null;
}

// Only render the tooltip if the column contents exceeds the length limit.
return $state;
});

return $this;
}

public function attributeData(): static
{
$this->attributeData = true;

$this->fieldHydrated(Str::replace('attribute_data.', '', $this->getName()));

$name = $this->getFieldHydrated();

$this->formatStateUsing(static function (Model $record) use ($name) {
return $record->translateAttribute($name);
});

return $this;
}

public function getFieldHydrated(): string
{
return $this->fieldHydrated;
}

public function getAttributeData(): bool
{
return $this->attributeData;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
use Filament\Tables;
use Filament\Tables\Table;
use Illuminate\Database\Eloquent\Model;
use Lunar\Admin\Support\Tables\Columns\TranslatedTextColumn;
use Lunar\Models\Product;

class ShippingExclusionRelationManager extends RelationManager
Expand Down Expand Up @@ -54,19 +55,10 @@ public function table(Table $table): Table
->limit(1)
->square()
->label(''),
Tables\Columns\TextColumn::make('purchasable.attribute_data.name')
->formatStateUsing(fn (Model $record): string => $record->purchasable->translateAttribute('name'))
TranslatedTextColumn::make('attribute_data.name')
->attributeData()
->limitedTooltip()
->limit(50)
->tooltip(function (Tables\Columns\TextColumn $column, Model $record): ?string {
$state = $column->getState();
$record = $record->purchasable;

if (strlen($record->translateAttribute('name')) <= $column->getCharacterLimit()) {
return null;
}

return $record->translateAttribute('name');
})
->label(__('lunarpanel::product.table.name.label')),
Tables\Columns\TextColumn::make('purchasable.variants.sku')
->label(__('lunarpanel::product.table.sku.label'))
Expand Down

0 comments on commit 1f38915

Please sign in to comment.