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
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
//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;
}
}
...
}
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:
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');
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With