Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Overriding the User Admin Form

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 ?

like image 697
Pierre de LESPINAY Avatar asked Jul 10 '12 12:07

Pierre de LESPINAY


1 Answers

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.

like image 184
Aurelijus Rozenas Avatar answered Oct 06 '22 01:10

Aurelijus Rozenas