2

I have a app with Sonata Admin, Sonata User and Fosuser. I have extend the Admin file from Sonata Admin to add new column and field :

<?php

namespace AppBundle\Admin;

use Sonata\UserBundle\Admin\Model\UserAdmin as SonataUserAdmin;
use Sonata\AdminBundle\Admin\Admin;
use Sonata\AdminBundle\Datagrid\ListMapper;
use Sonata\AdminBundle\Datagrid\DatagridMapper;
use Sonata\AdminBundle\Form\FormMapper;
use Sonata\AdminBundle\Route\RouteCollection;

class UserAdmin extends SonataUserAdmin 
{
/**
    * {@inheritdoc}
    */
protected function configureFormFields(FormMapper $formMapper)
{
    parent::configureFormFields($formMapper);

    $formMapper
        ->with('Others')
            ->add('company')  
            ->add('locations', 'sonata_type_collection', array(
                'required' => false
            ), array(
                'edit' => 'inline',
                'inline' => 'table',
                'sortable'  => 'position',
            ))               
        ->end()
    ;
}

// Fields to be shown on filter forms
protected function configureDatagridFilters(DatagridMapper $datagridMapper)
{
    parent::configureDatagridFilters($datagridMapper);

    $datagridMapper
        ->remove('email')      
        ->add('firstname')
        ->add('lastname')
        ->add('company.name')     
    ;
}    

// Fields to be shown on lists
protected function configureListFields(ListMapper $listMapper)
{
    unset($this->listModes['mosaic']);
    $listMapper
        ->add('company.name')     
        ->add('firstname')
        ->add('lastname') 
    ;
    parent::configureListFields($listMapper);
    $listMapper->remove('email');   
    $listMapper->add('_action', 'actions', array(
        'actions' => array(
            'Invoiced' => array(
                'template' => 'AppBundle:User:list__action_invoiced.html.twig'
            ),
            'Credited' => array(
                'template' => 'AppBundle:user:list__action_credited.html.twig'
            )                
        )
        ));         
}   

protected function configureRoutes(RouteCollection $collection)
{
    parent::configureRoutes($collection);

    $collection->add('invoiced', $this->getRouterIdParameter().'/invoiced');
    $collection->add('credited', $this->getRouterIdParameter().'/credited');
}      
}

Now, I have a issue with the new actions "invoiced" and "credited"... Controller "Sonata\AdminBundle\Controller\CRUDController::invoicedAction" for URI "/admin/app/user/1/invoiced" is not callable.

This config work with the other admin page but not with this SonataUserAdmin extension. In the other pages I extend "Sonata\AdminBundle\Admin\Admin" but here I need to extend "Sonata\UserBundle\Admin\Model\UserAdmin" to use the user system of fos...

Do you have a idea for me ???

Thanks

6 Answers6

0

I think you just need to reconfigure the value of the baseControllerName argument to whatever your controller is. You can do that in your admin.yml configuration file.

greg0ire
  • 22,714
  • 16
  • 72
  • 101
0

I Have that

admin.user:
    class: AppBundle\Admin\UserAdmin
    arguments: [~, AppBundle\Entity\User, AppBundle:User]
    tags:
        - { name: sonata.admin, manager_type: orm, label: User, group: Users }     
    calls:
        - [ setTranslationDomain, [AppBundle]]    
0

You already configured the service. Now you have to create a UserController that extends the CRUDController and implements your invoicedAction and creditedAction methods.

<?php

namespace AppBundle\Controller;

use AppBundle\Entity\User;
use Sonata\AdminBundle\Controller\CRUDController;
use Symfony\Component\HttpFoundation\RedirectResponse;
use Symfony\Component\HttpFoundation\Request;

class UserController extends CRUDController
{

    /**
     *
     * @param string $id
     * @param Request $request
     *
     * @return RedirectResponse
     */
    public function invoicedAction($id = null, Request $request = null)
    {
        if ( $request == null ) {
            $request = $this->getRequest();
        }
        $id = $request->get($this->admin->getIdParameter());

        $user = $this->admin->getObject($id);
        /* @var $user User */

        if (!$user) {
            throw $this->createNotFoundException(sprintf('unable to find the user with id : %s', $id));
        }

        $this->admin->checkAccess('invoiced', $user);

        $this->admin->setSubject($user);

        /// your code here...

        return new RedirectResponse($this->admin->generateUrl('show', array('id' => $user->getId())));
    }
}
ClickLabs
  • 547
  • 3
  • 5
0

Always the same..

Controller "Sonata\AdminBundle\Controller\CRUDController::invoicedAction" for URI "/admin/app/user/1/invoiced" is not callable.

:-(

0

you need to set your controller under sonata_user section see:

https://sonata-project.org/bundles/user/master/doc/reference/advanced_configuration.html app/config/config.yml

sonata_user:
  admin:
    user:
      controller: AppBundle:User
0

(Symfony4.1 + Symfony Flex) It's unneccessary to register a service in services.yaml your userAdmin class. If you have done this - you will have two entities on your Admin Panel. To override basic userAdmin class make sure your config/packages/sonata.yaml looks like this (it's enough for overriding):

sonata_user:
security_acl: false
manager_type: orm
class:
    user: App\Entity\Manager
    group: App\Entity\Group
admin:
    user:
       class:          App\Admin\UserAdmin
       controller:     App\Controller\InitController
       translation:    SonataUserBundle