Sylius: Resource image upload

Created on 17 Jun 2016  Â·  10Comments  Â·  Source: Sylius/Sylius

Hi,
I created a new custom resource entity Book implements ResourceInterface and the corresponding backend menu + forms to manage the instances in the backend. Everything works perfectly and I can create/update/delete my instances in the backend.

Now I want to add an Image to this Book entity class, like this _(as per http://symfony.com/doc/2.8/cookbook/controller/upload_file.html)_:

    /**
     * @ORM\Column(type="string")
     * @Assert\NotBlank(message="Please, upload the book cover as a PNG/JPEG/GIF file.")
     * @Assert\File(mimeTypes={ "image/png", "image/jpeg", "image/gif" })
     */
    private $image;

The form shows an upload button correctly, but when I submit the form, the file doesn't get uploaded and I get an error for the form widget stating that the file does not exist.

Do I have to configure something else for this to work ?

Thanks a lot

Help Wanted

Most helpful comment

Have you add enctype="multipart/form-data" to your form tag ?

All 10 comments

Anyone ? I haven't found anything in the fixtures and the doc is outdated it seems, so I'm blocked here ;(

Hi @tchapi! Are you handling the upload itself somehow? You could use our ImageUploader, but nothing will be done automatically here. Have a look at ImageUploadListener in core. This is what triggers the upload for taxons and variants.

Thanks,
I did mimick ImageUploaderListener + service declaration, but somehow it never gets used.

my_bundle.listener.image_upload:
        class: My\Bundle\EventListener\ImageUploadListener
        arguments: [@sylius.image_uploader]
        tags:
            - { name: kernel.event_listener, event: my_bundle.my_entity.pre_create, method: uploadImage }
            - { name: kernel.event_listener, event: my_bundle.my_entity.pre_update, method: uploadImage }

My entity implements _ResourceInterface_.

How do I "link" this service to the events emitted by the resource ? That' s the part I don't understand — I don't want to create a controller especially for that upload logic, or must I ? I had in mind that the uploadListener could take care of that by himself in registering for pre_create and pre_update, no ?

Thanks

Hey @tchapi! You're correct with the listeners, here the docs about ResourceBundle emitted events: http://docs.sylius.org/en/latest/bundles/general/events.html

Also, could you post your sylius_resource: node from resource declaration config?

Here it goes for the sylius_resource :

sylius_resource:
    resources:
        my_bundle.my_entity:
            classes:
                model: My\Bundle\Entity\MyEntity
            templates: MyBundle:Backend/MyEntity

and thanks @okwinza for the resource docs, but I have a feeling that my_entity.pre_update is never emitted. It should be the case, though, since it's a Resource, and so the controller is basically an instance of Sylius\Bundle\ResourceBundle\Controller\ResourceController which emits events.

If I look in the Profiler, my listener is listed for the correct event, but in the section "Not Called" ..

I think I'm missing a little configuration / piece of code somewhere, but I really can't find where.

Your configs look good so it seems the event is never fired somehow. Are
you sure the entity is correctly validated and saved/created?

On Tuesday, June 21, 2016, tchap [email protected] wrote:

and thanks @okwinza https://github.com/okwinza for the resource docs,
but I have a feeling that my_entity.pre_update is never emitted. It
should be the case, though, since it's a _Resource_, and so the
controller is basically an instance of
SyliusBundleResourceBundleControllerResourceController which emits
events.

If I look in the Profiler, my listener is listed for the correct event,
but in the section "Not Called" ..

I think I'm missing a little configuration / piece of code somewhere, but
I really can't find where.

—
You are receiving this because you were mentioned.
Reply to this email directly, view it on GitHub
https://github.com/Sylius/Sylius/issues/5282#issuecomment-227435126, or mute
the thread
https://github.com/notifications/unsubscribe/AAGpfdefnrmRwR2IfhXU10yoXxKKqgYDks5qN-N-gaJpZM4I4NVJ
.

I'm narrowing it down right now. In fact, in ResourceController.php, the event (say, pre_create) is dispatched _after_ the form is deemed valid. But the form is not valid since there is no file yet.

[...]
if ($request->isMethod('POST') && $form->submit($request)->isValid()) {
  $newResource = $form->getData();
  $event = $this->eventDispatcher->dispatchPreEvent(ResourceActions::CREATE, $configuration, $newResource);

[...]

So as a matter of fact I cannot use @Assert\File (as per Symfony 2.8's guidelines) it seems ? What do you think ?

Have you add enctype="multipart/form-data" to your form tag ?

Ok, enctype was definitely part of the culprit, thanks @Niiko !

I managed to make it work afterwards in using a different FileUploader from the Sylius one, following the 2.8 guidelines here that only require one field and does not clutter the Entity definition with all the methods from ImageInterface.

Thank you all !

Was this page helpful?
0 / 5 - 0 ratings