How to get underlying object in Sonata's Admin class when called by sonata_type_admin?
Asked Answered
S

3

8

So, in edit action of Sonata Admin I'm trying to display different form fields depending on create or edit context.

Maybe some background first..

I have a Gallery entity and a CoverPhoto entity bound with OneToOne.

Gallery:

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

CoverImage:

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

Here's coresponding GalleryAdmin class:

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 
}

And here we go with 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'))
        ;
    }
}

"Thumbnail" is my custom form field which displays thumbnail (shocker). But now i want this field only appear in "edit" context.

This should be piece of cake with

$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 ?

Scarificator answered 21/11, 2012 at 18:32 Comment(1)
Just for the record, that maximum nesting level error is to do with XDebug and nothing to do with Sonata AdminEffusion
S
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.

Scarificator answered 6/1, 2013 at 12:58 Comment(2)
Flameheart you should mark this answer as valid if this solved your question. Greetings!Philipps
I agree about the refactoring.Lambertson
E
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.

Effusion answered 13/2, 2014 at 18:21 Comment(1)
Thanks for this. I added your solution to my blog: blog.webdevilopers.net/… - In addition you can check the same inside your template with {% if admin.id(object) is not null %}.Upanishad
J
1

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!

Jolyn answered 9/7, 2018 at 10:37 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.