6

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.

John_iw2
  • 61
  • 1
  • 3
  • Issue 2: You must create a `preUpdate` method in `ItemPictureAdmin`? Here is an example: [Symfony2, Sonata Admin Bundle and File Uploads](http://blog.code4hire.com/2011/08/symfony2-sonata-admin-bundle-and-file-uploads/) – AndreaS Dec 08 '13 at 19:01
  • For the first issue, it is a bug in sonata not yet fixed, that in embedded admins the `$this->getSubject()` gets always the first object from the relation. There is a temporary solution to this. See this: http://stackoverflow.com/questions/18106502/how-to-get-child-object-in-embedded-admin-class-in-sonata-admin/22815329#22815329 – zeykzso Apr 02 '14 at 14:55

2 Answers2

0

I bumped on the Issue 1 myself. I tried every possible solution that I found, but nothing worked. I tried some of my solutions and one was good.
What I did:

  • I saved in the session the index of the child entity (in your case ItemPictureAdmin) and when I needed an entity, I would take the index from session and get the element with that index from the list in the parent entity.

  • First I save in session index 0 and in ItemPictureAdmin I access his parent list of ItemPictureAdmin with the index 0

  • After this, I incremented the index in the session. After I got all the elements from the list, I deleted the session parameter that saved the index.

This solution is an adaptation of caponica's solution here

Community
  • 1
  • 1
0

I had same problem and i am able to do this through "Custom Form Type Extension" for which documentation is given on the link "http://symfony.com/doc/current/cookbook/form/create_form_type_extension.html" .

It is the perfect solution ..

Ajeet Varma
  • 726
  • 4
  • 20