symfonysonata-adminsonatasonata-user-bundle

Extending Sonata User Bundle & custom actions


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


Solution

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