Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

custom action in SonataAdminBundle

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?

like image 763
Sergei Zherevchuk Avatar asked May 24 '12 15:05

Sergei Zherevchuk


1 Answers

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]
like image 145
Mun Mun Das Avatar answered Sep 20 '22 15:09

Mun Mun Das