2lenet / attachment-bundle
附件
1.2.1
2021-03-04 14:38 UTC
Requires
- php: >=7.2
- antishov/doctrine-extensions-bundle: ^1.4
- symfony/mime: ^4.3 || ^5.0
This package is auto-updated.
Last update: 2024-09-04 22:09:22 UTC
README
安装
composer req 2lenet/attachment-bundle
将以下内容添加到您的 config/routes.yaml 文件中(目前没有 flex 食谱)。
lle_attachment: resource: "@LleAttachmentBundle/Resources/config/routing/routes.yaml"
在模板中嵌入控制器
{{ render(controller('lle.attachment', {'item': item})) }}
或者使用字段,如果您只想显示一组附件
{{ render(controller('lle.attachment', {'item': item, options: {'field': 'pdf'}})) }}
这样您就可以使用 "lle.attachment" 动作与 EasyAdminPlusBundle 一起使用
show: title: title.examen.show fields: - { type: 'tab', id: 'documents', label: 'tab.documents', action: 'lle.attachment'}
或者使用字段,如果您只想显示一组附件
show: title: title.examen.show fields: - { type: 'tab', id: 'documents', label: 'tab.documents', action: 'lle.attachment', options: {'field': 'pdf'}}
信息:lle.attachment 是 lle.attachment.show.action 的别名
易于使用
渲染上传小部件
{{ render_attachment(entity) }}
渲染下载列表(ul)
{{ list_attachment(entity) }}
您可以在同一模板中使用多个附件
{{ render_attachment(item) }} {% for examen in item.examens %} <h3>{{ examen }}</h3> {{ render_attachment(examen) }} {% endfor %}
配置
使用默认值
framework: translator: fallbacks: ['fr'] lle_attachment: directory: data/attachment #directory of files is registred show_list: false #show or not show the list of files (table) with uploader widget need_confirm_remove: true #modal for confirm delete or no
感谢使用
预先加载实体列表
如果您使用 20 次调用 {{ list_attachment(entity) }} 列出 20 个实体,您希望只进行 1 次请求,而不是 20 次
调用
<?php $this->attachmentManager->load(MyEntityClass::class, $ids);
以下是与 EasyAdmin 的示例
<?php 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); } } }
在表单中添加上传的文件
- 在实体中添加 UploadedFileAttachmentInterface 和 UploadedFileAttachmentTrait
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)
- 现在您需要在持久化后调用 addFileByEntity(实体标识必须创建)
$this->attachmentManager->addFileByEntity($entity);
- 这里是一个简单的 EasyAdmin 示例
<?php 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'; }
- 对于验证,请使用 Symfony 验证器注解或表单中的验证器(建议:如果您使用验证器注解,请创建自己的 Trait)