Overriding the User Admin Form

烈酒焚心 提交于 2019-12-03 15:57:49

The overriding class should be set in config.yml:

# app/config/config.yml
sonata_user:
  admin:
    user:
      class:      MyCompany\UserBundle\Admin\Model\UserAdmin

Extend original UserAdmin:

namespace MyCompany\UserBundle\Admin\Model;

use Sonata\AdminBundle\Form\FormMapper;

class UserAdmin extends \Sonata\UserBundle\Admin\Model\UserAdmin
{

    protected function configureFormFields(FormMapper $formMapper)
    {
        // new logic
    }

}

Of course change class name MyCompany\UserBundle\Admin\Model\UserAdmin to reflect your bundle structure.

It is a better practice to keep your bundles in the src directory instead: (See Creating a bundle section). In this case, if you are using easy extends, make sure to use --dest=src in order to generate the bundle inside an Application namespace in src/.

php app/console sonata:easy-extends:generate SonataUserBundle --dest=src

By creating your overriding bundle in src/Application/Sonata/UserBundle and registering the vendor bundle as a parent, you won't have to create a new service. This explains you how to override the bundle properly: overriding a bundle and should save you a lot of time.

Don't forget to create the file you want to override in the same location as your parent bundle. In your case, you would have to copy paste SonataUser/Admin/Model/UserAdmin.php from the vendor into your bundle src/Application/Sonata/UserBundle/Admin/Model/UserAdmin.php and modify it as you wish. That's why overriding bundles can be so useful.

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