简体   繁体   English

Symfony2.6和Sonata Media Bundle&SonataUserBundle:如何以编辑形式获取图像预览

[英]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). 我正在上传图像(Sonata Media Bundle的Media实体)并将其映射到用户(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: /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 : /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 : /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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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