custom action in SonataAdminBundle

12,929

When you are creating service for EntityAdmin class the third argument is the controller name. You can create a class that extends CRUDController and set it in service. e.g

The controller,

//Vendor\YourBundle\Controller\EntityAdminController.php

class EntityAdminController extends CRUDController
{
    public function ispremiumAction()
    {
        //process
    }
}

In services.yml,

entity.admin.service:
  class: FQCN\Of\EntityAdmin
  tags:
    - { name: sonata.admin, manager_type: orm, group: your_group, label: Label }
  arguments: [null, FQCN\Of\Entity, VendorYourBundle:EntityAdmin]
Share:
12,929
Sergei Zherevchuk
Author by

Sergei Zherevchuk

Updated on July 27, 2022

Comments

  • Sergei Zherevchuk
    Sergei Zherevchuk almost 2 years

    On this page I found how to add route for my custom action.

    protected function configureRoutes(RouteCollection $collection) {
        $collection->add('ispremium', $this->getRouterIdParameter().'/ispremium'); 
    }
    

    After that I add custom action in my Admin class:

    protected function configureListFields(ListMapper $listMapper)
    {
        $listMapper
            ->addIdentifier('id')
            ->add('code', null, array('label' => 'Code'))
            ->add('_action', 'actions', array( 
                'actions' => array(  
                    'ispremium' => array(
                        'template' => 'AppMyBundleBundle:Admin:ispremium.html.twig'
                    )
                )
            ))
        ;
    }
    

    It generated url like this:

    /app_dev.php/admin/mobispot/discodes/discode/300876/ispremium
    

    My template for this link:

    <a href="{{ admin.generateObjectUrl('ispremium', object) }}">Link</a>
    

    I dont' know how to solve this problems:

    1. How to define custom controller for that route pass? Now I have an error:

      Method "Sonata\AdminBundle\Controller\CRUDController::ispremiumAction" does not exist.

    2. Can I change generated url with generateUrl method?