I am using SonataAdminBundle (with Doctrine2 ORM) and I have successfully added a file upload feature to my Picture model.
I would like, on the Show and Edit pages, to display a simple <img src="{{ picture.url }} alt="{{ picture.title }} />
tag just above the relevant form field (provided that the Picture being edited is not new, of course), so that the user may see the current photo, and decide whether to change it or not.
After hours of research, I've been unable to figure out how to do it. I suppose I need to override some template, but I'm a bit lost... Can somebody give me a hint?
Thank you!
Here is the relevant section of my PictureAdmin class.
protected function configureFormFields(FormMapper $formMapper)
{
$formMapper
->add('category', NULL, ['label' => 'Catégorie'])
->add('title', NULL, ['label' => 'Titre'])
->add('file', 'file', ['required' => false, 'label' => 'Fichier']) // Add picture near this field
->add('creation_date', NULL, ['label' => 'Date d\'ajout'])
->add('visible', NULL, ['required' => false, 'label' => 'Visible'])
->add('position', NULL, ['label' => 'Position']);
}
protected function configureShowFields(ShowMapper $showMapper)
{
$showMapper
->add('id', NULL, ['label' => 'ID'])
->add('category', NULL, ['label' => 'Catégorie'])
->add('title', NULL, ['label' => 'Titre'])
->add('slug', NULL, ['label' => 'Titre (URL)'])
->add('creation_date', NULL, ['label' => 'Date d\'ajout'])
->add('visible', NULL, ['label' => 'Visible'])
->add('position', NULL, ['label' => 'Position']);
// Add picture somewhere
}
You can simple do by this way
There is an easy way - but you will see the picture below the upload button. SonataAdmin lets put raw HTML into the ‘help’ option for any given form field. You can use this functionality to embed an image tag:
Solution for Symfony3
The answer from @kkochanski is the cleanest way I found so far. Here a version ported to Symfony3. I also fixed some bugs.
Create a new template
image.html.twig
for your new form type (full path:src/AppBundle/Resources/views/Form/image.html.twig
):Register the new form type template in your
config.yml
:Create a new form type and save it as
ImageType.php
(full path:src/AppBundle/Form/Type/ImageType.php
):If you have done this. You can just import the new
ImageType
in your entity admin class:And then, finally use the new form type without any inline-html or boilerplate code in
configureFormFields
:Instead of
$image->getImagePath()
you have to call your own method that returns the url to your image.Screenshots
Creating a new image entity using sonata admin:
Editing a image entity using sonata admin:
You can easily do this on show page by template attribute pass on
$showmapper
and inside your template you get current object so u can call get method and pull image path
To show image in edit mode you have to override
fileType
or you have to create your own customType on top offileType
There is also some bundle which is having this kind of functionality check out this GenemuFormBundle
I have managed to put the image above the field in the edit form. But my solution is a little bit specific, because I use Vich Uploader Bundle to handle uploads, so the generation of the image url was a little bit easier thanks to bundle helpers.
Let's look at my example, a film poster field in a film entity. This is part of my admin class:
mybundle_admin_image
is handled by a custom field type, that is just a child of file type by setting it'sgetParent
method: (don't forget to register your type class as a service)Then I have a template that extends Sonata's default styling, and I have it included in the admin class:
And finally I have a block for my custom image type that extends the basic file type:
This causes that a 200px wide preview of image (if exists) is shown above the upload field, linked to it's full size version opening in new tab. You can customize it as you want, e.g. adding a lightbox plugin.
Teo.sk wrote the method of showing images using VichUploader. I found an option which allow you to show images without this bundle.
First we need to create our form_type. There is tutorial: symfony_tutorial
In main Admin class:
Next part is a code from ImageType class.
And on the end time for image_type twig template.
For me it's working! I'm also using avalanche bundle to resize images.