Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

like image 302
user1548055 Avatar asked Jul 24 '12 09:07

user1548055


4 Answers

In your EntityAdmin class add following

public function configureRoutes(RouteCollection $collection)
{
  $collection->remove('create');
}
like image 95
chemalarrea Avatar answered Nov 07 '22 16:11

chemalarrea


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

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

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

//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;
        }
    }
...
}
like image 38
Indivision Dev Avatar answered Nov 07 '22 18:11

Indivision Dev


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
  • remove delete permissions for the object, depends what do you use for permissions
like image 41
WizardZ Avatar answered Nov 07 '22 17:11

WizardZ


Firstly use the class RouteCollection in your CustomClassAdmin :

use Sonata\AdminBundle\Route\RouteCollection;

and add the following code:

    public function configureRoutes(RouteCollection $collection)
    {
        $collection->remove('delete');
    }
like image 30
rapaelec Avatar answered Nov 07 '22 16:11

rapaelec