F
Filament8mo ago
mxc

How to have an Action with a url() method, execute code before opening the link?

Hi there, I have an action object with a url link and I want to update the database when it is clicked. I can't get the database update to work in the action method or in any other method I tried. Is there a way to do this? Alternatively how do I open a link in the action method after updating the database?
Forms\Components\Actions\Action::make('appointment_entry')
->label('Calendar Entry')
->icon('heroicon-m-calendar-days')
->url(fn() => CustomerLink::where("id", "=", $this->customer->id)->get()->first()->url))
->openUrlInNewTab()
->action(function () {
$key = $this->initialise();
$counter = new Counter();
$counter->log_id = $key;
$counter->user_id = auth()->user()->id;
$counter->save();
})
Forms\Components\Actions\Action::make('appointment_entry')
->label('Calendar Entry')
->icon('heroicon-m-calendar-days')
->url(fn() => CustomerLink::where("id", "=", $this->customer->id)->get()->first()->url))
->openUrlInNewTab()
->action(function () {
$key = $this->initialise();
$counter = new Counter();
$counter->log_id = $key;
$counter->user_id = auth()->user()->id;
$counter->save();
})
Solution:
The answer is use HtmlString as suggested by @Leandro Ferreira
Jump to solution
5 Replies
Homd
Homd8mo ago
Forms\Components\Actions\Action::make('appointment_entry')
->label('Calendar Entry')
->icon('heroicon-m-calendar-days')
->url(function(){

$key = $this->initialise();
$counter = new Counter();
$counter->log_id = $key;
$counter->user_id = auth()->user()->id;
$counter->save();
}

return CustomerLink::where("id", "=", $this->customer->id)->get()->first()->url)
})
->openUrlInNewTab()
Forms\Components\Actions\Action::make('appointment_entry')
->label('Calendar Entry')
->icon('heroicon-m-calendar-days')
->url(function(){

$key = $this->initialise();
$counter = new Counter();
$counter->log_id = $key;
$counter->user_id = auth()->user()->id;
$counter->save();
}

return CustomerLink::where("id", "=", $this->customer->id)->get()->first()->url)
})
->openUrlInNewTab()
Something like that?
mxc
mxc8mo ago
Thanks @Homd when I tried this it would log the click when the url is generated for the page and not when it was clicked so it would record a click per page view. Let me test again.
Homd
Homd8mo ago
That actually make sense haha. Maybe just create a custom controller for that? I can't find anything in the docs that lets you do both action and url
mxc
mxc8mo ago
Ok, might be the only way. Trying to figure out how to send a redirect from the action method. Ok so I can do something like this in the blade template.
<button x-on:click="$wire.log_calendar_entry(); window.open('{{$this->calendar_url}}', '_blank');">
Click 2
</button>
<button x-on:click="$wire.log_calendar_entry(); window.open('{{$this->calendar_url}}', '_blank');">
Click 2
</button>
is there a way to have this added to the action in the form definition programatically? I.E. I don't need to go and move my actions out of the form to make them blade templates? Trying to use
)->extraAttributes(["x-on:click" =>"\$wire.log_calendar_entry(); window.open('".$this->calendar_url."', '_blank');"]),
)->extraAttributes(["x-on:click" =>"\$wire.log_calendar_entry(); window.open('".$this->calendar_url."', '_blank');"]),
but getting what appears to be issues around escaping apostrophes.
Alpine Expression Error: expected expression, got '&'

Expression: "$wire.log_calendar_entry(); window.open(&#039;https://customerlink.co.za&#039;, &#039;_blank&#039;);"
Alpine Expression Error: expected expression, got '&'

Expression: "$wire.log_calendar_entry(); window.open(&#039;https://customerlink.co.za&#039;, &#039;_blank&#039;);"
Solution
mxc
mxc8mo ago
The answer is use HtmlString as suggested by @Leandro Ferreira