Skip to content
This repository has been archived by the owner on Apr 20, 2023. It is now read-only.

How to get resourceId from detached action from details? #32

Open
4n70w4 opened this issue Nov 25, 2020 · 5 comments
Open

How to get resourceId from detached action from details? #32

4n70w4 opened this issue Nov 25, 2020 · 5 comments

Comments

@4n70w4
Copy link

4n70w4 commented Nov 25, 2020

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 4n70w4 changed the title How to get resourceId from detached action from deails? How to get resourceId from detached action from details? Nov 25, 2020
@4n70w4
Copy link
Author

4n70w4 commented Nov 25, 2020

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

image

@0xfrej
Copy link

0xfrej commented Dec 9, 2020

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

@foxx9
Copy link

foxx9 commented Dec 10, 2020

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

@skar-helper
Copy link

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);

@dmitryvasilevskiy
Copy link

skar-helper
thanks, bro

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants