Skip to content

Deleting a Group of Fields from an Admin

Edit this page

In some cases, when you extend existing Admins, you might want to delete fields from the admin, or make them not show. You could delete every field by hand, using the FormMappers remove method:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
final class UserAdmin extends Sonata\UserBundle\Admin\Model\UserAdmin
{
    protected function configureFormFields(FormMapper $form): void
    {
        parent::configureFormFields($form);

        $form
            ->remove('facebookName')
            ->remove('twitterUid')
            ->remove('twitterName')
            ->remove('gplusUid')
            ->remove('gplusName');
    }
}

This works, as long as the extended Admin does not use Groups to organize its field. In the above example, we try to remove all fields from the User Admin, that comes with the SonataUserBundle. However, since the fields we deleted, are all part of the 'Social' Group of the form, the fields will be deleted and the empty group will stay. For this case, the FormMapper comes with a method, which allows you to get rid of a whole form group: removeGroup:

1
2
3
4
5
6
7
8
9
final class UserAdmin extends Sonata\UserBundle\Admin\Model\UserAdmin
{
    protected function configureFormFields(FormMapper $form): void
    {
        parent::configureFormFields($form);

        $form->removeGroup('Social', 'User');
    }
}

This will remove the whole 'Social' group from the form, which happens to contain all the fields, we deleted manually in the first example. The second argument is the name of the tab, the group belongs to. This is optional. However, when not provided, it will be assumed that you mean the 'default' tab. If the group is on another tab, it won't be removed, when this is not provided. There is a third optional argument for the method, which let's you choose, whether or not, tabs are also removed, if you happen to remove all groups of a tab. This behavior is disabled by default, but can be enabled, by setting the third argument of removeGroup to true.

This work, including the code samples, is licensed under a Creative Commons BY-SA 3.0 license.
TOC
    Version