本文介绍了SonataAdmin 自定义表单操作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 SonataAdminBundle,我想知道如何在 edit(类似于 Save、Update 和关闭)

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)

似乎没有任何关于它的记录.

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?

谢谢

推荐答案

您可以通过添加新路由来添加自定义表单操作.因为当您添加新路由时,您还需要添加处理此路由的操作.

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.

您可以通过在 Admin 类中定义新路由来注册新路由.仅应以这种方式注册 Admin 路由.

You can register new routes by defining them in your Admin class. Only Admin routes should be registered this way.

您以这种方式定义的路由是在管理员的上下文中生成的,add() 的唯一必需参数是操作名称.第二个参数可用于定义要附加到 baseRoutePattern 的 URL 格式,如果未明确设置,则默认为操作名称.

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. 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.

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

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

创建新操作所需的其他步骤

除了为新操作定义路由外,您还需要在控制器中为其创建处理程序.默认情况下,管理类使用 SonataAdminBundle:CRUD 作为它们的控制器,但这可以通过在定义管理服务(在 admin.yml 文件中)时更改第三个参数来更改.

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. 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).

例如,让我们将 MediaAdmin 类的控制器更改为 AcmeDemoBundle:MediaCRUD:

For example, lets change the Controller for our MediaAdmin class to 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]]

我们现在需要创建我们的控制器,最简单的方法是扩展基本的 Sonata CRUD 控制器:

We now need to create our Controller, the easiest way is to extend the basic Sonata CRUD controller:

use Sonata\AdminBundle\Controller\CRUDController;

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

在 CRUD 模板中,当前 Admin 类的路由可以通过 admin 变量的 generateUrl() 命令生成:

Inside a CRUD template, a route for the current Admin class can be generated via the admin variable’s generateUrl() command:

<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.

这篇关于SonataAdmin 自定义表单操作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-05 20:28