Lets assume i have a abstract doctrine entity class
abstract class BaseClass
{
private $title;
private $description;
}
and two inherited child classes
class A extends BaseClass
{
}
and with an extra Field
class B extends BaseClass
{
private $extraField;
...
}
For those two entities i want to create one admin for listing both together in one view. So in my admin.yml i add
AppBundle\Admin\BaseClassAdmin:
tags:
- { name: sonata.admin, manager_type: orm, label: "base" }
arguments:
- null
- AppBundle\Entity\BaseClass
-
calls:
- [setSubClasses, [{'A' : 'AppBundle\Entity\A', 'B' : 'AppBundle\Entity\B'}]]
And the BaseClass Admin class is
class BaseClassAdmin extends AbstractAdmin
{
protected function configureListFields(ListMapper $listMapper)
{
$listMapper
->addIdentifier('title');
}
protected function configureFormFields(FormMapper $form)
{
$form->add('title');
$form->add('description');
}
}
What i want to do is that the listview displays both types which is not the problem. But when i click on one item which is of type B i want to show the extraField in my form. But it doesn't.
This is cause instead calling the particular admin for B (which has extraField added in form) its calling only the general admin (BaseClassAdmin).
So my question
Is there a way to call the particular admin corresponding to the type of the entity from the list view? Or is the only way to modify the BaseClassAdmin and solving it by adding ugly if-statements in the form method?
You can call $this->getSubject()
in configureFormFields
, and add fields conditionally based on an instanceof check.