Different lists for one model in sonata admin bundle - php

How can I create two different lists for one model? In my case I have one model User. And for this model in my dashboard I must have two lists: Members and Admins. I create for members:
sonata.admin.members:
class: Project\AdminBundle\Admin\MemberAdmin
tags:
- { name: sonata.admin, manager_type: orm, model_manager: sonata.admin.manager.project, group: Members, label: Members}
arguments: [null, %fos_user.model.user.class%, SonataAdminBundle:CRUD]
calls:
- [setTranslationDomain, [ProjectUserBundle]]
and for admins:
sonata.admin.user:
class: Project\UserBundle\Admin\UserAdmin
tags:
- { name: sonata.admin, manager_type: orm, model_manager: sonata.admin.manager.project, group: Admins, label: Admin Users}
arguments: [null, %fos_user.model.user.class%, ProjectUserBundle:CRUD]
calls:
- [setTranslationDomain, [ProjectUserBundle]]
So, I have two tabs in admin panel, but it has one url, and work only with one Admin class.
Any ideas?

set two parameters baseRoutePattern and baseRouteName in your admin class

I did a similar thing to this and used Doctrine Single Table Inheritance ( http://docs.doctrine-project.org/projects/doctrine-orm/en/2.0.x/reference/inheritance-mapping.html#single-table-inheritance ) on my base user class to create separate Admin and WebUser entities.
Then it's really straight forward to just use Sonata Admin as per the examples without any tweaks required to manage the two sets in different lists.

For Example First Admin Class:
<?php
namespace App\PreorderBundle\Admin;
use Sonata\AdminBundle\Admin\Admin;
use Sonata\AdminBundle\Datagrid\ListMapper;
use Sonata\AdminBundle\Datagrid\DatagridMapper;
use Sonata\AdminBundle\Form\FormMapper;
class UserAdmin extends Admin
{
protected $baseRouteName = 'preorder_user';
protected $baseRoutePattern = 'preorder_user';
protected function configureFormFields(FormMapper $formMapper)
Second Admin Class:
namespace App\SecurityBundle\Admin;
use Sonata\AdminBundle\Admin\Admin;
use Sonata\AdminBundle\Datagrid\ListMapper;
use Sonata\AdminBundle\Datagrid\DatagridMapper;
use Sonata\AdminBundle\Form\FormMapper;
class UserAdmin extends Admin
{
protected $baseRoutePattern = 'user';
protected $baseRouteName = 'user';
protected function configureFormFields(FormMapper $formMapper)

Related

Overriding Default FOSUserBundle Controller in Symfony 3.4

I'm using the Fosuserbundle to manager members in my project { SF::3.4.8 },
when trying to override the controller of the registrationController by following the Symfony documentation
<?php
namespace TestUserBundle;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\RedirectResponse;
use FOSUserBundle\Controller\RegistrationController as BaseController;
class RegistrationController extends BaseController {
public function registerAction(Request $request)
{
die("Hello");
}
}
but the system ignore that controller and still use The original controller, so if there any way to override my controller by
First, overriding the controller is probably not the best way to process. You should consider to hook into controller. Here is the related documentation: https://symfony.com/doc/master/bundles/FOSUserBundle/controller_events.html
Then if you still want to override the controller, you should act in the dependency injection. The service name of the controller is fos_user.registration.controller.
To replace the service you can simply use:
services:
fos_user.registration.controller:
class: YourController
arguments:
$eventDispatcher: '#event_dispatcher'
$formFactory: '#fos_user.registration.form.factory'
$userManager: '#fos_user.user_manager'
$tokenStorage: 'security.token_storage'
You can also override it in a CompilerPass. Which is probably the best solution for you because you do this inside another bundle.
Here is how it should look:
use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface;
use Symfony\Component\DependencyInjection\ContainerBuilder;
class ReplaceRegistrationController extends CompilerPassInterface
{
public function process(ContainerBuilder $container)
{
$container
->getDefinition('fos_user.registration.controller')
->setClass(YourController::class)
;
}
}
Don't forget to register it inside your bundle:
$container->addCompilerPass(new ReplaceRegistrationController());

Override Users Form Sonata UserBundle

Hello I tried to follow the explanation given here:
How to remove fields from the admin user create page of Sonata User Bundle?
To add / remove lines to the Sonata Userbundle registration form but the problem is that I find myself for this error:
Here are the few lines of code that I put:
in src/Application/Sonata/userBundle/Admin/UserAdmin.php
use Sonata\UserBundle\Admin\Model\UserAdmin as BaseUserAdmin;
use Sonata\AdminBundle\Form\FormMapper;
class UserAdmin extends BaseUserAdmin {
protected function configureFormFields( FormMapper $formMapper ) {
parent::configureFormFields($formMapper);
$formMapper
->remove('facebookUid');
}
}
In
app/config/config.php :
sonata_user:
security_acl: true
manager_type: orm
admin: # Admin Classes
user:
class: Application\Sonata\UserBundle\Admin\UserAdmin
controller: SonataAdminBundle:CRUD
Can someone tell me why? Thank you
You have forgotten to declare the namespace of your UserAdmin class.
It has to be the very first line of code, and will be like that in your case:
<?php
namespace UserBundle\Admin;
use Sonata\UserBundle\Admin\Model\UserAdmin as BaseUserAdmin;
use Sonata\AdminBundle\Form\FormMapper;
class UserAdmin extends BaseUserAdmin {

How to override FOSUserBundle ProfileFormType? -- Symfony 3.1

I've googled so many other options, and there are so many similar stack overflow questions that I've looked at, but they all dealt with outdated versions of Symfony or FOSUserBundle... It doesn't help that the FOSUserBundle documentation doesn't exactly cover how to add custom fields to the Profile page, only the registration page.
So how do I make custom fields I've added to the User class show up in the user's profile page? I've tried pretty much all other stack overflow attempts.
Here's my services.yml:
services:
app.profile.form.type:
class: AppBundle\Form\ProfileFormType
tags:
- { name: form.type, alias: app_user_profile }
Here's my config.yml:
fos_user:
profile:
form:
type: AppBundle\Form\ProfileFormType
And just one iteration of many of my Profile class:
<?php
namespace AppBundle\Form\Type;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilderInterface;
class ProfileFormType extends AbstractType
{
public function buildUserForm(FormBuilderInterface $builder, array $options)
{
$builder->add('pokemon');
}
public function getParent()
{
return 'FOS\UserBundle\Form\Type\ProfileFormType';
}
public function getBlockPrefix()
{
return 'app_user_profile';
}
// For Symfony 2.x
public function getName()
{
return $this->getBlockPrefix();
}
}`
Maybe you should extend the parent form class?
ProfileFormType extends \FOS\UserBundle\Form\Type\ProfileFormType
{
....
}
There is a parameter class (User entity) passed throught constructor
In your profile class , the namespace is :namespace AppBundle\Form\Type
it means that you should put in your service :
services:
app.profile.form.type:
class: AppBundle\Form\Type\ProfileFormType
tags:
- { name: form.type, alias: app_user_profile }
And make sure you are creation the folder type
Dunno if you have still this proble, but It's probably just the buildUserForm.
Change the method name to buildForm, with the getParent(), you'll get a $builder already filled with default FosUser fields in the buildForm() method.

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:
It's possible to override Sonata User entities in order to get ride of some useless properties? How?
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:
I should be able to add new dynamic roles from there and I can't.
Is this possible? How? Any workaround?
I took a look at Github here and Docs here but couldn't find nothing helpful. Any advices?
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.

Use doctrine CRUD in Sonata Admin

I generated CRUD with command:
doctrine:generate:crud --entity=TeoBlogBundle
and i have simply CRUD for my Bundle. I would like use this CRUD in Sonata Admin Bundle, but I never found example for this.
I must create BlogAdmin class for my Bundle? Is not possible to import my CRUD? I want use my BlogBundle only in backend - SonataAdmin, where i have others bundles to manage my site. I would like add this to menu Sonata.
Sonata Admin bundle allow to extend CRUD controller and then you can use it without generating doctrine crud. You must create BlogAdmin Bundle. Use following code:
namespace Tutorial\BlogBundle\Controller;
use Sonata\AdminBundle\Controller\CRUDController as Controller;
class CommentAdminController extends Controller
{
}
Or you can suppress the need to create the admin controller. Here's example syntax for config.yml. The null below is required to accomplish this.
my.listing.admin.resource:
class: My\ExampleBundle\Admin\ResourceAdmin
tags:
- { name: sonata.admin, manager_type: orm, group: "Resource", label: "My Resource" }
arguments: [null, My\ExampleBundle\Entity\Resource, null ]
^
|
---------------------------------------------------------+

Categories