F
Filament10mo ago
Patrick

open action when a GET parameter is set

I'd like to open a specific action when a user comes to a page and a specific $_GET parameter is present. It's an edit form of a resource, which contains a header action button. Which has a wireclick:
wire:click="mountAction('Inplannen')"
wire:click="mountAction('Inplannen')"
I would like to automatically open this action when my URL looks like this: /admin/education/1/edit?openPlanningForm=true Is there a way to do this? I was thinking of dispatching something in the mount method , but I don't know what i should dispatch. My edit page:
class EditEducation extends EditRecord
{
protected static string $resource = EducationResource::class;

protected function getHeaderActions(): array
{
return [
Educations::getHeaderPlanAction(),
Actions\DeleteAction::make(),
];
}

public function mount(int|string $record): void
{
parent::mount($record);
if(request()->has('openPlanningForm')){
// do something to open the planning form
}
}
}
class EditEducation extends EditRecord
{
protected static string $resource = EducationResource::class;

protected function getHeaderActions(): array
{
return [
Educations::getHeaderPlanAction(),
Actions\DeleteAction::make(),
];
}

public function mount(int|string $record): void
{
parent::mount($record);
if(request()->has('openPlanningForm')){
// do something to open the planning form
}
}
}
Any tips?
10 Replies
Patrick
PatrickOP10mo ago
I tried this, but does nothing:
public $openPlanningFormOnMount = false;
public function rendered()
{
if($this->openPlanningFormOnMount){
$this->mountAction('Inplannen');
}
}

public function mount(int|string $record): void
{
parent::mount($record);
if(request()->has('openPlanningForm')){
// do something to open the planning form
$this->openPlanningFormOnMount = true;
}
}
public $openPlanningFormOnMount = false;
public function rendered()
{
if($this->openPlanningFormOnMount){
$this->mountAction('Inplannen');
}
}

public function mount(int|string $record): void
{
parent::mount($record);
if(request()->has('openPlanningForm')){
// do something to open the planning form
$this->openPlanningFormOnMount = true;
}
}
i could register a script and trigger it on the frontend maybe, but isn't there an easier way?
toeknee
toeknee10mo ago
$this->dispatch('open-modal', id: 'edit-user');
Patrick
PatrickOP10mo ago
Thank you! Im assuming the modal name is the same as the action?
No description
Patrick
PatrickOP10mo ago
doing this in mount or the livewire rendered hook doesnt trigger the modal:
public function mount(int|string $record): void
{
parent::mount($record);
if(request()->has('openPlanningForm')){
// do something to open the planning form
$this->dispatch('open-modal', id: 'Inplannen');
}
}
public function mount(int|string $record): void
{
parent::mount($record);
if(request()->has('openPlanningForm')){
// do something to open the planning form
$this->dispatch('open-modal', id: 'Inplannen');
}
}
After inspecting the mountAction method i can see a modal is called like this: , id: "{$this->getId()}-action"); mm yeah, when you click the button by hand
Patrick
PatrickOP10mo ago
this is the modal that is being opened
No description
toeknee
toeknee10mo ago
So in mount after the parent, if you then call it to open it should work I believe? Else use the js method
Patrick
PatrickOP10mo ago
ye but i dont know that ID in my code ill trigger a button click, that has the same effect/result thanks for the help!
toeknee
toeknee10mo ago
Ahh ok, no problem!
Patrick
PatrickOP10mo ago
I solved it, it feels a bit nasty, but hey it works. I added a custom data attribute to the action button. Gave the edit resource its own blade view (just copied the original one) Added this on top of the blade file:
@push('scripts')
@if(isset($_GET['openPlanningForm']))
<script>
document.addEventListener('DOMContentLoaded', function() {
setTimeout(() => {
var element = document.querySelector('[data-planbutton]');
if (element) {
element.click();
}
}, 1000);
});
</script>
@endif
@endpush
@push('scripts')
@if(isset($_GET['openPlanningForm']))
<script>
document.addEventListener('DOMContentLoaded', function() {
setTimeout(() => {
var element = document.querySelector('[data-planbutton]');
if (element) {
element.click();
}
}, 1000);
});
</script>
@endif
@endpush
mm i think i can better just hard @include the original in my own blade view yeah nice this works fine
@push('scripts')
@if(isset($_GET['openPlanningForm']))
<script>
document.addEventListener('DOMContentLoaded', function() {
setTimeout(() => {
var element = document.querySelector('[data-planbutton]');
if (element) {
element.click();
}
}, 1000);
});
</script>
@endif
@endpush
@include('filament-panels::resources.pages.edit-record')
@push('scripts')
@if(isset($_GET['openPlanningForm']))
<script>
document.addEventListener('DOMContentLoaded', function() {
setTimeout(() => {
var element = document.querySelector('[data-planbutton]');
if (element) {
element.click();
}
}, 1000);
});
</script>
@endif
@endpush
@include('filament-panels::resources.pages.edit-record')
Not going to mark this as answer because i have a feeling this is not the most optimal way 😛
Want results from more Discord servers?
Add your server