ssossossosso
Documentation Home »The Oro Cookbook »Entities »How to Delete Tasks
1.10 version

How to Delete Tasks

The data grid that you created so far is able to let the user delete entries without having to reload the entire page. To achieve this you have to follow these three steps:

  1. Configure an API manager service.
  2. Create a controller that deletes a task.
  3. Register a route for the created controller.
  4. Link to the controller from the data grid.

Configure the API Manager Service

Fortunately, the OroSoapBundle comes with the oro_soap.manager.entity_manager.abstract service that is based on the ApiEntityManager class and is able to delete any entity managed by Doctrine. You just have to configure a concrete service that gets the entity’s fully qualified class name and the entity manager service passed as arguments:

1
2
3
4
5
6
7
services:
    app.task_manager.api:
        class: Oro\Bundle\SoapBundle\Entity\Manager\ApiEntityManager
        parent: oro_soap.manager.entity_manager.abstract
        arguments:
            - AppBundle\Entity\Task
            - '@doctrine.orm.entity_manager'

Create the Controller

Creating the controller that is responsible to delete a task is easy as you can extend the RestController class from the OroSoapBundle which comes with basic features:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
// src/AppBundle/Controller/Api/Rest/TaskController.php
namespace AppBundle\Controller\Api\Rest;

use FOS\RestBundle\Controller\Annotations\NamePrefix;
use FOS\RestBundle\Controller\Annotations\RouteResource;
use Oro\Bundle\SoapBundle\Controller\Api\Rest\RestController;

/**
 * @RouteResource("task")
 * @NamePrefix("app_api_")
 */
class TaskController extends RestController
{
    public function deleteAction($id)
    {
        return $this->handleDeleteRequest($id);
    }

    public function getForm()
    {
        // This method is not needed to delete entities.
        //
        // Note: You will need to provide a proper implementation here
        // when you start working with more features of REST APIs.
    }

    public function getFormHandler()
    {
        // This method is not needed to delete entities.
        //
        // Note: You will need to provide a proper implementation here
        // when you start working with more features of REST APIs.
    }

    public function getManager()
    {
        return $this->get('app.task_manager.api');
    }
}

Note

The name of action method is important. The FOSRestBundle will use it to ensure that the route will only be matched when DELETE requests are issued (which are dispatched by the OroPlatform when a row is to be deleted).

Register the Routes

You now need to make sure that your controller is processed by the route loader of the FOSRestBundle which automatically creates and registers routes based on the method names of your API controller:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
# src/AppBundle/Resources/config/oro/routing.yml
app.api.task:
    resource: '@AppBundle/Controller/Api/TaskController.php'
    type: rest
    prefix: api/rest/{version}/
    requirements:
        version: latest|v1
        _format:  json
    defaults:
        version: latest

Update the Data Grid Configuration

To make it possible to remove tasks you need to define a property that describes how the URL of you REST API controller action is built and then add this URL to the list of available actions in your data grid configuration:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
# src/AppBundle/Resources/config/oro/datagrids.yml
datagrids:
    app-tasks-grid:
        properties:
            id: ~
            delete_link:
                type: url
                route: app_api_delete_task
                params:
                    - id
            # ...
        actions:
            # ...
            delete:
                type: delete
                label: Delete
                link: delete_link
                icon: trash

Note

It is important to use the delete value for the type option so that the OroPlatform issues an HTTP DELETE request when the trash bin icon is clicked (instead of a regular GET request).

Browse maintained versions:
current1.102.02.32.4