Question

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 ?

Était-ce utile?

La solution 2

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 }

Autres conseils

That's right, simply point to your new UserAdmin class:

#app/config/config.yml
sonata_user:
admin:
    user:
      class:      Application\Sonata\UserBundle\Admin\UserAdmin    

After that you override the complete configureFormFields() method and leave out fields.

But there is also a remove() method:

class UserAdmin extends SonataUserAdmin
{
    // Fields to be shown on create/edit forms
    protected function configureFormFields(FormMapper $formMapper)
    {
        parent::configureFormFields($formMapper);
        $formMapper
            ->remove('facebookUid');
    }
}

You can do this with any field. Unfortunately there is no easy removeGroup('Social') method - this could be an improvement.

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top