nova-detached-actions
nova-detached-actions copied to clipboard
How to get resourceId from detached action from details?
Hi! I define action:
(new ScriptCreate() )->iconClasses('mr-3 -ml-2')
->confirmButtonText(__('Create') )
->showOnDetailToolbar()
How to get resource Id in ScriptCreate::handle
?
request()->all()
contains:
request()->all() = {array} [10]
resources = "all"
action = "script-create"
pivotAction = "false"
search = null
filters = null
trashed = null
viaResource = null
viaResourceId = null
viaRelationship = null
But if I run action via action select box - resource id passed!
@4n70w4
Not sure about this. But system I was working on was using older Nova (standalone actions which are used by this package are introduced in version 3.10.0 and I had to make my implementation) and in my case it's doable by setting property standalone
on action object to false and use onlyOnDetailToolbar()
. I think it should do the thing.
Also retrieve it like this (this works only when property standalone
is set to false - this package sets it by default to true)
public function handle(ActionFields $fields, ?Collection $resources = null)
{
self::message($resources->first()->id); // prints id to message
}
Hi, same issue for me here,
I was able to get the item in a similar way than @4n70w4 by checking request params, but it doesnt work with this plugin.
@Lai0n I tried your solution but the resources list contains all items, not only the one currently displayed in to detail view
EDIT :
When manually changing the source code of CustomDetailToolbar.vue, adding data to override the "selectedResources" to the intended ID instead of the default "all", it works : the ?Collection $resources you mentioned then only contains the resource we want .
The way I get the resourceID is a bit hacky, but I can be a lead to a more elegant solution
CustomDetailToolbar.vue
data: function () {
var resourceId = null;
var currentComponent = this;
do {
resourceId = currentComponent.$props.resourceId;
currentComponent = currentComponent.$parent;
} while (currentComponent && !resourceId);
return {
visibleActionsDefault: 3,
actionsList: [],
selectedResources: resourceId || 'all',
confirmActionModalOpened: false,
invisibleActionsOpen: false,
}
Potential temporary workaround is to use Request::server('HTTP_REFERER')
and extract the ID from the URL.
If using spatie/url then this works:
$url = Url::fromString(Request::server('HTTP_REFERER'));
$id = Str::of($url->getPath())->explode('/')->last();
$resource = $resources->firstWhere('id', '=', $id);
$this->log($resource);
skar-helper thanks, bro