如何显示SonataAdminBundle中上传字段上方的当前图片? [英] How to display the current picture above the upload field in SonataAdminBundle?

查看:232
本文介绍了如何显示SonataAdminBundle中上传字段上方的当前图片?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 SonataAdminBundle (使用Doctrine2 ORM),并且已经成功地将文件上传功能添加到了我的图片模型中。

就像在 Show Edit 页面中显示简单的< img src ={{picture.url}} alt ={ {picture.title}} /> 标签就在相关的表单字段上方(当然,被编辑的图片当然不是新的),以便用户可以看到当前的照片,是否改变它。



经过数小时的研究,我一直无法弄清楚如何去做。我想我需要重写一些模板,但我有点失落...
有人可以给我一个提示吗?



谢谢!
$ b

以下是我的PictureAdmin类的相关部分。






  protected function configureFormFields(FormMapper $ formMapper)
{
$ formMapper
- > add ('category',NULL,['label'=>'Catégorie'])
- > add('title',NULL,['label'=>'Titre'])
- > add('file','file',['required'=> false,'label'=>'Fichier'])//在此字段附近添加图片
- > add ('''''''','''''''',''''''), >'Visible'])
- > add('position',NULL,['label'=&'Position']);


保护函数configureShowFields(ShowMapper $ showMapper)
{
$ showMapper
- > add('id',NULL,['label '=>'ID'])
- > add('category',NULL,['label'=>'Catégorie'])
- > add('title',NULL ['label'=>'Titre'])
- > add('slug',NULL,['label'=>'Titre(URL)'])
- > add('creation_date',NULL,['label'=>'Date d \'ajout'])
- > add('visible',NULL,['label'=&''Visible' ])
- > add('position',NULL,['label'=&''Position']);
//在某处添加图片
}

解决方案

您可以通过 $ showmapper

$ $ $ $ $ $ $'$'$&'; $'$&';项目:mytemplate.html.twig'
);

对象,所以你可以调用get方法和拉图像路径

   {%block name%} {{admin.trans (field_description.label)}} {%endblock%}< / th> 
< td>
< img src ={{object.getFile}}title ={{object。 getTitle}}/>
< / br>
{%block field%} {{value | nl2br}} {%endblock%}
< / td>

要以编辑模式显示图片,您必须覆盖 fileType 或者您必须在 fileType 之上创建自己的customType

还有一些包含这种功能的包
请看这个 GenemuFormBundle


I am using SonataAdminBundle (with Doctrine2 ORM) and I have successfully added a file upload feature to my Picture model.

I would like, on the Show and Edit pages, to display a simple <img src="{{ picture.url }} alt="{{ picture.title }} /> tag just above the relevant form field (provided that the Picture being edited is not new, of course), so that the user may see the current photo, and decide whether to change it or not.

After hours of research, I've been unable to figure out how to do it. I suppose I need to override some template, but I'm a bit lost... Can somebody give me a hint?

Thank you!

Here is the relevant section of my PictureAdmin class.

protected function configureFormFields(FormMapper $formMapper)
{
    $formMapper
        ->add('category', NULL, ['label' => 'Catégorie'])
        ->add('title', NULL, ['label' => 'Titre'])
        ->add('file', 'file', ['required' => false, 'label' => 'Fichier']) // Add picture near this field
        ->add('creation_date', NULL, ['label' => 'Date d\'ajout'])
        ->add('visible', NULL, ['required' => false, 'label' => 'Visible'])
        ->add('position', NULL, ['label' => 'Position']);
}

protected function configureShowFields(ShowMapper $showMapper)
{
    $showMapper
        ->add('id', NULL, ['label' => 'ID'])
        ->add('category', NULL, ['label' => 'Catégorie'])
        ->add('title', NULL, ['label' => 'Titre'])
        ->add('slug', NULL, ['label' => 'Titre (URL)'])
        ->add('creation_date', NULL, ['label' => 'Date d\'ajout'])
        ->add('visible', NULL, ['label' => 'Visible'])
        ->add('position', NULL, ['label' => 'Position']);
        // Add picture somewhere
}

解决方案

You can easily do this on show page by template attribute pass on $showmapper

->add('picture', NULL, array(
    'template' => 'MyProjectBundle:Project:mytemplate.html.twig'
);

and inside your template you get current object so u can call get method and pull image path

<th>{% block name %}{{ admin.trans(field_description.label) }}{% endblock %}</th>
<td>
    <img src="{{ object.getFile }}" title="{{ object.getTitle }}" />
    </br>
    {% block field %}{{ value|nl2br }}{% endblock %}
</td>

To show image in edit mode you have to override fileType or you have to create your own customType on top of fileType

There is also some bundle which is having this kind of functionality check out this GenemuFormBundle

这篇关于如何显示SonataAdminBundle中上传字段上方的当前图片?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆