Download the PHP package mezcalito/sylius-file-upload-plugin without Composer
On this page you can find all versions of the php package mezcalito/sylius-file-upload-plugin. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download mezcalito/sylius-file-upload-plugin
More information about mezcalito/sylius-file-upload-plugin
Files in mezcalito/sylius-file-upload-plugin
Package sylius-file-upload-plugin
Short Description Mezcalito file upload plugin for Sylius.
License MIT
Informations about the package sylius-file-upload-plugin
Mezcalito Sylius File Upload Plugin
This plugin works almost the same as the standard Sylius images uploading (see How to add images to an entity? from the Sylius Documentation), except that it accepts any kind of file types.
Initially this was proposed as a pull request on Sylius core, but it was declined as it was not backwards compatible with Sylius 1.x (Here is the link to the initial PR), so here it is as a Sylius plugin.
Installation
First require this package with composer
Then add the bundle to your config/bundles.php
file
In your config/packages/_sylius.yaml
file, add the following
This file defines the gaufrette
filesystem
andadapter
used by the plugin, which you can override.
Usage
This section is an adaptation of the official Sylius documentation about How to add images to an entity ?.
Extending entities with an
files
field is quite a popular use case. In this cookbook we will present how to add files to the Shipping Method entity.
you can see this sample in test/Application.
1. Create the book class with the FilesAwareInterface
Here we used the
FilesAwareTrait
which is provided for convenience.
2. Register your book as a resource's model class
We will use the attributes:
3. Create the BookFile class
In the App\Entity
namespace place the BookFile
class which should look like this:
4. Add the mapping and resource attributes for the BookFile
Your new entity will be saved in the database, therefore it needs a mapping file, where you will set the Book
as the owner
of the BookFile
.
5. Create the book form type
It needs to have the files field as a LiveCollectionType.
Declare the form
In case you need only a single file upload, this can be done in 2 very easy steps.
First, in the code for the form provided above set allow_add
and allow_delete
to false
Second, in the __construct
method of the Book
entity you defined earlier add the following:
6. Create the book file form type
Declare the form
7. Create the BookGrid
create the file template field for the grid you can use bin/console make:grid
and the template for files
8. Create the book form twig component
9. Declare the FilesUploadListener service
In order to handle the file upload you need to attach the FilesUploadListener
to the Book
entity events:
10. Add Book section to menu
11. Validation
Your form so far is working fine, but don't forget about validation.
The easiest way is using validation config files under the App/Resources/config/validation
folder.
This could look like this for an image e.g.:
or for a PDF e.g.:
This defines the validation constraints for each file entity.
Finally, connect the validation of the ShippingMethod
to the validation of each single File Entity
:
12. Migration
Do the database migration