因此,在 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 ?