ReynierPM
ReynierPM

Reputation: 18660

How to override SonataUser entities properties and forms?

I am setting up a brand new project using Symfony 2.7.x and Sonata Admin 2.3.x plus Sonata User. By default Sonata add a bunch of useless fields and I want to keep my entity as clean as possible. So my first question is:

Now as second part of the question and related to the same I want to create or use my own form for add new Users and/or Groups because with defaults ones I can't add roles. See the image below to see what I am talking about:

Sonata Group Add

I should be able to add new dynamic roles from there and I can't.

I took a look at Github here and Docs here but couldn't find nothing helpful. Any advices?

Upvotes: 2

Views: 440

Answers (1)

Tjaari76
Tjaari76

Reputation: 46

You can get rid of the Sonata properties by extending the FOSUserBundle entity directly rather than SonataUser User model.

change the entity that your User extends actually:

use Sonata\UserBundle\Model\User as BaseUser;

to the following:

use FOS\UserBundle\Entity\User as BaseUser;

Then, to delete useless properties from forms and maybe add new, override the default sonata UserAdmin class:

1- Create an admin class called UserAdmin in your own bundle.

2- Open the file vendor/sonata-project/user-bundle/Admin/Model/UserAdmin.php and take the configureFormFields from it. Paste it in your own admin class and keep only the fields you need by deleting useless fields from the base form builder.

The class can looks like:

use FOS\UserBundle\Model\UserManagerInterface;
use Sonata\AdminBundle\Admin\Admin;
use Sonata\AdminBundle\Form\FormMapper;

class UserAdmin extends Admin // You can extends directly from SonataUserAdmin if it's easier for you
{
    protected $userManager;

    protected function configureFormFields(FormMapper $formMapper)
    {
        $formMapper
           ... The fields you keep ...
    }

    /**
     * @param UserManagerInterface $userManager
     */
    public function setUserManager(UserManagerInterface $userManager)
    {
        $this->userManager = $userManager;
    }

    /**
     * @return UserManagerInterface
     */
    public function getUserManager()
    {
        return $this->userManager;
    }
}

3- Define the new UserAdmin class as a service

services:
    sonata.user.admin.user:
        class: YourOwnAdminBundle\Admin\UserAdmin
        tags:
            - { name: sonata.admin, manager_type: orm, group: %sonata.user.admin.groupname%, label: "User", label_catalogue: %sonata.user.admin.label_catalogue%", icon: "%sonata.user.admin.groupicon%"}
        arguments:
            - ~
            - %sonata.user.admin.user.entity%
            - %sonata.user.admin.user.controller%
        calls:
            - [setUserManager, ["@fos_user.user_manager"]]
            - [setTranslationDomain, ["%sonata.user.admin.user.translation_domain%"]]

Then, adapt the configuration of sonata-user in config.yml:

sonata_user: 
    ...
    admin:
        user:
            class:          YourOwnAdminBundle\Admin\UserAdmin
            controller:     SonataAdminBundle:CRUD
            translation:    SonataUserBundle

And it should be good.

Look at this similar question in case of I forgotten something or you need more.

Upvotes: 2

Related Questions