In Sonata Admin, I want to use upload and preview picture file in sonata_type_collection.
In my Admin/ItemAdmin.php :
class ItemAdmin extends Admin
{
protected function configureFormFields(FormMapper $formMapper)
{
$formMapper
->add('title')
->with('Photos')
->add('pictures', 'sonata_type_collection', array(
'label' => 'Pictures',
'by_reference' => false,
'required' => false,
), array(
'edit' => 'inline',
'inline' => 'table',
'sortable' => 'id'
))
->end()
;
}
In my Admin/ItemPictureAdmin.php :
class ItemPictureAdmin extends Admin {
protected function configureFormFields(FormMapper $formMapper) {
$obj = $this->getSubject();
$formBuilder = $formMapper->getFormBuilder();
if(is_object($obj) && $obj->getPicture())
{
$formMapper
->add('picturePreview', 'image_preview', array(
'required' => false,
'property_path' => false,
'upload_dir' => '/uploads/picture/',
'file_name' => $obj->getPicture()
));
}
$formMapper->add('pictureFile', 'file', array('label' => 'Pic'));
}
The type "image_preview" is a new type I created with a simple template twig to show the picture.
Preview : https://i.stack.imgur.com/AC0ih.png
Issue 1 : I have two pictures recorded in database with two differents picture files but in the list, it displays the first only.
Issue 2 : It's not possible to update a picture... I have to delete the record and upload a new file again.