This example will help you create custom provider for Sonata Media that will allowe you to upload svg.
Adapted solution from here.
sonata_media: | |
db_driver: doctrine_orm | |
default_context: default | |
contexts: | |
default: | |
providers: | |
- admin.provider.image | |
#... |
<?php | |
namespace YourBundle\Provider; | |
use Sonata\MediaBundle\Provider\FileProvider; | |
class ImageProvider extends FileProvider | |
{ | |
} |
services: | |
admin.provider.image: | |
class: YourBundle\Provider\ImageProvider | |
tags: | |
- { name: sonata.media.provider } | |
arguments: | |
- "admin.provider.image" | |
- "@sonata.media.filesystem.local" | |
- "@sonata.media.cdn.server" | |
- "@sonata.media.generator.default" | |
- "@sonata.media.thumbnail.format" | |
- ['gif', 'svg', 'jpg', 'jpeg', 'png'] | |
- ['image/svg+xml', 'image/gif', 'image/jpeg', 'image/pjpeg', 'image/png'] | |
calls: | |
- [ setTemplates, [{ helper_thumbnail : 'YourBundle:Provider:thumbnail.html.twig', helper_view : 'YourBundle:Provider:view_image.html.twig' }]] |
<img src="/uploads/media/{{ options.file }}" {% for name, value in options %}{{name}}="{{value}}" {% endfor %} /> |
<img src="/uploads/media/{{ options.file }}" {% for name, value in options %}{{ name }}="{{ value }}" {% endfor %} /> |
<?php | |
namespace YourBundle\Admin; | |
use Sonata\AdminBundle\Admin\AbstractAdmin; | |
use Sonata\AdminBundle\Datagrid\DatagridMapper; | |
use Sonata\AdminBundle\Datagrid\ListMapper; | |
use Sonata\AdminBundle\Form\FormMapper; | |
class YourAdmin extends AbstractAdmin | |
{ | |
/** | |
* @param FormMapper $formMapper | |
*/ | |
protected function configureFormFields(FormMapper $formMapper) | |
{ | |
$formMapper | |
->add('icon', 'sonata_type_model_list', ['required' => false], [ | |
'link_parameters' => ['context' => 'default', 'provider' => 'admin.provider.image']]); | |
} | |
} |
This will add svg extension to files instead of empty. Also i've added ability to show referenced svg in admin panel.