Search code examples
phpsymfonyfosuserbundlesymfony-sonatasonata-admin

SonataUser - Remove fields from admin form


I'm using FOSUser with SonataUser & SonataAdmin. Also I used SonataEasyExtends to create app/Application/Sonata/UserBundle as told in several tutorials when using FOS with Sonata.

I simply would like to remove several default fields from the Admin form of the user entity like Social ones for example (facebook, twitter, ...).

I tried to override the form by putting

#app/Application/Sonata/UserBundle/Admin/Model/UserAdmin.php

namespace Application\Sonata\UserBundle\Admin\Model;

use Sonata\UserBundle\Admin\Model\UserAdmin as UserAdmin;

class MyUserAdmin extends UserAdmin
{
    /**
     * {@inheritdoc}
     */
    protected function configureFormFields(FormMapper $formMapper)
    {
        $formMapper
            ->with('General')
                ->add('username')
                ->add('email')
                ->add('plainPassword', 'text', array('required' => false))
            ->end()
            ->with('Groups')
                ->add('groups', 'sonata_type_model', array('required' => false))
            ->end()
        ;

        if (!$this->getSubject()->hasRole('ROLE_SUPER_ADMIN')) {
            $formMapper->with('Management')
                ->add('roles', 'sonata_security_roles', array(
                    'expanded' => true,
                    'multiple' => true,
                    'required' => false
                ))
                ->add('locked', null, array('required' => false))
                ->add('expired', null, array('required' => false))
                ->add('enabled', null, array('required' => false))
                ->add('credentialsExpired', null, array('required' => false))
            ->end();
        }

        $formMapper
            ->with('Security')
                ->add('token', null, array('required' => false))
                ->add('twoStepVerificationCode', null, array('required' => false))
            ->end();
    }
}

I think I have to register it as a service, and tell Sonata to use it by default.

But I don't know how to do it.

Am I at least on the right track ?


Solution

  • Sonata User Admin's form will not be automatically overridden.

    According to Thomas Rabaix we have to tell sonata to use our new admin instance

    #app/config/config.yml
    services:
        vendor.admin.extens:
            class: Application\Sonata\ProductBundle\Admin\ProductAdmin
            tags:
                - { name: sonata.admin.extens, target: sonata.product.admin.product }