简体   繁体   中英

Symfony2.6 & Sonata Media Bundle &SonataUserBundle: How to get image preview in edit form

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?

You must configure twig:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM