"Delete" action in list view
I think this feature could be very useful, instead of creating a new action for deletion in the list view. This is just a suggestion (but somewhere I'll need this feature), this feature could be optional but directly implemented in the bundle (like show and edit actions). Of course there would be a warning message before deletion like the one in the show/edit view.
In any case thanks again for your work!
I understand your suggestion. The problem is that adding a delete link for list items is very easy, but doing it completely right is pretty complex (you have to create a lot of delete forms, display the appropriate modal window to confirm the delete, etc.)
We'll add this action in the future. For now you can define a delete action for the listings of your entities and use a method or route of your own application:
easy_admin:
list:
actions:
- { name: 'deleteFromList', type: 'method', label: 'Delete', icon: 'trash' }
And then you define your own controller with the following method:
// src/AppBundle/Controller/AdminController.php
namespace AppBundle\Controller;
// ...
class AdminController extends BaseAdminController
{
// ...
public function deleteFromListAction()
{
$id = $this->request->query->get('id');
$entity = $this->em->getRepository('AppBundle:Product')->find($id);
$this->em->remove($entity);
$this->em->flush();
// redirect to the 'list' view of the given entity
return $this->redirectToRoute('admin', array(
'view' => 'list',
'entity' => $this->request->query->get('entity'),
));
}
}
Thanks for this answer, that's what I needed until delete action is released!
I encounter an error when I try to delete a "Client":
Attempted to call method "deleteFromListAction" on class "JavierEguiluz\Bundle\EasyAdminBundle\Controller\AdminController".
Here is my controller:
<?php
namespace Groupware\CreativeBundle\Controller;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use JavierEguiluz\Bundle\EasyAdminBundle\Controller\AdminController as BaseAdminController;
class AdminController extends BaseAdminController
{
public function deleteFromListAction()
{
$id = $this->request->query->get('id');
$entity = $this->em->getRepository('GroupwareCreativeBundle:Client')->find($id);
$this->em->remove($entity);
$this->em->flush();
// redirect to the 'list' view of the given entity
return $this->redirectToRoute('admin', array(
'view' => 'list',
'entity' => $this->request->query->get('entity'),
));
}
}
Moreover is there any way to make a global function for all entities? Because I have multiple entities in one bundle and I think making an AdminController for each entity is a bad idea. Thanks in advance!
Yep you can change the repository call to use the $this->entity['class'] instead of the Bundle:Entity shortcut :)
Wow, thanks for the tip! Sadly I'm getting this error again, don't know why:
Attempted to call method "deleteFromListAction" on class "JavierEguiluz\Bundle\EasyAdminBundle\Controller\AdminController".
@arukado-sama : Do you have the indexAction method in your controller ? :
/**
* @Route("/", name="admin")
*/
public function indexAction(Request $request)
{
return parent::indexAction($request);
}
and registered your controller and its routing annotations in routing.yml ? :
#routing.yml
app_easy_admin:
resource: @AppBundle/Controller/AdminController.php
type: annotation
prefix: /admin
I added the override of indexAction.
Here is my route for easy-admin:
admin:
resource: "@EasyAdminBundle/Controller/"
type: annotation
prefix: /admin
Do I have to create a new route for my bundle?
@arukado-sama this brief section of the documentation explains in detail what you have to do: https://github.com/javiereguiluz/EasyAdminBundle/blob/master/Resources/doc/tutorials/customizing-admin-controller.md#customization-based-on-controller-methods
@arukado-sama : Yes indeed. You need to tell the routing component that this new path for /admin exists and is now pointing to your own controller, not easyadmin default one.
@javiereguiluz : a section about routing is missing from this part of the documentation (loading the annotations or creating a single route to CustomController::indexAction).
It works now, didn't know this trick with annotation! Thanks for your help! I have a new issue now, the delete button isn't working (in the show and edit views), don't know why, but I'll investigate and open a new issue if I can't solve it.