2012-09-17 6 views
9

sonata admin bundleにアクションを追加しようとしています。私は私の管理クラスを変更:SonataAdminBundleでのアクションの追加

protected function configureListFields(ListMapper $listMapper) 
{ 
    $listMapper 
     ->addIdentifier('id') 
     ->add('name')   
     // add custom action links 
     ->add('_action', 'actions', array(
      'actions' => array(
       'view' => array(), 
       'calculate' => array('template' => 'myappMyBundle:Admin:list__action_calculate.html.twig'), 
       'edit' => array(), 
      ) 
     )) 
    ; 
} 

protected function configureSideMenu(MenuItemInterface $menu, $action, Admin $childAdmin = null) 
{ 
    if (!$childAdmin && !in_array($action, array('edit'))) { 
     return; 
    } 
    $admin = $this->isChild() ? $this->getParent() : $this; 
    $id = $admin->getRequest()->get('id'); 
    $menu->addChild('calculate', array('uri' => 'http://google.com?id=' . $id)); 
} 

およびSRC/myappに/ MyBundle /リソース/ビュー/管理/にlist__action_calculate.html.twigと呼ばれるテンプレートを置く:

{% if admin.isGranted('EDIT', object) and admin.hasRoute('edit') %} 
<a href="{{ admin.generateObjectUrl('calculate', object) }}" class="calculate_link" title="{{ 'action_calculate'|trans({}, 'SonataAdminBundle') }}"> 
    <img src="{{ asset('bundles/sonataadmin/famfamfam/page_white_edit.png') }}" alt="{{ 'action_calculate'|trans({}, 'SonataAdminBundle') }}" /> 
</a> 
{% endif %} 

しかし、私はsymfonyからこのエラーを得た:

An exception has been thrown during the rendering of a template 
("unable to find the route `mysite.mybundle.admin.myentity.calculate`") 
in "SonataAdminBundle:CRUD:list.html.twig" 

私は何が欠けていますか? Docのこのページよりも、ドキュメントにヒントがありますか?

答えて

6

最後にそれを手に入れました。

protected function configureRoutes(RouteCollection $collection) 
{ 
    $collection->add('calculate'); 

} 

# Override to add actions like delete, etc... 
public function getBatchActions() 
{ 
    // retrieve the default (currently only the delete action) actions 
    $actions = parent::getBatchActions(); 

    // check user permissions 
    if($this->hasRoute('edit') && $this->isGranted('EDIT') && $this->hasRoute('delete') && $this->isGranted('DELETE')) 
    { 
     // define calculate action 
     $actions['calculate']= array ('label' => 'Calculate', 'ask_confirmation' => true); 

    } 

    return $actions; 
} 

protected function configureListFields(ListMapper $listMapper) 
{ 
    $listMapper 
     ->addIdentifier('id') 
     ->add('name')   
     // add custom action links 
     ->add('_action', 'actions', array(
      'actions' => array(
       'view' => array(), 
       'calculate' => array('template' => 'chemoinfoEdrugBundle:CRUD:list__action_calculate.html.twig'), 
       'edit' => array(), 
      ) 
     )) 
    ; 
} 

と管理コントローラで:

public function batchActionCalculate(ProxyQueryInterface $selectedModelQuery) 
{ 
    ... 
} 

と中/ SRC /個人用サイト/ mybundle /リソース/ビュー/ CRUD:

{% if admin.isGranted('EDIT', object) and admin.hasRoute('edit') %} 
<a href="{{ admin.generateObjectUrl('calculate', object) }}" class="calculate_link" title="{{ 'action_calculate'|trans({}, 'SonataAdminBundle') }}"> 
    <img src="{{ asset('bundles/sonataadmin/famfamfam/calculator.png') }}" alt="{{ 'action_calculate'|trans({}, 'SonataAdminBundle') }}" /> 
</a> 
{% endif %} 
+0

公式adminクラスで

ドキュメント:[バッチアクション](http://sonata-project.org/bundles/admin/2-2/doc/reference/batch_actions.html) –

+0

新しい文書が公開されていますod、更新していただきありがとうございます。 – user1254498

関連する問題