F
Filament8mo ago
CGM

How do I pass values through a render hook?

I have the following hook for my EditUser::class
->renderHook(
PanelsRenderHook::CONTENT_END,
fn (): View => view('components.tenant.roles.role-editor'),
[EditUser::class],
)
->renderHook(
PanelsRenderHook::CONTENT_END,
fn (): View => view('components.tenant.roles.role-editor'),
[EditUser::class],
)
but the view takes a prop of @props(['userId']) how do I get this from my EditUser::class and into the view?
2 Replies
CGM
CGMOP8mo ago
$record->id is what I'm trying to get to the view in particular. This didn't work, but I think I'm attempting something like this...
->renderHook(
PanelsRenderHook::CONTENT_END,
fn ($record): View => view('components.tenant.roles.role-editor', $record->id),
[EditUser::class],
)
->renderHook(
PanelsRenderHook::CONTENT_END,
fn ($record): View => view('components.tenant.roles.role-editor', $record->id),
[EditUser::class],
)
I've made some progress and got the view working, but this doesn't feel right. Is there a better way to access the $record from the EditUser::class in my view?
->renderHook(
PanelsRenderHook::CONTENT_END,
fn (): View => view('components.tenant.roles.role-editor', ['userId' => request('record')]),
[EditUser::class],
)
->renderHook(
PanelsRenderHook::CONTENT_END,
fn (): View => view('components.tenant.roles.role-editor', ['userId' => request('record')]),
[EditUser::class],
)
For reference, I'm using the renderHook() in my panel service provider.
James
James7mo ago
I've run across the same thing, and found a different slightly hacky approach: You can add the record id in the getRenderHookScopes method of (e.g.) the ViewRecord class, so put in something like:
public function getRenderHookScopes(): array
{
return [ ...parent::getRenderHookScopes(), $this->record->id ];
}
public function getRenderHookScopes(): array
{
return [ ...parent::getRenderHookScopes(), $this->record->id ];
}
So then in the render hook you can access that id like:
FilamentView::registerRenderHook(
PanelsRenderHook::PAGE_HEADER_WIDGETS_BEFORE,
fn (array $scopes) => Report::find(end($scopes))->description,
scopes: [
ViewUsersReport::class,
],
);
FilamentView::registerRenderHook(
PanelsRenderHook::PAGE_HEADER_WIDGETS_BEFORE,
fn (array $scopes) => Report::find(end($scopes))->description,
scopes: [
ViewUsersReport::class,
],
);
It's nasty, and I wil be adding some more checks to this to prevent crashes etc. but the idea works.

Did you find this page helpful?