简体   繁体   English

Sonata Admin:添加自定义触发器/操作以列出/编辑操作

[英]Sonata Admin: Add custom triggers/actions to list/edit action

I'm using SonataAdminBundle for managing entities in my application. 我正在使用SonataAdminBundle来管理我的应用程序中的实体。 The admins of the site can add videos, and some of them first need to be approved by their speakers. 该网站的管理员可以添加视频,其中一些首先需要由他们的扬声器批准。 There is an authorization system working already - I have working code which will generate a special link and notify the speaker, who can approve or disapprove the video, and notify back the admins automatically. 已经有一个授权系统正在运行 - 我有一个工作代码,它将生成一个特殊链接,并通知可以批准或拒绝视频的发言人,并自动通知管理员。

I'd like to customize my admin section, so there will be a button ask for authorization next to the videos. 我想自定义我的管理部分,因此在视频旁边会有一个按钮ask for authorization I'm okay having it either in the list action ( /admin/acme/videos/list ) or in the edit action somewhere in the right-nav ( /admin/acme/videos/x/edit/ ) 我可以在列表操作( /admin/acme/videos/list )或右侧导航中的某个编辑操作中使用它( /admin/acme/videos/x/edit/

What's the best approach to do this? 这样做的最佳方法是什么? The documentation says very little about blocks customization, but I found this example which may be the thing I'm looking for, but I couldn't figure out how to use it. 文档对块自定义说的很少,但我发现这个例子可能是我正在寻找的东西,但我无法弄清楚如何使用它。

One option is to use the preUpdate hook, and add a checkbox to the edit action, but a button would be much nicer. 一个选项是使用preUpdate挂钩,并为编辑操作添加一个复选框,但按钮会更好。

To add an action for edit form 为编辑表单添加操作

Add to your admin class: 添加到您的管理类:

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('My action', array('uri' => 'http://google.com?id=' . $id));
}

It will create left side menu for actions like /admin/acme/videos/x/edit/. 它将为/ admin / acme / videos / x / edit /等操作创建左侧菜单。 Having id for current item allows you to build any custom URL. 拥有当前项的ID允许您构建任何自定义URL。

To add an action for list: In your admin file add 要为列表添加操作:在管理文件中添加

protected function configureListFields(ListMapper $listMapper)
{
    $listMapper
        ->add('_action', 'actions', array(
            'actions' => array(
                'act' => array('template' => 'AcmeBundle:Video:my_temp.html.twig'),
            )
        ))
    ;
}

It will add a column with links, then you need to create a template for your column, something like 它会添加一个包含链接的列,然后您需要为列创建一个模板,例如

<a href="{{ admin.generateObjectUrl('delete', object) }}" class="delete_link" title="{% trans from 'SonataAdminBundle' %}action_delete{% endtrans %}">
    <img src="{{ asset('bundles/sonataadmin/famfamfam/delete.png') }}" alt="{% trans from 'SonataAdminBundle' %}action_delete{% endtrans %}" />
</a>

All examples are taken from link that you provided. 所有示例均来自您提供的链接。 Hope it helps 希望能帮助到你

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

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