Conditional Pageblock Dependent On Resource

Is there a way to only display a pageblock in a particular resource? I have this for example:
public static function getBlockSchema(): Block
{
return Block::make('exit_popup')
->label('Exit Popup')
->when('resource', 'ComponentResource')
->schema([
TextInput::make('title')
->label('Title')
->required(),

TextArea::make('intro')
->label('Intro')
->required(),

TextInput::make('submission_message')
->label('Submission Message')
->required(),

TextInput::make('button_label')
->label('Button Label')
->required(),
]);
}
public static function getBlockSchema(): Block
{
return Block::make('exit_popup')
->label('Exit Popup')
->when('resource', 'ComponentResource')
->schema([
TextInput::make('title')
->label('Title')
->required(),

TextArea::make('intro')
->label('Intro')
->required(),

TextInput::make('submission_message')
->label('Submission Message')
->required(),

TextInput::make('button_label')
->label('Button Label')
->required(),
]);
}
I only want this pageblock to be visible within the ComponentResource. Is this possible?
12 Replies
LeandroFerreira
LeandroFerreira4mo ago
->visible(get_called_class() === ComponentResource::class)
->visible(get_called_class() === ComponentResource::class)
?
nathan269_
nathan269_4mo ago
Thanks for the suggestion Leandro, but no luck with that. It doesn't display on any resource when I try that. both get_called_class() and ComponentResource::class are the same value on the page I'm testing on, so I don't see why it shouldn't work. I also tried ->hidden !==
Tieme
Tieme4mo ago
dont know if it exsist on a Block. But try
->visibleOn(ComponentResource::class)
->visibleOn(ComponentResource::class)
nathan269_
nathan269_4mo ago
Thanks Tieme but that doesn't work either
LeandroFerreira
LeandroFerreira4mo ago
== instead of === ?
nathan269_
nathan269_4mo ago
tried that also. No luck
LeandroFerreira
LeandroFerreira4mo ago
->visible(str_contains(get_called_class(), 'ComponentResource')) ?
nathan269_
nathan269_4mo ago
doesn't work either
LeandroFerreira
LeandroFerreira4mo ago
did you remove ->when() ?
nathan269_
nathan269_4mo ago
yeah it's removed and get_called_class() definitely returns "App\Filament\Resources\ComponentResource"
LeandroFerreira
LeandroFerreira4mo ago
it should work
nathan269_
nathan269_4mo ago
what's also weird is that if I have this:
->hidden(str_contains(get_called_class(), 'App\Filament\Resources\ComponentResource'))
->hidden(str_contains(get_called_class(), 'App\Filament\Resources\ComponentResource'))
it does display as an option Forgot to put this in here but I used this as my solution:
->hidden(Str::contains(request()->getRequestUri(), 'pages'))
->hidden(Str::contains(request()->getRequestUri(), 'pages'))
I didn't want the exit popup to be an option for the pages section of the admin.