简体   繁体   English

SonataAdmin自定义表单操作

[英]SonataAdmin custom form action

I am using SonataAdminBundle and I'd like to know how to add a custom form action in the edit (Something similar to the Save, Update and Close ) 我正在使用SonataAdminBundle ,我想知道如何在edit添加自定义表单操作(类似于Save, Update and Close

There doesn't seem to anything documented about it. 似乎没有任何记载。

I'm trying to add a custom input field that will call a controller or something to update a value and send an email 我正在尝试添加一个自定义输入字段,该字段将调用控制器或其他东西来更新值并发送电子邮件

Is there any docs or examples on how to do this? 是否有任何有关此操作的文档或示例?

Thanks 谢谢

You can add custom form actions by adding new routes. 您可以通过添加新路线来添加自定义表单操作。 Because when you add new route you need also add action to handle this route. 因为当您添加新路线时,还需要添加操作来处理该路线。

Create Route 建立路线

You can register new routes by defining them in your Admin class. 您可以通过在Admin类中定义新路线来注册新路线。 Only Admin routes should be registered this way. 仅管理员路由应以这种方式注册。

The routes you define in this way are generated within your Admin's context, and the only required parameter to add() is the action name. 您以这种方式定义的路由是在管理员的上下文中生成的, add()唯一需要的参数是操作名称。 The second parameter can be used to define the URL format to append to baseRoutePattern , if not set explicitly this defaults to the action name. 第二个参数可用于定义要附加到baseRoutePattern的URL格式(如果未明确设置),则默认为操作名称。

<?php
use Sonata\AdminBundle\Route\RouteCollection;

class MediaAdmin extends Admin
{
        protected function configureRoutes(RouteCollection $collection)
    {
        $collection->add('myCustomAction');
        $collection->add('view', $this->getRouterIdParameter().'/view');
    }
}

Other steps needed to create your new action 创建新动作所需的其他步骤

In addition to defining the route for your new action you also need to create a handler for it in your Controller. 除了为新操作定义路线外,您还需要在Controller中为其创建处理程序。 By default Admin classes use SonataAdminBundle:CRUD as their controller, but this can be changed by altering the third argument when defining your Admin service (in your admin.yml file). 默认情况下,Admin类使用SonataAdminBundle:CRUD作为其控制器,但是可以通过在定义Admin服务时(在admin.yml文件中)更改第三个参数来更改它。

For example, lets change the Controller for our MediaAdmin class to AcmeDemoBundle:MediaCRUD: 例如,让我们将MediaAdmin类的Controller更改为AcmeDemoBundle:MediaCRUD:

# src/Acme/DemoBundle/Resources/config/admin.yml
sonata.admin.media:
    class: Acme\DemoBundle\Admin\MediaAdmin
    tags:
        - { name: sonata.admin, manager_type: orm, label: "Media" }
    arguments:
        - ~
        - Acme\DemoBundle\Entity\Page
        - 'AcmeDemoBundle:MediaCRUD' # define the new controller via the third argument
    calls:
        - [ setTranslationDomain, [Acme\DemoBundle]]

We now need to create our Controller, the easiest way is to extend the basic Sonata CRUD controller: 现在,我们需要创建控制器,最简单的方法是扩展基本的Sonata CRUD控制器:

use Sonata\AdminBundle\Controller\CRUDController;

class MediaCRUDController extends CRUDController
{
    public function myCustomAction()
    {
        // your code here ...
    }
}

Inside a CRUD template, a route for the current Admin class can be generated via the admin variable's generateUrl() command: 在CRUD模板中,可以通过admin变量的generateUrl()命令生成当前Admin类的路由:

<a href="{{ admin.generateUrl('list') }}">List</a>

<a href="{{ admin.generateUrl('list', params|merge('page': 1)) }}">List</a>

Just override the template you need and add this custom action. 只需覆盖您需要的模板并添加此自定义操作即可。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM