5

I'm trying to override the SonataUser/Admin/Model/UserAdmin's configureFormFields() because I need to remove some default fields from the admin form. So I have copied the file vendor/bundles/Sonata/UserBundle/Admin/Model/UserAdmin.php in my bundle app/Application/Sonata/UserBundle/Admin/Model/UserAdmin.php and modified it. Then declared it as a service:

# app/application/Sonata/UserBundle/Resources/config/services.yml
services:
    application_user.registration.form.type:
        class: Application\Sonata\UserBundle\Admin\Model\UserAdmin
        arguments: [%sonata_user.model.user.class%]
        tags:
            - { name: form.type, alias: application_user_admin }

Now questions: Am I doing right ? How can I tell sonata admin to use it ?

Pierre de LESPINAY
  • 44,700
  • 57
  • 210
  • 307

2 Answers2

16

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.

Aurelijus Rozenas
  • 2,176
  • 2
  • 28
  • 40
2

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.

Mick
  • 30,759
  • 16
  • 111
  • 130
  • Actually I used [SonataEasyExtends](http://sonata-project.org/bundles/easy-extends/master/doc/index.html) to extend the SonataUser Bundle (as told in the [docs](http://sonata-project.org/bundles/user/2-0/doc/reference/introduction.html)), it automatically creates the structure of the extending bundle. It's using `/app/Application` by default, I left it because it's always mentioned like that in SonataUser docs. – Pierre de LESPINAY Jul 10 '12 at 13:28
  • If you specify no parameter, the files are generated in app/Application/Sonata... but you can specify the path with --dest=src. I have updated my answer. – Mick Jul 10 '12 at 13:30
  • I saw that (I intentionally left it because they are still using this path in the docs. I'll move it later maybe). The question is more about the way to tell sonata to use my new Admin class. – Pierre de LESPINAY Jul 10 '12 at 13:40
  • You can still override the class instead of creating a new service anyway. Good luck :-) – Mick Jul 10 '12 at 13:41
  • That's what I did (see my question), but this class don't get overridden. – Pierre de LESPINAY Jul 10 '12 at 13:52
  • I see. That's surprising. Just to confirm, does your bundle override the vendor bundle properly by using the function getParent()? – Mick Jul 10 '12 at 13:57
  • Yes this is done automatically by EasyExtends. Entities and translations from this bundle are already taken into account (extended). There seems to be [something](https://groups.google.com/d/topic/sonata-users/Hp3P781GKI0/discussion) to declare in the config – Pierre de LESPINAY Jul 10 '12 at 14:18