-
Notifications
You must be signed in to change notification settings - Fork 372
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
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
1 parent
d7eea77
commit 1f38915
Showing
8 changed files
with
99 additions
and
51 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
77 changes: 77 additions & 0 deletions
77
packages/admin/src/Support/Tables/Columns/TranslatedTextColumn.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters