8

因此,在 Sonata Admin 的编辑操作中,我试图根据创建编辑上下文显示不同的表单字段。

也许先有一些背景..

我有一个与 OneToOne 绑定的 Gallery 实体和一个 CoverPhoto 实体。

画廊:

/**
 * @ORM\OneToOne(targetEntity="CoverImage", mappedBy="gallery", cascade={"all"}, orphanRemoval=true)
 **/
private $cover;

封面图片:

/**
 * @ORM\OneToOne(targetEntity="Gallery", inversedBy="cover")
 **/
private $gallery; 

这是对应的GalleryAdmin类:

class GalleriesAdmin extends Admin
{
    protected function configureFormFields(FormMapper $formMapper)
    {
        $formMapper
                -> add('name', null, array('label' => 'Nazwa'))
                -> add('category', 'sonata_type_model', array('label' => 'Kategoria'), array('subject' => $this -> getSubject()))
                -> add('cover', 'sonata_type_admin', array('label' => 'Okładka'))
                -> add('images', 'sonata_type_collection', array('by_reference' => false, 'label' => 'Powiązane zdjęcia'), array(
                    'edit' => 'inline',
                    'sortable' => false,
                    'inline' => 'table',
                ))
            ;
    }

    //other stuff 
}

在这里,我们使用CoverImageAdmin

class CoverImagesAdmin extends Admin
{
    protected function configureFormFields(FormMapper $formMapper)
    {
        $formMapper
                -> add('path', 'thumbnail', array('label' => 'Miniatura'))
                -> add('file', 'file', array('required' => false, 'label' => 'Plik'))
        ;
    }
}

“缩略图”是我的自定义表单字段,它显示缩略图(令人震惊)。但现在我希望这个字段只出现在“编辑”上下文中。

这应该是小菜一碟

$this -> getSubject()

method of Admin class and condition. Unfortunately when I call getSubject() in CoverImagesAdmin class which is used to render nested form it always returns null. The same with getParent();

Calling getForm() results in

Fatal error: Maximum function nesting level of '500' reached, aborting! in /home/flameheart/Projects/KolberPhotography/vendor/symfony/symfony/src/Symfony/Component /OptionsResolver/Options.php on line 350

I've tried to call about every method of Admin and FormMapper just to determine form's context but ended up with nothing.

Do you guys have any idea how to solve this in a clean way ?

4

3 回答 3

19

I managed to do it this way:

protected function configureFormFields(FormMapper $formMapper)
{

    if($this -> getRoot() -> getSubject() -> getCover() && $this -> getRoot() -> getSubject() -> getCover() -> getId() )
    {
        $formMapper -> add('path', 'thumbnail', array('label' => 'Miniatura', 'attr' => array('id' => 'gallery_cover_image'), 'label_attr' => array('id' => 'gallery_cover_label')));
    }

    $formMapper -> add('file', 'file', array('required' => false, 'label' => 'Plik'));
}

Imo, this Sonata thing really needs loads of documentation and refactoring instead of further development.

于 2013-01-06T12:58:11.420 回答
7

It's an old question, I know, but the cleanest way I have found of doing this is:

$this->id($this->getSubject())

If it returns true it is an edit form, if it is false, it is a create form.

于 2014-02-13T18:21:35.787 回答
0

You can get the subject inside of an admin with $this->subjectExists() and you can check the context of the configureFormFields() admin function with a function $this->subjectExists(). If it's true, you're editing, otherwise you're creating!

于 2018-07-09T10:37:48.763 回答