10

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

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

list__action_calculate.html.twig というテンプレートを src/myapp/MyBundle/Resources/views/Admin/ に置きます:

{% 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のこのページよりもドキュメントに手がかりがありますか。

4

1 に答える 1

8

ついに手に入れた!

adminクラス:

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/ mysite / mybundle / Resources / views / 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 %}
于 2012-09-17T14:02:48.193 に答える