PHP code example of pvlkns / symfony-dropzone

1. Go to this page and download the library: Download pvlkns/symfony-dropzone library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

pvlkns / symfony-dropzone example snippets


public function buildForm(\Symfony\Component\Form\FormBuilderInterface $builder, array $options)
{ 

    // userFiles is OneToMany
    $builder->add('userFiles', DropzoneType::class, [
        'class' => File::class,
        'maxFiles' => 6,
        'uploadHandler'=>'uploadHandler',  // route name
        'removeHandler'=> 'removeHandler'// route name
   ]),
   ->add('arrayIdMedia', TextType::class, ['mapped' => false]); //hide this type after tests
}

    /**
     * @Route("/uploadhandler", name="uploadHandler")
     */
    public function uploadhandler(Request $request, ImageUploader $uploader) {

        $dateNow = new \DateTime('now');
        $doc = $uploader->upload($request->files->get('file'));
        $file = new Attachment();
        $file->setCreatedAt($dateNow);
        $file->setUpdatedAt($dateNow);
        $file->setImageFile($doc);

        $this->entityManager->persist($file);
        $this->entityManager->flush();
        return new JsonResponse([ "id" => $file->getId() ]);
    }


    /**
     * @Route("/removeHandler/{id}", name="removeHandler")
     */
    public function removeHandler(Request $request, $id) {

        $file = $this->repoAttachment->findOneBy(['id' => $id]);
        $idFile = $file->getId();

        $this->entityManager->remove($file);
        $this->entityManager->flush();

        return new JsonResponse([ "id" => $idFile ]);
    }


    public function addClassifield(Request $request, EntityManagerInterface $entityManager): Response
    {
        $item = new Item();
        $form = $this->createForm(AddPropertyType::class, $item);

        $form->handleRequest($request);

        if ($form->isSubmitted() && $form->isValid()) {
            $item = $form->getData();

            $arrayItemsMedia = explode(',',$form->get("arrayIdMedia")->getData());
            foreach ($arrayItemsMedia as $key => $value) {
                $mediaObject = $this->repoAttachment->findOneBy(['id' => intval($value)]);
                $item->addAttachment($mediaObject);
            }

            $entityManager->persist($item);
            $entityManager->flush();
        }

        return $this->render('dashboard/dashboard-add-property.html.twig', [
            'form' => $form->createView(),
        ]);

    }