1
votes

I am uploading an image (Media entity of Sonata Media Bundle) and mapping it to a user (Vendor entity of Sonata User Bundle). The image gets uploaded, the entry in the media table gets created, the key to media entry gets added to user table. No errors triggered. But when I open the form to edit the user, I do not see image preview neither filename near the file input button. As if association file-user is not recognized by the form. Here is my code:

/app/config/config.yml:

sonata_media:
  contexts:
    vendor:
        providers:
            - sonata.media.provider.image
            - sonata.media.provider.file
        formats:
            small_square: { width: 104, height: 104, quality: 70}
            small_portrait: { height: 119, quality: 90}
            small: { width: 100 , quality: 70}
            big:   { width: 1000 , quality: 70}

/src/Application/Sonata/UserBundle/Admin/VendorAdmin.php :

protected function configureFormFields(FormMapper $formMapper)
{
     ...
     $formMapper
          ->with('Gallery', array('class' => 'gallery'))
            ->add('userRatingFile', 'sonata_media_type', array('label' => 'Images', 'required' => false,
                'cascade_validation' => true,
                'context' => 'vendor',
                'provider' => 'sonata.media.provider.image'
            ))
            ->end();
      ...
}

private function doPreLogic($vendor)
{
    ...
    $vendor->setUserRatingFile($vendor->getUserRatingFile());
}

/src/Application/Sonata/UserBundle/Resources/config/doctrine/UserType.Vendor.orm.xml :

<one-to-one field="userRatingFile" target-entity="Application\Sonata\MediaBundle\Entity\Media">
    <cascade>
        <cascade-all/>
    </cascade>
    <join-column name="userRatingFile_id" referenced-column-name="id" />
</one-to-one>

What is be missing? How can I add the preview of the uploaded file?

1

1 Answers

1
votes

You must configure twig:

# Twig Configuration
twig:
    debug:            "%kernel.debug%"
    strict_variables: "%kernel.debug%"
        form:
            resources:
                #...
                - SonataMediaBundle:Form:media_widgets.html.twig