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

Feature - Translated column #1501

Merged
merged 12 commits into from
Mar 11, 2024
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,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?
\Lunar\Admin\Support\Tables\Columns\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 @@ -93,7 +93,7 @@ public function table(Table $table): Table
{
return $table
->columns([
Tables\Columns\TextColumn::make('name.en')->label(
\Lunar\Admin\Support\Tables\Columns\TranslatedTextColumn::make('name')->label(
__('lunarpanel::attribute.table.name.label')
),
Tables\Columns\TextColumn::make('handle')
Expand Down
15 changes: 3 additions & 12 deletions packages/admin/src/Filament/Resources/ProductResource.php
Original file line number Diff line number Diff line change
Expand Up @@ -232,19 +232,10 @@ protected static function getTableColumns(): array
->limit(1)
->square()
->label(''),
Tables\Columns\TextColumn::make('attribute_data.name')
->formatStateUsing(fn (Model $record): string => $record->translateAttribute('name'))
\Lunar\Admin\Support\Tables\Columns\TranslatedTextColumn::make('attribute_data.name')
->attributeData()
->limitedTooltip()
->limit(50)
->tooltip(function (Tables\Columns\TextColumn $column, Model $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');
})
->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 @@ -33,20 +33,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'))
\Lunar\Admin\Support\Tables\Columns\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 @@ -54,19 +54,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'))
\Lunar\Admin\Support\Tables\Columns\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
Loading