Reputation: 462
Is it possible to define Sonata admins dynamically? The problem is, I have n lists (or classifiers) which I want to create admin for, and I don't fancy the idea to create separate service for every list.
Example lists:
list_id item_id name
COUNTRY EST Estonia
COUNTRY LAT Latvia
COUNTRY LTU Lithuania
LANG ET Estonian
LANG LV Latvian
...etc
Each list may have slightly different requirements (different validation rules for instance) and I would like to use default item Admin, with possibility to override it.
Or how would you do it?
Upvotes: 1
Views: 381
Reputation: 2518
You can simply add if-conditions in your ListMapper:
public function configureListFields(ListMapper $listMapper)
{
$listMapper
->add('id') // every Entity has this (for example)
;
if ($condition) {
->add('country') // only if $condition is true
}
}
But I dunno how you imagine to access you Admin. Different roles/routes?
A better approach would be creating an AbstractAdmin for your (Base)Entity and extending it for your use-cases (than you can add every Admin to your menue though). This would also easily allow you overwriting
use Sonata\AdminBundle\Admin\AbstractAdmin;
abstract class BaseAdmin extends AbstractAdmin
{
/**
* default validation group
*
* @var array
*/
protected $formOptions = array(
'validation_groups' => array('default'),
);
public function configureListFields(ListMapper $listMapper)
{
$listMapper->add('id'); // every entity gets this one (for example)
}
}
Then just extend your admin:
class CountryAdmin extends BaseAdmin
{
protected $formOptions = array(
'validation_groups' => array('default', 'country'),
);
public function configureListFields(ListMapper $listMapper)
{
parent::configureListFields($listMapper);
$listMapper->add('country');
}
}
I'm not sure if I got your entity-examples correct, but it should show you how to define your BaseAdmin and extend multiple Admins (allowing adding/removing fields and validation-groups per Admin).
Upvotes: 0