Custom Template in Sonata Admin

2019-05-26 19:23发布

I am using Sonata Admin to manage CRUD tasks in my application. In one Admin called Multimedia, which has one-to-many relations with Files and Weblinks, both of which are embedded in the Multimedia form. I have a custom template that renders the fields horizontally and with titles. My question is, do I have to specify two different templates for Files and Weblinks because using a single file has failed, Files renders the embed form how I want it but weblink ignores directive.

Here's the Admin Code

class MultimediaAdmin extends Admin
{
// Fields to be shown on create/edit forms
protected function configureFormFields(FormMapper $formMapper)
{
    $formMapper
        ->with('General')
            ->add('name')
            ->add('publish_date')
            ->add('keywords')
            ->add('copyright')
        ->end()
        ->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()
        ->with('Weblinks')
            ->add('weblinks','sonata_type_collection',
            array('label' => 'External Videos',
                'btn_add'       => 'Add Video',
                'by_reference' => 'false',
                'type_options' => array('delete' => false)
                ), array(
                    'edit' => 'inline',
                    'template' => 'MyMultimediaBundle:Multimedia:horizontal.fields.html.twig'
                )
            )
        ->end()
    ;
}
// Fields to be shown on lists
protected function configureListFields(ListMapper $listMapper)
{
    $listMapper
        ->addIdentifier('name')
        ->add('publish_date')
        ->add('keywords')
        ->add('copyright')
        ->add('_action','actions',array('actions'=>(array('edit'=>array(),'view'=>array(),'delete'=>array()))))
    ;
}
// Fields to be shown on filter forms
protected function configureDatagridFilters(DatagridMapper $datagridMapper)
{
    $datagridMapper
        ->add('name')
        ->add('publish_date')
        ->add('keywords')
        ->add('copyright')
    ;
}
public function prePersist($multimedia)
{
    $this->preUpdate($multimedia);
}

public function preUpdate($multimedia)
{
    $multimedia->setFiles($multimedia->getFiles());
}
public function getFormTheme()
{
return array_merge(
    parent::getFormTheme(),
    array('MyMultimediaBundle:Multimedia:horizontal.fields.html.twig')
);
}

1条回答
beautiful°
2楼-- · 2019-05-26 19:46

I figured it out, pretty basic error in the template regarding block naming. Hope this helps someone in future

查看更多
登录 后发表回答