PHP code example of 2lenet / attachment-bundle

1. Go to this page and download the library: Download 2lenet/attachment-bundle 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/ */

    

2lenet / attachment-bundle example snippets



$this->attachmentManager->load(MyEntityClass::class, $ids);



namespace App\EventListener;

use App\Entity\Rapport;
use Lle\AttachmentBundle\Service\AttachmentManager;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use EasyCorp\Bundle\EasyAdminBundle\Event\EasyAdminEvents;
use Symfony\Component\EventDispatcher\GenericEvent;

class RapportSubscriber implements EventSubscriberInterface
{
    private $attachmentManager;

    public function __construct(AttachmentManager $attachmentManager)
    {
        $this->attachmentManager = $attachmentManager;
    }


    public static function getSubscribedEvents()
    {
        return [
            EasyAdminEvents::POST_LIST => 'onPostList',
        ];
    }


    public function onPostList(GenericEvent $event)
    {
        if($event->getArgument('entity')['class'] === Rapport::class){
            $ids = [];
            foreach($event->getSubject()->getCurrentPageResults() as $item){
                $ids[] = $item->getId();
            }
            $this->attachmentManager->load(Rapport::class, $ids);
        }
    }



}


use Lle\AttachmentBundle\UploadedFileAttachmentInterface;
use Lle\AttachmentBundle\UploadedFileAttachmentTrait;

$builder->add('uploadedFilesAttachment', FileType::class, ['multiple'=> true]);
// or (and) $builder->add('uploadedFileAttachment', FileType::class, ['multiple'=> false]);
$builder->add('uploadedFilesAttachmentField'); //optional (default is null)

$this->attachmentManager->addFileByEntity($entity);




namespace App\EventListener;


use Doctrine\ORM\EntityManagerInterface;
use EasyCorp\Bundle\EasyAdminBundle\Event\EasyAdminEvents;
use Lle\AttachmentBundle\Service\AttachmentManager;
use Lle\AttachmentBundle\UploadedFileAttachmentInterface;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\EventDispatcher\GenericEvent;

class AttachmentSubscriber implements EventSubscriberInterface
{

    private $attachmentManager;

    public function __construct(AttachmentManager $attachmentManager){
        $this->attachmentManager = $attachmentManager;
    }

    public function addFile(GenericEvent $event){
        if($event->getSubject() instanceof UploadedFileAttachmentInterface){
            $this->attachmentManager->addFileByEntity($event->getSubject());
        }
    }


    public static function getSubscribedEvents()
    {
        return [
            EasyAdminEvents::POST_PERSIST => 'addFile',
            EasyAdminEvents::POST_UPDATE => 'addFile'
        ];
    }
}


public function getUploadedFilesAtachmentField(): ?string{
    return 'uploaded';
}