To use file as a resource type you need to add the Files library to your application.
The following command will add the bundle to your composer.json
:
composer require fsi/files
Now, register the bundle in bundles.php
.
IMPORTANT!! make sure that FSi\Component\Files\Integration\Symfony\FilesBundle
is registered
before FSi\Bundle\ResourceRepositoryBundle\FSiResourceRepositoryBundle
. Otherwise you will not be able
to use the file type resource.
// config/bundles.php
<?php
return [
// ... packages required by FilesBundle
FSi\Component\Files\Integration\Symfony\FilesBundle::class => ['all' => true],
// FSiResourceRepositoryBundle must be after FilesBundle
FSi\Bundle\ResourceRepositoryBundle\FSiResourceRepositoryBundle::class => ['all' => true]
];
Now you need to change the extended class of your Resource Entity from
FSi\Bundle\ResourceRepositoryBundle\Model\Resource
to FSi\Bundle\ResourceRepositoryBundle\Model\ResourceWebFile
This is how your Resource class should look now:
<?php
declare(strict_types=1);
namespace FSi\Bundle\DemoBundle\Entity;
use Doctrine\ORM\Mapping\Entity;
use Doctrine\ORM\Mapping\Table;
use FSi\Bundle\ResourceRepositoryBundle\Model\ResourceWebFile;
// You can use whatever drive you want for mapping
#[Entity(repositoryClass: "FSi\Bundle\ResourceRepositoryBundle\Doctrine\ResourceRepository"]
#[Table(name: "fsi_resource")]
class Resource extends ResourceWebFile
{
}
Update your database schema forcefully:
$ php bin/console doctrine:schema:update --force
or via migrations:
php bin/console doctrine:migrations:diff
php bin/console doctrine:migrations:migrate -n
And now you should be able to use the new web_file
resource type in your Resource Map.
See the related documentation on how to use it.
Example:
# config/resource_map.yaml
resources:
type: group
home_page:
type: group
terms_of_service:
type: web_file
form_options:
label: Terms of service
constraints:
FSi\Component\Files\Integration\Symfony\Validator\Constraint\UploadedWebFile:
mimeTypes: ["application/pdf"]
header:
type: web_file
form_options:
label: Header background
image: true
constraints:
FSi\Component\Files\Integration\Symfony\Validator\Constraint\UploadedImage:
maxWidth: 1650
maxHeight: 600
advertisement:
type: web_file
form_options:
label: Advertisement
image: true
removable: true
constraints:
FSi\Component\Files\Integration\Symfony\Validator\Constraint\UploadedImage:
maxWidth: 400
maxHeight: 500