Render a preview image in SonataAdminBundle
For those of you that are using the SonataAdminBundle and SonataMediaBundle to generate a CMS for your website, you might have wondered how to show a preview of your freshly uploaded image. Well, here’s how to do it.
So first, here’s a the configuration of my formMapper in my admin class:
[sourcecode]
protected function configureFormFields(FormMapper $formMapper)
{
$formMapper
->add(‘title’, null, array(‘attr’ => array(‘title’ => ‘contentItem-title’)))
->add(‘description’)
->add(‘deviceClass’)
->add(‘image’, ‘sonata_type_model_list’, array(
‘required’ => false
), array(
‘link_parameters’ => array(
‘context’ => ‘default’,
‘provider’ => ‘sonata.media.provider.image’
)
)
)
[/sourcecode]
Without going into too many details about the Sonata media bundle, the above configuration will show a nice field with buttons for uploading an image. But instead of showing the uploaded image, it shows only the image name. Now, to show the image do the following:
1. Copy the file Sonata/AdminBundle/Resources/views/Helper/short-object-description.html.twig to a place in your own bundle. Preferably somewhere in the Resources/views directory.
2. Next add some statements so that if an image is provided you will show it:
[sourcecode]
<span>
{% if object.providerName is defined and object.providerName == ‘sonata.media.provider.image’ %}
<a href="{{ url }}" target="new">{% media object, ‘reference’ %}</a>
{% else %}
<a href="{{ url }}" target="new">{{ description }}</a>
{% endif %}
</span>
[/sourcecode]
3. Last thing to do is to tell Sonata that you want to override the default template for your own. You do this in the config.yml of your project by adding the following line:
[sourcecode]
sonata_admin:
templates:
short_object_description: YourBundle:Admin:short-object-description.html.twig
[/sourcecode]
This should show you the original image in the box next to the buttons. Naturally you can also show a thumbnail by changing the reference parameter in the template.
thank you very much, this is exactly what i try to do !!!
I’m very glad to find this post. My question is not directly related to the article. I succeeded to install and use SonataAdmin, but failed usign SonataMedia. The documentation is so thick and as I’m new to Symfony2 it’s challenging for me to get throught. Any idea for your part would be highly appreciated. Thanks
Nice and clean !
Hello,
Thanks for your tuto but it doesn’t work for me..
Nothing change. It stills display the name_file.jpg.
Plus, I would like to display a thumbnail of the actual picture uploaded.
SonataAdminBundle; SonataMediaBundle, and I use ” “sonata_media_type” ” for my upload.. :/
May I’m wrong with my view ?
Thanks
Perfect, thanks a lot !!
I don’t know if it’s just me or if perhaps everyone
else encountering issues with your blog. It looks like some of the written
text on your posts are running off the screen.
Can somebody else please provide feedback and
let me know if this is happening to them as well?
This might be a problem with my internet browser because I’ve had this happen before.
Thanks
No matter if some one searches for his vital
thing, so he/she needs to be available that in detail, so that thing is maintained over here.
Feel free to surf to my blog post … aloe vera (Adam)
It’s hard to find your posts in google. I found it on 15 spot, you should build quality backlinks , it will
help you to get more visitors. I know how
to help you, just type in google – k2 seo tips and tricks
I read a lot of interesting posts here. Probably you spend a lot of time
writing, i know how to save you a lot of work, there is an online tool that creates high
quality, SEO friendly articles in minutes, just search
in google – laranitas free content source
Thanks,
Always usefull 2 years later! 😉
We niet zelf plaatst de waarschijnlijkheid , omdat het eind van de dag , is het
alles wat we hebben .