symfony - How to get child object in embedded Admin class in Sonata Admin? -


i'm trying , manipulate actual object related imageadmin class in sonataadmin (using symfony 2.3). works fine when imageadmin class 1 being used. when imageadmin embedded in admin goes horribly wrong.

here's works when don't have embedded admins:

class imageadmin extends admin {     protected $baseroutepattern = 'image';      protected function configureformfields(formmapper $formmapper) {         $subject = $this->getsubject();     } } 

but when embed imageadmin in parentadmin using this:

class pageadmin extends admin {     protected function configureformfields(formmapper $formmapper) {         $formmapper->add('image1', 'sonata_type_admin');     } } 

then when you're editing parent item id 10 , call getsubject() in imageadmin image id 10!

in other words getsubject() extracts id url calls $this->getmodelmanager()->find($this->getclass(), $id);, cross-references parent id , image id. oops!


so... want able hold of actual object being rendered/edited in current imageadmin instance, whether it's being edited directly or via embedded form, , able things it.

maybe getsubject() wrong tree barking up, note $this->getcurrentchild() returns false when called imageadmin::configureformfields(), when imageadmin embedded using sonata_type_admin field type. i'm quite confused...

anyway, hope possible hold of object in obvious way i've overlooked , here can enlighten me!

thanks tautrimas ideas, managed figure out answer this:

in imageadmin set this:

protected function configureformfields(formmapper $formmapper) {     if($this->hasparentfielddescription()) { // admin embedded         $getter = 'get' . $this->getparentfielddescription()->getfieldname();         $parent = $this->getparentfielddescription()->getadmin()->getsubject();         if ($parent) {           $image = $parent->$getter();         } else {           $image = null;         }     } else { // admin not embedded         $image = $this->getsubject();     }      // can things $image, show thumbnail in help:     $filefieldoptions = array('required' => false);     if ($image && ($webpath = $image->getwebpath())) {         $filefieldoptions['help'] = '<img src="'.$webpath.'" class="admin-preview" />';     }      $formmapper         ->add('file', 'file', $filefieldoptions)     ; } 

i'll post in upcoming sonataadmin cookbook soon!

https://github.com/sonata-project/sonataadminbundle/issues/1546


Comments

Popular posts from this blog

plot - Remove Objects from Legend When You Have Also Used Fit, Matlab -

java - Why does my date parsing return a weird date? -

Need help in packaging app using TideSDK on Windows -