How to remove fields from the admin user create page of Sonata User Bundle?

五迷三道 提交于 2019-12-29 09:50:11

问题


I have installed Sonata Admin Bundle and User Bundle in my Symfony 2.6 application. I found that the User bundle creates a bundle at src/Application/Sonata/UserBundle. When I go to the admin user creation page (admin/sonata/user/user/create), I found a lot of fields there:

  • General
    • Username
    • E-Mail-Address
    • Plain password
  • Groups
  • Profile
    • Date of birth
    • Firstname
    • Lastname
    • Website
    • Biography
    • Gender
    • Locale
    • Timezone
    • Phone
  • Social
    • Facebook Uid
    • Facebook Name
    • Twitter Uid
    • Twitter Name
    • Google+ Uid
    • Google+ Name
  • Management

    • Roles
    • Locked
    • Expired
    • Enabled
    • Credentails expired
  • Security

    • Token
    • Two Step Verification Code

I only need Username, E-Mail, Password, Groups, Firstname and Lastname for my admin user creation. How can I remove the other unnecessary fields from that page?


回答1:


Under configuration for sonata user bundle in config.yml i.e sonata_user you can override sonata user admin class and define your own admin class

sonata_user
    admin:                  # Admin Classes
        user:
            class:          Application\Sonata\UserBundle\Admin\UserAdmin
            #Sonata\UserBundle\Admin\Entity\UserAdmin
            controller:     ApplicationSonataUserBundle:UserCURD /** you can also override CURD controller for your admin class*/
            translation:    SonataUserBundle

Now create your user admin class in your extended bundle i.e ApplicationSonataUserBundle and extend your class with sonata's Sonata\UserBundle\Admin\Model\UserAdmin, now in configureFormFields() define your desired fields you want to add

use Sonata\UserBundle\Admin\Model\UserAdmin as BaseUserAdmin;
class UserAdmin extends BaseUserAdmin {
    protected function configureFormFields( FormMapper $formMapper ) {
     $formMapper->add('some field'); ...
     /** Do your stuff here */
    }
}

Or remove some of the fields :

use Sonata\UserBundle\Admin\Model\UserAdmin as BaseUserAdmin;
class UserAdmin extends BaseUserAdmin {
    protected function configureFormFields( FormMapper $formMapper ) {
        parent::configureFormFields($formMapper);
        $formMapper->remove('some field');
    }
}

See ADVANCED CONFIGURATION for sonata user bundle



来源:https://stackoverflow.com/questions/30690298/how-to-remove-fields-from-the-admin-user-create-page-of-sonata-user-bundle

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!