F
Filament10mo ago
taqi

Solutions to Repetitive Code

TextEntry::make('email')
->formatStateUsing(fn(string $state): string => $this->hasAccess ? $state : __('Denied'))
->badge(fn() => !$this->hasAccess)
->icon(fn() => !$this->hasAcecss ? 'heroicon-o-lock-closed' : ''),
TextEntry::make('phone')
->formatStateUsing(fn(string $state): string => $this->hasAccess ? $state : __('Denied'))
->badge(fn() => !$this->hasAccess)
->icon(fn() => !$this->hasAcecss ? 'heroicon-o-lock-closed' : ''),
TextEntry::make('address')
->formatStateUsing(fn(string $state): string => $this->hasAccess ? $state : __('Denied'))
->badge(fn() => !$this->hasAccess)
->icon(fn() => !$this->hasAcecss ? 'heroicon-o-lock-closed' : ''),
TextEntry::make('email')
->formatStateUsing(fn(string $state): string => $this->hasAccess ? $state : __('Denied'))
->badge(fn() => !$this->hasAccess)
->icon(fn() => !$this->hasAcecss ? 'heroicon-o-lock-closed' : ''),
TextEntry::make('phone')
->formatStateUsing(fn(string $state): string => $this->hasAccess ? $state : __('Denied'))
->badge(fn() => !$this->hasAccess)
->icon(fn() => !$this->hasAcecss ? 'heroicon-o-lock-closed' : ''),
TextEntry::make('address')
->formatStateUsing(fn(string $state): string => $this->hasAccess ? $state : __('Denied'))
->badge(fn() => !$this->hasAccess)
->icon(fn() => !$this->hasAcecss ? 'heroicon-o-lock-closed' : ''),
See how many times the common function repeats. Is there a more concise and maintainable way to achieve this functionality without repeating or writing a bunch of code? I appreciate any examples or suggestions
Solution:
I have create a Helper class and createt a function like this ```php class Helpers {...
Jump to solution
4 Replies
Solution
Tieme
Tieme10mo ago
I have create a Helper class and createt a function like this
class Helpers
{
public static function RelationChangePlaceholder($type)
{
return \Filament\Forms\Components\Placeholder::make(uniqid())
->label(false)
->hiddenOn(OrdersResource::class)
->visible(fn ($context): int => $context === 'edit')
->content(fn (): string => Helpers::translate('Please note, when adjusting the details below, all orders that use ').' '.$type.' '.Helpers::translate('will also be adjusted.'))
->hintColor('warning')
->extraAttributes([
'class' => 'text-center p-2 rounded-md font-medium ring-1 ring-inset bg-warning-50 text-warning-600 ring-warning-600/10 dark:bg-warning-400/10 dark:text-warning-400 dark:ring-warning-400/30',
])
->columnSpanFull();
}
}
class Helpers
{
public static function RelationChangePlaceholder($type)
{
return \Filament\Forms\Components\Placeholder::make(uniqid())
->label(false)
->hiddenOn(OrdersResource::class)
->visible(fn ($context): int => $context === 'edit')
->content(fn (): string => Helpers::translate('Please note, when adjusting the details below, all orders that use ').' '.$type.' '.Helpers::translate('will also be adjusted.'))
->hintColor('warning')
->extraAttributes([
'class' => 'text-center p-2 rounded-md font-medium ring-1 ring-inset bg-warning-50 text-warning-600 ring-warning-600/10 dark:bg-warning-400/10 dark:text-warning-400 dark:ring-warning-400/30',
])
->columnSpanFull();
}
}
And using it like this
public static function form(Form $form): Form
{
return $form
->schema([
Helpers::RelationChangePlaceholder(Helpers::translate('this address')),

]);
}
public static function form(Form $form): Form
{
return $form
->schema([
Helpers::RelationChangePlaceholder(Helpers::translate('this address')),

]);
}
taqi
taqiOP10mo ago
BINGO!! IT WORKS!!!! Now I have to write this for my solution:
// customer function
public function applyPremiumDataRestrictions(string $column): TextEntry
{
return TextEntry::make($column)
->formatStateUsing(fn(string $state): string => $this->hasAccess ? $state : __('Denied'))
->badge(fn() => !$this->hasAccess)
->icon(fn() => !$this->hasAcecss ? 'heroicon-o-lock-closed' : '');
}

// inside schema
$this->applyPremiumDataRestrictions('name')->label(__('Name)),
// customer function
public function applyPremiumDataRestrictions(string $column): TextEntry
{
return TextEntry::make($column)
->formatStateUsing(fn(string $state): string => $this->hasAccess ? $state : __('Denied'))
->badge(fn() => !$this->hasAccess)
->icon(fn() => !$this->hasAcecss ? 'heroicon-o-lock-closed' : '');
}

// inside schema
$this->applyPremiumDataRestrictions('name')->label(__('Name)),
Instead of a helper class, I have created another function inside the same class. Thanks a lot my friend @Tieme 😊
Tieme
Tieme10mo ago
Inside the same class is also possible what you did. It is in my helper because i use it in all relations to a order, so multiple classes.
taqi
taqiOP10mo ago
I agree. I may need that in near future. I will go with the helper class approach then.
<?php

namespace App\Helpers;

use Filament\Infolists\Components\TextEntry;

class TextEntryPremium
{
public $hasAccess = false;

public static function make(string $column): TextEntry
{
return TextEntry::make($column)
->formatStateUsing(fn(string $state): string => $this->hasAccess ? $state : __('Denied'))
->badge(fn() => !$this->hasAccess)
->icon(fn() => !$this->hasAcecss ? 'heroicon-o-lock-closed' : '');
}
}
<?php

namespace App\Helpers;

use Filament\Infolists\Components\TextEntry;

class TextEntryPremium
{
public $hasAccess = false;

public static function make(string $column): TextEntry
{
return TextEntry::make($column)
->formatStateUsing(fn(string $state): string => $this->hasAccess ? $state : __('Denied'))
->badge(fn() => !$this->hasAccess)
->icon(fn() => !$this->hasAcecss ? 'heroicon-o-lock-closed' : '');
}
}
use App\Helpers\TextEntryPremium;

public function profileInfolist(Infolist $infolist): Infolist
{
return $infolist
->record($this->profile)
->schema([
TextEntry::make('name'),
TextEntryPremium::make('email'),
]);
}
use App\Helpers\TextEntryPremium;

public function profileInfolist(Infolist $infolist): Infolist
{
return $infolist
->record($this->profile)
->schema([
TextEntry::make('name'),
TextEntryPremium::make('email'),
]);
}
@Tieme This is the final solution I came up with
Want results from more Discord servers?
Add your server