Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Symfony dustin10/VichUploaderBundle events

I am using dustin10/VichUploaderBundle to upload images.

I am using the Gregwar/ImageBundle to resize the images.

dustin10/VichUploaderBundle has a POST_UPLOAD event. How do I trigger the event. I have read the docs but it doesn't say how to trigger the events.

https://github.com/dustin10/VichUploaderBundle/blob/master/Event/Events.php

The plan is to resize the images with the ImageBundle on Post Upload.

S

like image 297
user742736 Avatar asked Feb 09 '23 19:02

user742736


1 Answers

You can't "trigger" the event, it is already triggered here:

   /**
     * Checks for file to upload.
     *
     * @param object $obj       The object.
     * @param string $fieldName The name of the field containing the upload (has to be mapped).
     */
    public function upload($obj, $fieldName)
    {
        $mapping = $this->getMapping($obj, $fieldName);
        // nothing to upload
        if (!$this->hasUploadedFile($obj, $mapping)) {
            return;
        }
        $this->dispatch(Events::PRE_UPLOAD, new Event($obj, $mapping));
        $this->storage->upload($obj, $mapping);
        $this->injector->injectFile($obj, $mapping);
        $this->dispatch(Events::POST_UPLOAD, new Event($obj, $mapping));
    }

What you can do is handle the event which is what I think you are referring to. You can do that by creating a listener as outlined here. The listener will listen for the POST_UPLOAD event like so:

# app/config/services.yml
services:
    app_bundle.listener.uploaded_file_listener:
        class: AppBundle\EventListener\UploadedFileListener
        tags:
            - { name: kernel.event_listener, event: vich_uploader.post_upload, method: onPostUpload }

Your listener class will typehint for the vich uploader event like the following:

// src/AppBundle/EventListener/AcmeRequestListener.php
namespace AppBundle\EventListener;

use Symfony\Component\HttpKernel\HttpKernel;
use Vich\UploaderBundle\Event\Event;

class UploadedFileListener
{
    public function onPostUpload(Event $event)
    {
        $uploadedFile = $event->getObject();
        // your custom logic here
    }
}
like image 119
jcroll Avatar answered Feb 12 '23 03:02

jcroll