Cannot change url by method `withUrl()`, when events are created by generator
Opened this issue · 3 comments
Hello,
In my app I have calendar_events table with all calendar events morphed to many other models.
My generator look like this:
class CrmCalendarEventGenerator extends CustomEventGenerator
{
protected function modelQuery(EloquentBuilder $queryBuilder, Carbon $startOfCalendar, Carbon $endOfCalendar): EloquentBuilder
{
return $queryBuilder->with(['eventable'])->where('start', '>=', $startOfCalendar)
->where(function ($q) use ($endOfCalendar) {
$q->where('end', '<=', $endOfCalendar)
->orWhereNull('end');
})->where('type', 'recall');
}
/**
* @return array<Event>
*/
protected function resourceToEvents(NovaResource $resource, Carbon $startOfCalendar, Carbon $endOfCalendar): array
{
$model = $resource->model();
assert($model instanceof CalendarEvent);
assert($model->eventable instanceof HasCalendarEvents);
$event = new Event($model->eventable->getCalendarTitle(), $model->start);
$event->notes($model->description);
$event->addStyle($model->style ?? 'default');
// TODO: not working
$event->withUrl('/resources/' . Str::plural($model->eventable_type) . '/' . $model->eventable_id);
return [$event];
}
}
And like you see, there is no option to change url by method withUrl()
, when events are created by generator. Calendar Event always redirects to CalendarEvent model, instead of my customized URL.
Mhm, I see what you mean.
As you can see in the loadAllEvents
method of the AbstractCalendarDataProvider
of this package, the event url is being overwritten after the event generator has generated the event, so your custom URL is being overwritten.
I understand that this is suboptimal and I agree that what you expect to work, should work like that, but currently it doesn't.
A fix for now, would be to implement the urlForResource
method in your CalendarDataProvider, and move your URL calculation logic to there. I think it will then work as you want.
Let me know! :)
Hi,
yes, urlForResource() works great:
protected function urlForResource(NovaResource $resource): string
{
$model = $resource->model();
assert($model instanceof CalendarEvent);
return '/resources/' . Str::plural($model->eventable_type) . '/' . $model->eventable_id;
}
but I think it is little unintuitive, when all other data is set in resourceToEvents() method.
nova-calendar/src/DataProvider/AbstractCalendarDataProvider.php
Lines 293 to 299 in efcafb0
Can we move this logic to Event, and use urlForResource as default url until user set own url?
Hi, yes, I agree, this needs a little refactoring. I'll leave this issue open until I get around to releasing the improvement so others can find the work-around easily.