Overriding the User Admin Form

1,972 views
Skip to first unread message

Pierre de LESPINAY

unread,
Jul 10, 2012, 8:20:26 AM7/10/12
to sonata...@googlegroups.com
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:

```yml
# 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 ?

Dmitry Abrosimov

unread,
Jul 10, 2012, 8:36:21 AM7/10/12
to sonata...@googlegroups.com
I've encountered the same task, I wanted to override UserAdmin class to configure some fields for my specific purposes so what i did was just added the following lines into the /app/config/config.yml:

parameters:
    sonata.user.admin.user.class: BundleNamespace\AdminBundle\Admin\UserAdmin

Thomas Rabaix

unread,
Jul 10, 2012, 8:40:19 AM7/10/12
to sonata...@googlegroups.com
You can register an AdminExtension which can be used to alter another admin instance.

services:
    vendor.admin.product_extension:
        class: Application\Sonata\ProductBundle\Admin\ProductAdmin
        tags:
            - { name: sonata.admin.extension, target: sonata.product.admin.product }


--
You received this message because you are subscribed to the Google Groups "sonata-users" group.
To view this discussion on the web visit https://groups.google.com/d/msg/sonata-users/-/gGu8boWRUCQJ.

To post to this group, send email to sonata...@googlegroups.com.
To unsubscribe from this group, send email to sonata-users...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/sonata-users?hl=en.



--
Thomas Rabaix
http://rabaix.net | http://sonata-project.org

Pierre de LESPINAY

unread,
Jul 10, 2012, 10:08:53 AM7/10/12
to sonata...@googlegroups.com
Thank you Thomas, that's what I'm searching for
So for my case I should have:

#/app/config/config.yml
#...
services:
    sonata.admin.user_extension:
        class: Application\Sonata\UserBundle\Admin\Model\UserAdmin
        tags:
            - { name: sonata.admin.extension, target: sonata.user.admin.user }
#...

Shouldn't I ?

It's giving me

> Cannot import resource "/var/www/Symfony/app/config/."
> ...
Declaration of Application\Sonata\UserBundle\Admin\Model\UserAdmin::configureListFields() should be compatible with that of Sonata\UserBundle\Admin\Model\UserAdmin::configureListFields() in /var/www/Symfony/app/Application/Sonata/UserBundle/Admin/Model/UserAdmin.php line 180

Not sure my extension is valid

Pierre de LESPINAY

unread,
Jul 10, 2012, 10:41:06 AM7/10/12
to sonata...@googlegroups.com
Should I have a getParent() in my extension ?

Thomas Rabaix

unread,
Jul 11, 2012, 6:37:56 PM7/11/12
to sonata...@googlegroups.com
No, the extension should implement the AdminExtensionInterface or extends base AdminExtension class.

--
You received this message because you are subscribed to the Google Groups "sonata-users" group.
To view this discussion on the web visit https://groups.google.com/d/msg/sonata-users/-/k_KZ4Y-Yr9MJ.

To post to this group, send email to sonata...@googlegroups.com.
To unsubscribe from this group, send email to sonata-users...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/sonata-users?hl=en.

quentin delettre

unread,
Oct 1, 2012, 12:22:03 PM10/1/12
to sonata...@googlegroups.com, tho...@rabaix.net

Can you provide us an example ?

I don't get it. I got this error :

PHP Fatal error:  Call to undefined method Sonata\UserBundle\Admin\Entity\UserAdmin::setTranslationDomain()

 I put in my services :


    sonata.admin.user_extension:
        class: Application\Sonata\UserBundle\Admin\Model\UserAdmin
        tags:
            - { name: sonata.admin.extension, target: sonata.user.admin.user }

 
and in /Application/Sonata/UserBundle/Admin/Model/UserAdmin.php :


use Sonata\AdminBundle\Admin\AdminExtension;
...
class UserAdmin extends AdminExtension

 As requested i modified the function from protected to public due to errors.

I don't understand where the translation problem come from... ?



Le jeudi 12 juillet 2012 00:37:56 UTC+2, Thomas Rabaix a écrit :
No, the extension should implement the AdminExtensionInterface or extends base AdminExtension class.

quentin delettre

unread,
Oct 3, 2012, 3:43:18 AM10/3/12
to sonata...@googlegroups.com, tho...@rabaix.net
After many tries (and errors). Answer from Dimitry Abrosimov was more simple and worked. In summary, i don't get it how to extend AdminExtension and have a service to alter admin instance.

Thanks anyway.
Message has been deleted

stef

unread,
Oct 12, 2012, 3:31:21 AM10/12/12
to sonata...@googlegroups.com, tho...@rabaix.net
Can you tell me exactly how you solved?. I'm getting same error as

Cannot import resource "/app/config/." from "/app/config/routing.yml". after that i could not follow what is there in this post.

Cassiano Tartari

unread,
Jan 29, 2013, 1:10:54 PM1/29/13
to sonata...@googlegroups.com
The error:

Declaration of Application\Sonata\UserBundle\Admin\Model\UserAdmin::configureListFields() should be compatible with that of Sonata\UserBundle\Admin\Model\UserAdmin::configureListFields() in /var/www/Symfony/app/Application/Sonata/UserBundle/Admin/Model/UserAdmin.php line 180

Is solved adding the "use Sonata\AdminBundle\Form\FormMapper;" before the class declaration.

I am still trying to override the user form to show just some fields and specific ones of my application without success.

Anothe thing that I still trying to understand are the roles. In my system I will have just 4 kind of users Super Admins, Group Admins and 2 kind of simple user inside each group.

Christian Herzog

unread,
Feb 25, 2013, 6:23:35 AM2/25/13
to sonata...@googlegroups.com
I think I solved this by doing the following:

config.yml:
change the user admin class to your new class, eg.
sonata_user:
    security_acl:     false
    class:
        user:         Tech\NewsBundle\Entity\User
    admin:                  # Admin Classes
        user:

            class:          Application\Sonata\UserBundle\Admin\Model\UserAdmin
            controller:     SonataAdminBundle:CRUD
            translation:    SonataUserBundle
Note the "Application\Sonata\UserBundle\Admin\Model\UserAdmin".
I did not need the service:
services:
    sonata.admin.user_extension:
        class: Application\Sonata\UserBundle\Admin\Model\UserAdmin
        tags:
            - { name: sonata.admin.extension, target: sonata.user.admin.user }
        arguments: [ null, Application\Sonata\UserBundle\Admin\Model\UserAdmin, "ApplicationSonataUserBundle:UserAdmin" ]
        #$code, $class, $baseControllerName
This gave me an error anyway, as I didn't figure out the correct values for the arguments. If anybody knows what goes there, it'd be nice to know :)

Other than that, the start of my useradmin.php looks like this:
<?php
#app/Application/Sonata/UserBundle/Admin/Model/UserAdmin.php

namespace Application\Sonata\UserBundle\Admin\Model;
//        Application\Sonata\UserBundle\Admin\Model

use Sonata\AdminBundle\Admin\Admin;

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 SonUserAdmin;

class UserAdmin extends SonUserAdmin
{


Hope that helps. Took me quite a while.

Disclaimer: if a symfony pro sees any NO-GO, please tell me!
I am fairly new and haven't gotten the full overview ...

Thanks!
Reply all
Reply to author
Forward
0 new messages