nova-detached-actions icon indicating copy to clipboard operation
nova-detached-actions copied to clipboard

How to get resourceId from detached action from details?

Open 4n70w4 opened this issue 3 years ago • 5 comments

Hi! I define action:

            (new ScriptCreate() )->iconClasses('mr-3 -ml-2')
                ->confirmButtonText(__('Create') )
                ->showOnDetailToolbar()

How to get resource Id in ScriptCreate::handle?

image

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

4n70w4 avatar Nov 25 '20 17:11 4n70w4

But if I run action via action select box - resource id passed!

image

4n70w4 avatar Nov 25 '20 17:11 4n70w4

@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
}

0xfrej avatar Dec 09 '20 18:12 0xfrej

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,
    }

foxx9 avatar Dec 10 '20 14:12 foxx9

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 avatar Jan 27 '22 20:01 skar-helper

skar-helper thanks, bro

dmitryvasilevskiy avatar Feb 16 '22 18:02 dmitryvasilevskiy