7

I wrote a code to list data taken from a simple database and there I put an action to edit data. When I click on this Edit button, it goes to the default "Edit" page. There is a button called "delete" there. I want to remove that button...

4

4 回答 4

13

在您的 EntityAdmin 类中添加以下内容

public function configureRoutes(RouteCollection $collection)
{
  $collection->remove('create');
}
于 2012-09-19T08:06:24.567 回答
4

我需要从编辑表单中隐藏删除按钮,但不从列表中删除删除功能。

我就是这样做的......以防有人需要做类似的事情

第 1 步:将 SonataAdminBundle:CRUD:base_edit_form.html.twig 复制到您的包中,并根据需要注释掉代码/更新

//YourBundle/Resources/views/EntityAdmin/base_edit_form.html.twig
{% block form %}

...

    {#{% if admin.hasroute('delete') and admin.isGranted('DELETE', object) %}#}
    {#{{ 'delete_or'|trans({}, 'SonataAdminBundle') }}#}
    {#<a class="btn btn-danger" href="{{ admin.generateObjectUrl('delete', object) }}">{{ 'link_delete'|trans({}, 'SonataAdminBundle') }}</a>#}
    {#{% endif %}#}

...

{% endblock %}

第 2 步:添加新的视图资源 edit.html.twig 以扩展默认编辑模板

//YourBundle/Resources/views/EntityAdmin/edit.html.twig
{% extends 'SonataAdminBundle:CRUD:base_edit.html.twig' %}

{% use 'YourBundle:EntityAdmin:base_edit_form.html.twig' with form as parentForm %}

{% block form %}
    {{ block('parentForm') }}
{% endblock %}

第 3 步:更新您的 Admin 类以使用上述模板

//YourBundle/Admin/EntityAdmin.php
class EntityAdmin extends Admin{
...
    public function getTemplate($name)
    {
        switch ($name) {
            case 'edit':
                return 'SomeBundle:EntityAdmin:edit.html.twig';
                break;
            default:
                return parent::getTemplate($name);
                break;
        }
    }
...
}
于 2014-06-06T07:09:30.590 回答
3

base_edit_form.html.twig:

{% if admin.hasroute('delete') and admin.isGranted('DELETE', object) %}
    {% trans from 'SonataAdminBundle' %}delete_or{% endtrans %}
    <a class="btn danger" href="{{ admin.generateObjectUrl('delete', object) }}">{% trans from 'SonataAdminBundle' %}link_delete{% endtrans %}</a>
{% endif %}

我看到了 3 种方法来实现这一点:

  • 删除删除路线,它将从所有地方删除所有删除按钮,以供管理员使用
  • 在您的管理员中重新定义 hasroute 功能,它会产生相同的效果
  • 删除对象的删除权限,取决于您使用什么权限
于 2012-07-30T00:19:38.800 回答
1

首先在您的 CustomClassAdmin 中使用类 RouteCollection :

use Sonata\AdminBundle\Route\RouteCollection;

并添加以下代码:

    public function configureRoutes(RouteCollection $collection)
    {
        $collection->remove('delete');
    }

于 2019-06-14T14:35:23.180 回答