6

I'm trying to modify the default admin of the User entity.
Just need to remove certain fields from the form actually.

I imagine this doc will be usefull for me when it'll be available.
For now I have created this admin and tried to override the default User one.

app/Application/Sonata/UserBundle/Admin/Model/UserAdmin.php

namespace Application\Sonata\UserBundle\Admin\Model;

use Sonata\AdminBundle\Form\FormMapper;
use Sonata\AdminBundle\Datagrid\DatagridMapper;
use Sonata\AdminBundle\Datagrid\ListMapper;
use Sonata\AdminBundle\Show\ShowMapper;
use Sonata\UserBundle\Admin\Model\UserAdmin as BaseType;

class UserAdmin extends BaseType
{
    /**
     * {@inheritdoc}
     */
    protected function configureListFields(ListMapper $listMapper)
    {
        $listMapper
            ->addIdentifier('username')
            ->add('groups')
            ->add('enabled')
        ;
    }

    /**
     * {@inheritdoc}
     */
    protected function configureFormFields(FormMapper $formMapper)
    {
        $formMapper
            ->with('General')
                ->add('username')
                ->add('email')
                ->add('plainPassword', 'text', array('required' => false))
            ->end()
            ->with('Groups')
                ->add('groups', 'sonata_type_model', array('required' => false))
            ->end()
            ->with('Profile')
                ->add('firstname', null, array('required' => false))
                ->add('lastname', null, array('required' => false))
            ->end()
        ;
    }

    /**
     * {@inheritdoc}
     */
    public function preUpdate($user)
    {
        $this->getUserManager()->updateCanonicalFields($user);
        $this->getUserManager()->updatePassword($user);
    }

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

}

app/config/config.yml

services:
    sonata.admin.extension:
        class: Application\Sonata\UserBundle\Admin\Model\UserAdmin
        tags:
            - { name: sonata.admin.extension, target: sonata.user.admin.user }
        arguments: [null, Sonata\UserBundle\Entity\User, SonataUserBundle:UserAdmin]

But I'm getting

Cannot import resource "/var/www/Symfony/app/config/." from "/var/www/Symfony/app/config/routing.yml".
...
ErrorException: Catchable Fatal Error: Argument 1 passed to Sonata\AdminBundle\Admin\Admin::addExtension() must be an instance of Sonata\AdminBundle\Admin\AdminExtensionInterface, instance of Application\Sonata\UserBundle\Admin\Model\UserAdmin given, called in /var/www/Symfony/app/cache/dev/appDevDebugProjectContainer.php on line 3139 and defined in /var/www/Symfony/vendor/bundles/Sonata/AdminBundle/Admin/Admin.php line 2359

What am I doing wrong ?

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

5 Answers5

4

In case someone will look this up someday, I got this working by overriding UserAdmin.php

add following line into registerBundle method of app/AppKernel.php

// app/AppKernel.php
public function registerBundles()
{
    $bundles = array(
        // other bundle declarations
        new Sonata\UserBundle\SonataUserBundle(),
    );
} 

Now set value of sonata.user.admin.user.class parameter to the FQCN of the User entity which was created during FOSUserBundle setup.

//app/config/config.yml
parameters:
    #....
    sonata.user.admin.user.entity: YourVendor\YourBundle\Entity\User

Now create a class that extends default UserAdmin class and override configureShowFields, configureFormFields, configureDatagridFilters and configureListFields methods to add the needed user admin fields. Following is the sample extended UserAdmin class which is based on the bare bone user entity created in FOSUserBundle documentation.

<?php
//src/YourVendor/YourBundle/Admin/UserAdmin.php

namespace YourVendor\YourBundle\Admin;

use Sonata\UserBundle\Admin\Model\UserAdmin as BaseUserAdmin;

use Sonata\AdminBundle\Form\FormMapper;
use Sonata\AdminBundle\Datagrid\DatagridMapper;
use Sonata\AdminBundle\Datagrid\ListMapper;
use Sonata\AdminBundle\Show\ShowMapper;

use FOS\UserBundle\Model\UserManagerInterface;
use Sonata\AdminBundle\Route\RouteCollection;


class UserAdmin extends BaseUserAdmin
{
    /**
     * {@inheritdoc}
     */
    protected function configureShowFields(ShowMapper $showMapper)
    {
        $showMapper
            ->with('General')
                ->add('username')
                ->add('email')
            ->end()
            // .. more info
        ;
    }

    /**
     * {@inheritdoc}
     */
    protected function configureFormFields(FormMapper $formMapper)
    {

        $formMapper
            ->with('General')
                ->add('username')
                ->add('email')
                ->add('plainPassword', 'text', array('required' => false))
            ->end()
            // .. more info
            ;

        if (!$this->getSubject()->hasRole('ROLE_SUPER_ADMIN')) {
            $formMapper
                ->with('Management')
                    ->add('roles', 'sonata_security_roles', array(
                        'expanded' => true,
                        'multiple' => true,
                        'required' => false
                    ))
                    ->add('locked', null, array('required' => false))
                    ->add('expired', null, array('required' => false))
                    ->add('enabled', null, array('required' => false))
                    ->add('credentialsExpired', null, array('required' => false))
                ->end()
            ;
        }
    }

    /**
     * {@inheritdoc}
     */
    protected function configureDatagridFilters(DatagridMapper $filterMapper)
    {
        $filterMapper
            ->add('id')
            ->add('username')
            ->add('locked')
            ->add('email')
        ;
    }
    /**
     * {@inheritdoc}
     */
    protected function configureListFields(ListMapper $listMapper)
    {
        $listMapper
            ->addIdentifier('username')
            ->add('email')
            ->add('enabled', null, array('editable' => true))
            ->add('locked', null, array('editable' => true))
            ->add('createdAt')
        ;

        if ($this->isGranted('ROLE_ALLOWED_TO_SWITCH')) {
            $listMapper
                ->add('impersonating', 'string', array('template' => 'SonataUserBundle:Admin:Field/impersonating.html.twig'))
            ;
        }
    }
}

Now set the value of sonata.user.admin.user.class to the FQCN of the created UserAdmin class in app/config/config.yml, e.g

parameters:
    sonata.user.admin.user.class: YourVendor\YourBundle\Admin\UserAdmin

If everything setup correctly you will see an new users row in admin/dashboard page. All user operations should work as expected.

Matheno
  • 4,112
  • 6
  • 36
  • 53
3

In your config.yml, add the following:

parameters:
    sonata.user.admin.user.class: Application\Sonata\UserBundle\Admin\Model\UserAdmin
Pierre
  • 1,553
  • 12
  • 22
1

Change the tag in your service definition

tags:
    - { name: sonata.admin, manager_type: orm, group: some.menu, label: User }
AdrienBrault
  • 7,747
  • 4
  • 31
  • 42
1

Same fix for Symfony 2.1, you need to specify custom UserAdmin class for Sonata bundle like this

sonata_user:
    admin:                  # Admin Classes
        user:
            class:          Application\Sonata\UserBundle\Admin\Entity\UserAdmin
            controller:     SonataAdminBundle:CRUD
            translation:    SonataUserBundle

Also check Sonata UserBundle documentation for more options

sumkincpp
  • 161
  • 3
0

You are getting this error, because you are trying to create admin extension and your class is not implementing AdminExtensionInterface. You could also extend the abstract AdminExtension class, which implements the interface.

This is clearly defined in the documentation of Sonata Admin Bundle - Extensions: http://sonata-project.org/bundles/admin/master/doc/reference/extensions.html

eroteev
  • 620
  • 1
  • 7
  • 17