Override Users Form Sonata UserBundle - php

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 {

Related

What is the use of Main File in Symfony 2 Bundle?

What is the use of Main File in Symfony 2 Bundle?
Below is the Default Path of a file:
Project->src->BundleName->BundleName.php
For Ex:
Symfony_Project/src/AppBundle/AppBundle.php
The Content Of Above file is Always Blank:
<?php
namespace AppBundle;
use Symfony\Component\HttpKernel\Bundle\Bundle;
class AppBundle extends Bundle
{
}
What is the use of this file in symfony?
Why We Can use this File?
Is it Mandatory or Not? /Can we Delete it?
Why it is empty?
This file can be used to override any other bundle (your application bundles / third party bundles) and its resources. You can set parent bundle for a given bundle. For example, you are having FosUserBundle included and you want to override some of its actions/layout files etc.. To accomplish this, create you bundle UserBundle.php. add FosUserBundle as its parent like as follows :
// src/UserBundle/UserBundle.php
namespace UserBundle;
use Symfony\Component\HttpKernel\Bundle\Bundle;
class UserBundle extends Bundle
{
public function getParent()
{
return 'FOSUserBundle';
}
}
Override controller:
// src/UserBundle/Controller/RegistrationController.php
namespace UserBundle\Controller;
use FOS\UserBundle\Controller\RegistrationController as BaseController;
class RegistrationController extends BaseController
{
public function registerAction()
{
$response = parent::registerAction();
// ... do custom stuff
return $response;
}
}
Then within your UserBundle directory structure, you can override controllers/layout files etc..
For more info please refer this link : https://symfony.com/doc/2.8/bundles/inheritance.html

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());

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.

No route found from another bundle

I have added a second bundle and when I try to open some url from that bundle, I keep getting an error that the route was not found. Adding the same route to the main bundle works perfectly.
What's wrong?
This is my project structure. I also:
- added the UserBundle to AppKernel.php (IDE shows the class exists)
- use AppBundle\Controller namespace in AppBundle, and UserBundle\Controller namespace in UserBundle
The controller I try to access from the UserBundle looks like this:
namespace UserBundle\Controller;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\Security\Core\SecurityContext;
class SecurityController extends Controller
{
/**
* #Route("/login", name="user_login")
*/
public function loginAction(Request $request)
{
return array();
}
}
I suspect you need to add the UserBundle to your routing.yml configuration. You should have something like this in the routing.yml:
user_bundle:
resource: "#UserBundle/Controller/"
type: annotation

Different lists for one model in sonata admin bundle

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)

Categories