SonataUser - Advanced Admin

I am trying to change the default admin of a User object.
You just need to remove certain fields from the form.

I assume that this document will be useful to me when it is available.
At the moment, I created this administrator and tried to override the default value of User .

Application / 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; } } 

application /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 get

Unable to 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, application instance \ Sonata \ UserBundle \ Admin \ Model \ UserAdmin, 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?

+6
source share
5 answers

In your config.yml add the following:

 parameters: sonata.user.admin.user.class: Application\Sonata\UserBundle\Admin\Model\UserAdmin 
+3
source

If someone once considers this, I got this working by overriding UserAdmin.php

add the following line to the registerBundle method of the application /AppKernel.php

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

Now set the value of the sonata.user.admin.user.class parameter to the FQCN of the User object that was created during the installation of the FOSUserBundle.

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

Now create a class that extends the UserAdmin class by default and overrides the configureShowFields, configureFormFields, configureDatagridFilters and configureListFields methods to add the necessary user admin fields. The following is an example of the extended UserAdmin class, which is based on a custom bone object created in the 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, for example

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

If everything is configured correctly, you will see a new line of users on the admin / dashboard page. All user actions should work as expected.

+3
source

Change the tag in your service definition

 tags: - { name: sonata.admin, manager_type: orm, group: some.menu, label: User } 
+1
source

The same fix for Symfony 2.1, you need to specify your own UserAdmin class for the Sonata package, like this

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

Also check out the Sonata UserBundle package for more options.

+1
source

You get this error because you are trying to create an administrator extension, and your class does not use AdminExtensionInterface. You can also extend the abstract AdminExtension class, which implements the interface.

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

0
source

Source: https://habr.com/ru/post/920208/


All Articles