1

我不知道这是交响曲还是奏鸣曲管理包问题。

我有我的主 MultimediaAdmin 类,它有多个嵌入式 FileAdmin 条目。

class MultimediaAdmin extends Admin
{

protected function configureFormFields(FormMapper $formMapper)
{
    $formMapper

        ->with('Files')
            ->add('files','sonata_type_collection',
            array('label' => 'Multimedia Files',
                'btn_add'       => 'Add File',
                'by_reference' => 'false',
                'type_options' => array('delete' => false)
                ), array(
                    'edit' => 'inline',
                    'template' => 'MyMultimediaBundle:Multimedia:horizontal.fields.html.twig'
                )
            )
        ->end()
        ->with('Tags')
            ->add('tags')
        ->end()
    ;
}

}

我有一个自定义模板样式化嵌入式 FileAdmin 表单的外观,其中几个字段中有一个在编辑时显示上传媒体的预览。

/* horizontal.fields.html.twig */

<fieldset>
                    <div class="sonata-ba-collapsed-fields">
                    {% for nested_group_field_name, nested_group_field in form.children %}
                        {% for field_name, nested_field in nested_group_field.children %}
                        <div class="control-group">
                            <label class="control-label" for="nested_field.vars['sonata_admin'].admin.trans(nested_field.vars.label" {{ nested_field.vars['required']  ? 'class="required"' : '' }}>{{ nested_field.vars['sonata_admin'].admin.trans(nested_field.vars.label) }}</label>
                            <div class="controls">
                            {% if sonata_admin.field_description.associationadmin.formfielddescriptions[field_name] is defined %}                               

                                {{ form_widget(nested_field, {
                                    'inline': 'natural',
                                    'edit'  : 'inline'
                                }) }}
                                {% set dummy = nested_group_field.setrendered %}
                            {% else %}
                                {{ form_widget(nested_field) }}
                            {% endif %}
                                {% if sonata_admin.field_description.help %}
                <span class="help-block sonata-ba-field-help">{{ sonata_admin.admin.trans(sonata_admin.field_description.help, {}, sonata_admin.field_description.translationDomain)|raw }}</span>
            {% endif %}
                            </div>

                            </div>
                        {% endfor %}
                    {% endfor %}
                    </div>
                </fieldset>

这是 FileAdmin,它在编辑时添加了图像预览以显示缩略图

class FileAdmin extends Admin
{

protected function configureFormFields(FormMapper $formMapper)
{

        $media = $this->getSubject();


        // use $fileFieldOptions so we can add other options to the field
        $fileFieldOptions = array('required' => false,'label' => 'Files', 'attr' => array("multiple" => "multiple"), 'by_reference' => false);
        if ($media && ($webPath = $media->getWebPath())) {
            $fileFieldOptions['help'] = '<img src="'.$webPath.'" class="admin-preview" />';
        }
    $formMapper
        ->add('title','text',array('label'=>'Title'))
        ->add('abstract','textarea',array('label'=>'Abstract'))
        ->add('language')
        ->add('format')
        ->add('file', 'file', $fileFieldOptions)
        ->add('quality')
    ;
}
}

自定义表单在样式方面工作完美,但我唯一的问题是,在多媒体表单中编辑嵌入文件时它不显示图像预览。当我直接去编辑文件时,而不是在 MultimediaAdmin 下,图像预览效果很好。我哪里会出错?

4

0 回答 0