在 sonata admin Bundle 的列表视图中添加新按钮

Add new Button in the List view of sonata admin Bundle

也许这个问题太容易回答了,但是我在sonata admin bundle 的文档中搜索并没有找到我需要的东西。 当您导航到 sonata admin Bundle 中模型的列表视图时,您会在右上角找到操作按钮,在其下方您会找到添加新操作。 在我的例子中,我需要直接在视图中显示添加新操作,就像这个屏幕截图中的一样:

谁能帮帮我?

您使用的是哪个版本的奏鸣曲?你的例子的版本是什么?

我的是 2.3,看起来你不能通过参数播放它,但你可以覆盖布局以只显示创建按钮。

如果您的所有管理员都需要它,您最好覆盖配置中的布局。如果您只需要它用于列表、显示或删除,那么只覆盖那些模板。 在 config.yml:

sonata_admin:
    templates:
        layout:    AppBundle:Layouts:standard_layout_override.html.twig
        show:      AppBundle:Layouts:show.html.twig
        list:      AppBundle:Layouts:list.html.twig
        delete:    AppBundle:Layouts:delete.html.twig
        edit:      AppBundle:Layouts:edit.html.twig

在该文件中覆盖该块:

{% block sonata_page_content_header %}
    {% block sonata_page_content_nav %}
        {% if _tab_menu is not empty or _actions is not empty %}
            <nav class="navbar navbar-default" role="navigation">
                {% block tab_menu_navbar_header %}
                    {% if _navbar_title is not empty %}
                        <div class="navbar-header">
                            <span class="navbar-brand">{{ _navbar_title|raw }}</span>
                        </div>
                    {% endif %}
                {% endblock %}
                <div class="container-fluid">
                    <div class="navbar-left">
                        {% if _tab_menu is not empty %}
                            {{ _tab_menu|raw }}
                        {% endif %}
                    </div>

                    {% if _actions|replace({ '<li>': '', '</li>': '' })|trim is not empty %}
                        <ul class="nav navbar-nav navbar-right">
                            <li class="dropdown sonata-actions">
                                <a href="#" class="dropdown-toggle" data-toggle="dropdown">{{ 'link_actions'|trans({}, 'SonataAdminBundle') }} <b class="caret"></b></a>
                                <ul class="dropdown-menu" role="menu">
                                    {{ _actions|raw }}
                                </ul>
                            </li>
                        </ul>
                    {% endif %}
                </div>
            </nav>
        {% endif %}
    {% endblock sonata_page_content_nav %}
{% endblock sonata_page_content_header %}

您也应该能够为某些管理员执行此操作,仅覆盖为这些管理员定义特定模板的服务定义,例如:

app.admin.product:
        class: AppBundle\Admin\ProductAdmin
        arguments: [~, AppBundle\Entity\Product, AppBundle:Admin\Product]
        tags:
            - {name: sonata.admin, manager_type: orm, group: Products, label: Products}
        calls:
            - [ setTemplate, [edit, AppBundle:Product:edit.html.twig]]

但我无法获取 "AppBundle:Product:edit.html.twig" 模板来删除覆盖同一块的列表操作。

希望对您有所帮助。

我知道这是一个老问题,但我会留下这个答案以备将来参考。

我不得不做类似的事情,我就是这样做的。

在您的管理中class覆盖 configureActionButtons() 方法

class YourAdmin extends AbstractAdmin
{
    //...
    /**
     * Overriden from (AbstractAdmin)
     */
    public function configureActionButtons($action, $object = null)
    {
        $list = parent::configureActionButtons($action,$object);

        $list['custom_action'] = array(
                'template' =>  'AcmeBundle:YourAdmin:custom_button.html.twig',
        );

        return $list;
    }
    //...

}

然后在 AcmeBundle/Resources/views/YourAdmin/custom_button 中创建按钮。html.twig

{# custom_button.html.twig #}    
<a class="sonata-action-element" href="{{ admin.generateUrl('your_route') }}">
   <i class="fa fa-plus-circle"></i>Custom Action
</a>

当然你可以在之后添加权限检查(为了清楚起见,它们被省略了)

希望对大家有所帮助

在 SonataAdmin ~3 中,您可以配置模板,直接在 YourEntityAdmin::configureListFields() 方法中显示您的操作模板。

这样做you first

create new template anywhere, extend sonata layout and use sonata_admin_content block.

"Anywhere" 真正的意思是 "anywhere":起初我有点困惑。

基本上,您在 Resources\CRUD\list__action_your_action.html.twig 中创建一个模板,然后从 YourEntityAdmin::configureListFields() 中的配置中调用它:

class YourEntityAdmin
{
    protected function configureListFields(ListMapper $listMapper)
    {
        $listMapper

             // other fields...

            ->add('_action', null, array(
                'actions' => array(

                    // ...

                    'clone' => array(
                        'template' => 'AppBundle:CRUD:list__your_action.html.twig'
                    )
                )
            ))
        ;
    }
}

记得在这个模板中写一些东西,也可以是一个简单的字符串,比如

{# Resources\CRUD\list__action_your_action.html.twig #}
your action button template

"your action button template" 将呈现在 "Actions" 列中,基本上是按钮的标签:使用模板,您可以随意操作它。 这真的很难理解,因为在我的专栏中没有任何操作,因为我将模板留空所以没有渲染任何东西!

无论如何,您可以在有关 CREATING A CUSTOM ADMIN ACTION.

的文档中找到添加新按钮(以及关联的 route/action)的完整过程