信息列表
自定义 Entry
简介
你可以创建你自己的自定义条目(Entry)类及视图,它可以在整个应用中进行重用,甚至可以将其作为插件发布到社区。
你可以创建自定义条目(Entry)类及视图,请要使用如下命令:
php artisan make:filament-infolist-entry AudioPlayerEntry
该命令将创建以下组件类:
use Filament\Infolists\Components\Entry;
class AudioPlayerEntry extends Entry
{
protected string $view = 'filament.infolists.components.audio-player-entry';
}
同时生成一个视图文件:resources/views/filament/infolists/components/audio-player-entry.blade.php
。
NOTE
Filament 信息列表条目不是 Livewire 组件。在条目类中定义公共属性和方法不会使之在 Blade 视图中可访问到。
在 Blade 视图中访问条目状态
在 Blade 视图中,你可以使用 $getState()
函数访问条目的状态值:
<x-dynamic-component
:component="$getEntryWrapperView()"
:entry="$entry"
>
{{ $getState() }}
</x-dynamic-component>
在 Blade 视图中访问其他组件的状态
在 Blade 视图中,你可以使用 $get()
函数访问 Schema 中其他组件的状态值:
<x-dynamic-component
:component="$getEntryWrapperView()"
:entry="$entry"
>
{{ $get('email') }}
</x-dynamic-component>
TIP
Unless a form field is reactive, the Blade view will not refresh when the value of the field changes, only when the next user interaction occurs that makes a request to the server. If you need to react to changes in a field’s value, it should be live()
.
在 Blade 视图中访问 Eloquent 记录
Inside the Blade view, you may access the current Eloquent record using the $record
variable:
<x-dynamic-component
:component="$getEntryWrapperView()"
:entry="$entry"
>
{{ $record->name }}
</x-dynamic-component>
在 Blade 视图中访问当前操作
Inside the Blade view, you may access the current operation, usually create
, edit
or view
, using the $operation
variable:
<x-dynamic-component
:component="$getEntryWrapperView()"
:entry="$entry"
>
@if ($operation === 'create')
This is a new conference.
@else
This is an existing conference.
@endif
</x-dynamic-component>
在 Blade 视图中访问当前 Livewire 组件
Inside the Blade view, you may access the current Livewire component instance using $this
:
@php
use Filament\Resources\Users\RelationManagers\ConferencesRelationManager;
@endphp
<x-dynamic-component
:component="$getEntryWrapperView()"
:entry="$entry"
>
@if ($this instanceof ConferencesRelationManager)
You are editing conferences the of a user.
@endif
</x-dynamic-component>
在 Blade 视图中访问当前 Entry 实例
Inside the Blade view, you may access the current entry instance using $entry
. You can call public methods on this object to access other information that may not be available in variables:
<x-dynamic-component
:component="$getEntryWrapperView()"
:entry="$entry"
>
@if ($entry->isLabelHidden())
This is a new conference.
@endif
</x-dynamic-component>
Adding a configuration method to a custom entry class
You may add a public method to the custom entry class that accepts a configuration value, stores it in a protected property, and returns it again from another public method:
use Filament\Infolists\Components\Entry;
class AudioPlayerEntry extends Entry
{
protected string $view = 'filament.infolists.components.audio-player-entry';
protected ?float $speed = null;
public function speed(?float $speed): static
{
$this->speed = $speed;
return $this;
}
public function getSpeed(): ?float
{
return $this->speed;
}
}
Now, in the Blade view for the custom entry, you may access the speed using the $getSpeed()
function:
<x-dynamic-component
:component="$getEntryWrapperView()"
:entry="$entry"
>
{{ $getSpeed() }}
</x-dynamic-component>
Any public method that you define on the custom entry class can be accessed in the Blade view as a variable function in this way.
To pass the configuration value to the custom entry class, you may use the public method:
use App\Filament\Infolists\Components\AudioPlayerEntry;
AudioPlayerEntry::make('recording')
->speed(0.5)
Allowing utility injection in a custom entry configuration method
Utility injection is a powerful feature of Filament that allows users to configure a component using functions that can access various utilities. You can allow utility injection by ensuring that the parameter type and property type of the configuration allows the user to pass a Closure
. In the getter method, you should pass the configuration value to the $this->evaluate()
method, which will inject utilities into the user’s function if they pass one, or return the value if it is static:
use Closure;
use Filament\Infolists\Components\Entry;
class AudioPlayerEntry extends Entry
{
protected string $view = 'filament.infolists.components.audio-player-entry';
protected float | Closure | null $speed = null;
public function speed(float | Closure | null $speed): static
{
$this->speed = $speed;
return $this;
}
public function getSpeed(): ?float
{
return $this->evaluate($this->speed);
}
}
Now, you can pass a static value or a function to the speed()
method, and inject any utility as a parameter:
use App\Filament\Infolists\Components\AudioPlayerEntry;
AudioPlayerEntry::make('recording')
->speed(fn (Conference $record): float => $record->isGlobal() ? 1 : 0.5)
Edit on GitHubStill need help? Join our Discord community or open a GitHub discussion