简体   繁体   English

如何在sonata admin的默认“编辑”形式中删除“删除”按钮?

[英]How to remove “delete” button in default “Edit” form of sonata admin?

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... 我想删除那个按钮......

In your EntityAdmin class add following 在您的EntityAdmin类中添加以下内容

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

I needed to hide the delete button from the edit form but not remove the delete functionality from the listing. 我需要隐藏编辑表单中的删除按钮,但不从列表中删除删除功能。

This is how I did it ... in case someone needs to do something similar 这就是我做的方式......如果有人需要做类似的事情

Step 1 : Copy SonataAdminBundle:CRUD:base_edit_form.html.twig into your bundle, and comment out code / update as required 第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 %}

Step 2 : Add a new view resource edit.html.twig to extend the default edit template 第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 %}

Step 3 : Update your Admin class to use the above template 第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;
        }
    }
...
}

base_edit_form.html.twig: 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 %}

I see 3 ways to make this happen: 我看到有三种方法可以实现这一目标:

  • remove delete route and it will remove all delete buttons from all places for give admin 删除删除路由,它将删除所有地方的所有删除按钮给管理员
  • redefine hasroute function in your admin, it will give the same effect 在您的管理员中重新定义hasroute功能,它将产生相同的效果
  • remove delete permissions for the object, depends what do you use for permissions 删除对象的删除权限,取决于您使用什么权限

Firstly use the class RouteCollection in your CustomClassAdmin : 首先在CustomClassAdmin中使用类RouteCollection:

 use Sonata\\AdminBundle\\Route\\RouteCollection; 

and add the following code: 并添加以下代码:

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

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

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