dcasia / nova-filepond

A Nova field for uploading File, Image and Video using Filepond.
MIT License
49 stars 28 forks source link

Issue with using Filepond field on Actions #39

Closed jaymeh closed 1 year ago

jaymeh commented 1 year ago

As a follow on from #27 trying to add a filepond field onto an action leads to a similar issue as described in #38. It appears that the field value is null where the FilePondWrapper expects an array.

files: [ ...props.field.value ],

CleanShot 2023-10-27 at 14 00 01

Just wondering if anything can be done to allow this to function as expected in an Action modal?

Thanks

jaymeh commented 1 year ago

I could use the following workaround but feels a bit strange that I would have to set the default value for the field to render correctly:

Filepond::make('Assets', 'location')
    ->rules('required')
    ->multiple()
    ->mimesTypes(['image/*'])
    ->default(fn() => []),
milewski commented 1 year ago

I figured out a way to make the frontend part work here: https://github.com/dcasia/nova-filepond/pull/40, however the handle method receives an encrypted payload since the fillAttribute is never called, I didnt find a way to override the action logic to be able to make it seamless, basically this line 173 is the one that does the file storing / filling:

https://github.com/dcasia/nova-filepond/blob/41fa1004de9074a7ebc63909e220e53d55b12161/src/Filepond.php#L167-L173

But you could manually handle it with something like this:

class TestAction extends Action
{
    public function handle(ActionFields $fields, Collection $models)
    {
        $images = collect($fields->get('images'))->map(Data::fromEncrypted(...));

        dd($images);
    }

    public function fields(NovaRequest $request): array
    {
        return [
            Filepond::make('Images', 'images'),
        ];
    }
}
Illuminate\Support\Collection {#1663 // app/Nova/Actions/TestAction.php:29
  #items: array:2 [
    0 => DigitalCreative\Filepond\Data\Data {#1659
      +path: "nova-filepond/temp/pJvCy73kaoyru3yp/13876436_830590130405601_5111768853454055697_n.jpg"
      +filename: "13876436_830590130405601_5111768853454055697_n.jpg"
      +disk: "local"
    }
    1 => DigitalCreative\Filepond\Data\Data {#1654
      +path: "nova-filepond/temp/55XulL3NiNRUkJdL/13876436_830590130405601_5111768853454055697_n.jpg"
      +filename: "13876436_830590130405601_5111768853454055697_n.jpg"
      +disk: "local"
    }
  ]
  #escapeWhenCastingToString: false
}

After the upload, you would need to move it from the "temp" dir to a permanent one...

milewski commented 1 year ago

Let me know if this works, or if there are more issues with this approach so I can merge https://github.com/dcasia/nova-filepond/pull/40

jaymeh commented 1 year ago

Thanks, I just pulled the support actions branch into my codebase and the widget is now rendering as expected.

I am having an issue with the process function though. I think it is somewhere here:

$fields = match (true) {
    !is_null($action) => new FieldCollection($resource
        ->availableActions($request)
        ->firstWhere('uriKey', $action)
        ->fields($request)),
    default => $resource->creationFields($request),
};

$rules = $fields
    ->firstWhere('attribute', $attribute)
    ->getCreationRules($request);

CleanShot 2023-10-27 at 16 04 19

CleanShot 2023-10-27 at 16 04 11

milewski commented 1 year ago

Check the request you should see a new "action" attribute being submitted when you click upload: https://github.com/dcasia/nova-filepond/blob/84deef86e8140290caf66bfca4291132f6c716ee/resources/js/components/FilePondWrapper.vue#L79-L81

jaymeh commented 1 year ago

Yup, I see that I think the issue is my action has no uri.

CleanShot 2023-10-27 at 16 08 42

milewski commented 1 year ago

Fixed, pull latest code of that branch

jaymeh commented 1 year ago

Yep, that did it. I also checked over Create, update and replace functionality and all looks to be working ok still. 😃

milewski commented 1 year ago

Released v1.0.3

jaymeh commented 1 year ago

Lovely, thank you very much!