我使用SonataAdminBundle(与Doctrine2 ORM)和我已经成功将文件上传功能,我的图片模型。
我想,上的显示和编辑的网页,以显示一个简单的<img src="{{ picture.url }} alt="{{ picture.title }} />
标记的正上方的相关表单字段(其中,该图片被编辑是不是新的,当然),从而使用户可以看到当前的照片,并决定是否改变与否。
之后的研究时间,我一直无法弄清楚如何做到这一点。 我想我需要重写一些模板,但我有点失落......有人可以给我一个提示?
谢谢!
这里是我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 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
}
Answer 1:
您可以轻松地通过模板属性做这个节目页面上传递$showmapper
->add('picture', NULL, array(
'template' => 'MyProjectBundle:Project:mytemplate.html.twig'
);
和你的模板里面你当前对象,这样你们可以调用get方法和拉图像路径
<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>
要显示在编辑模式下的图像,你必须覆盖fileType
或你必须创建之上自己customType fileType
还有其为具有这种功能检查出一些这方面的束GenemuFormBundle
Answer 2:
我已成功地把图像领域在上面的编辑形式。 但我的解决方案是一个有点特殊,因为我用VICH上传捆绑处理上传,因此图像的URL的生成是一个有点容易由于捆绑帮手。
让我们来看看我的例子,一个电影的海报场电影的实体。 这是我的管理类的一部分:
//MyCompany/MyBundle/Admin/FilmAdmin.php
class FilmAdmin extends Admin {
protected function configureFormFields(FormMapper $formMapper)
{
$formMapper
->add('title')
....
->add('poster', 'mybundle_admin_image', array(
'required' => false,
))
}
mybundle_admin_image
通过自定义字段类型来处理,这只是一个文件类型的孩子通过设置它getParent
方法:(不要忘记注册您的类型类服务)
//MyCompany/MyBundle/Form/Type/MyBundleAdminImageType.php
public function getParent()
{
return 'file';
}
然后,我有一个扩展索纳塔的默认样式模板,我把它包含在管理类:
//MyCompany/MyBundle/Admin/FilmAdmin.php
public function getFormTheme() {
return array('MyCompanyMyBundle:Form:mycompany_admin_fields.html.twig');
}
最后我有我的自定义图像类型,它扩展了基本的文件类型的模块:
//MyCompany/MyBundle/Resources/views/Form/mycompany_admin_fields.html.twig
{% block mybundle_admin_image_widget %}
{% spaceless %}
{% set subject = form.parent.vars.value %}
{% if subject.id and attribute(subject, name) %}
<a href="{{ asset(vich_uploader_asset(subject, name)) }}" target="_blank">
<img src="{{ asset(vich_uploader_asset(subject, name)) }}" width="200" />
</a><br/>
{% endif %}
{% set type = type|default('file') %}
<input type="{{ type }}" {{ block('widget_attributes') }} {% if value is not empty %}value="{{ value }}" {% endif %}/>
{% endspaceless %}
{% endblock %}
这会导致图像的200像素宽的预览(如果存在)显示上述上传字段,链接到它的全尺寸版本开放在新标签。 您可以自定义它,只要你想,例如添加灯箱插件。
Answer 3:
你可以很容易地做到这一点通过助手编辑页面(FormMapper-> setHelps)或选择“帮助”传递FormMapper
protected function configureFormFields(FormMapper $formMapper) {
$options = array('required' => false);
if (($subject = $this->getSubject()) && $subject->getPhoto()) {
$path = $subject->getPhotoWebPath();
$options['help'] = '<img src="' . $path . '" />';
}
$formMapper
->add('title')
->add('description')
->add('createdAt', null, array('data' => new \DateTime()))
->add('photoFile', 'file', $options)
;
}
Answer 4:
解决方案Symfony3
从@kkochanski答案是,我发现迄今最彻底的方法。 这里的一个版本移植到Symfony3。 我还修复了一些bug。
创建一个新的模板image.html.twig
为新表单类型(完整路径: src/AppBundle/Resources/views/Form/image.html.twig
):
{% block image_widget %}
{% spaceless %}
{% set type = type|default('file') %}
<input type="{{ type }}" {{ block('widget_attributes') }} {% if value is not empty %}value="{{ value }}" {% endif %}/>
{% if image_web_path is not empty %}
<img src="{{ image_web_path }}" alt="image_photo"/>
{% endif %}
{% endspaceless %}
{% endblock %}
注册您的新表单模板类型config.yml
:
twig:
form_themes:
- AppBundle::Form/image.html.twig
创建一个新的表单类型并将其保存为ImageType.php
(完整路径: src/AppBundle/Form/Type/ImageType.php
):
<?php
namespace AppBundle\Form\Type;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\OptionsResolver\OptionsResolver;
use Symfony\Component\Form\FormView;
use Symfony\Component\Form\FormInterface;
use Symfony\Component\Form\FormBuilderInterface;
/**
* Class ImageType
*
* @package AppBundle\Form\Type
*/
class ImageType extends AbstractType
{
/**
* @return string
*/
public function getParent()
{
return 'file';
}
/**
* @return string
*/
public function getName()
{
return 'image';
}
/**
* @param OptionsResolver $resolver
*/
public function configureOptions(OptionsResolver $resolver)
{
$resolver->setDefaults(array(
'image_web_path' => ''
));
}
/**
* @param FormView $view
* @param FormInterface $form
* @param array $options
*/
public function buildView(FormView $view, FormInterface $form, array $options)
{
$view->vars['image_web_path'] = $options['image_web_path'];
}
/**
* @param FormBuilderInterface $builder
* @param array $options
*/
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder
->setAttribute('image_web_path', $options['image_web_path'])
;
}
}
如果你这样做了。 您可以直接导入新ImageType
在实体管理类:
use AppBundle\Form\Type\ImageType
然后,最后使用新的表单类型,而不以任何直列HTML或样板代码configureFormFields
:
$formMapper
->add('imageFile', ImageType::class, ['image_web_path' => $image->getImagePath()])
;
取而代之的$image->getImagePath()
你必须调用自己的方法将URL返回给你的形象。
截图
创建使用奏鸣曲管理新形象的实体:
编辑使用奏鸣曲管理一个形象的实体:
Answer 5:
您可以通过这种方式可以简单做
$image = $this->getSubject();
$imageSmall = '';
if($image){
$container = $this->getConfigurationPool()->getContainer();
$media = $container->get('sonata.media.twig.extension');
$format = 'small';
if($webPath = $image->getImageSmall()){
$imageSmall = '<img src="'.$media->path($image->getImageSmall(), $format).'" class="admin-preview" />';
}
}
$formMapper->add('imageSmall', 'sonata_media_type', array(
'provider' => 'sonata.media.provider.image',
'context' => 'default',
'help' => $imageSmall
));
Answer 6:
Teo.sk写使用VichUploader示出的图像的方法。 我发现了一个选项,让你可以显示图像,而这个包。
首先,我们需要建立我们FORM_TYPE。 有教程: symfony_tutorial
在主要管理类:
namespace Your\Bundle;
//.....//
class ApplicationsAdmin extends Admin {
//...//
public function getFormTheme() {
return array_merge(
parent::getFormTheme(),
array('YourBundle:Form:image_type.html.twig') //your path to form_type template
);
protected function configureFormFields(FormMapper $formMapper)
{
$formMapper->add('file_photo', 'image', array(
'data_class' => 'Symfony\Component\HttpFoundation\File\File',
'label' => 'Photo',
'image_web_path' => $this->getRequest()->getBasePath().'/'.$subject->getWebPathPhoto()// it's a my name of common getWebPath method
))
//....//
;
}
}
下一部分是从类将ImageType一个代码。
namespace Your\Bundle\Form\Type;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\OptionsResolver\OptionsResolverInterface;
use Symfony\Component\OptionsResolver\Options;
use Symfony\Component\Form\FormView;
use Symfony\Component\Form\FormInterface;
use Symfony\Component\Form\FormBuilder;
use Symfony\Component\Form\FormBuilderInterface;
class ImageType extends AbstractType
{
public function getParent()
{
return 'file';
}
public function getName()
{
return 'image';
}
public function setDefaultOptions(OptionsResolverInterface $resolver)
{
$resolver->setDefaults(array(
'image_web_path' => ''
));
}
public function buildView(FormView $view, FormInterface $form, array $options)
{
$view->vars['image_web_path'] = $options['image_web_path'];
}
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder
->setAttribute('image_web_path', $options['image_web_path'])
;
}
}
而在结束时间IMAGE_TYPE树枝模板。
{% block image_widget %}
{% spaceless %}
{% set type = type|default('file') %}
<input type="{{ type }}" {{ block('widget_attributes') }} {% if value is not empty %}value="{{ value }}" {% endif %}/>
<img src="{{ image_web_path }}" alt="image_photo"/>
{% endspaceless %}
{% endblock %}
对我来说,它的工作! 我还使用雪崩束来调整图像。
Answer 7:
有一个简单的方法 - 但你会看到上传按钮下面的图片。 SonataAdmin让我们把原始的HTML到任何给定的表单字段的“帮助”选项。 您可以使用此功能嵌入图像标签:
protected function configureFormFields(FormMapper $formMapper) {
$object = $this->getSubject();
$container = $this->getConfigurationPool()->getContainer();
$fullPath = $container->get('request')->getBasePath().'/'.$object->getWebPath();
$formMapper->add('file', 'file', array('help' => is_file($object->getAbsolutePath() . $object->getPlanPath()) ? '<img src="' . $fullPath . $object->getPlanPath() . '" class="admin-preview" />' : 'Picture is not avialable')
}
文章来源: How to display the current picture above the upload field in SonataAdminBundle?