Important

You are browsing the documentation for version 3.1 of OroCommerce, OroCRM and OroPlatform, which is no longer maintained. Read version 5.1 (the latest LTS version) of the Oro documentation to get up-to-date information.

See our Release Process documentation for more information on the currently supported and upcoming releases.

Pass Request Parameters to the Grid

In some cases, you need to pass parameters from the request to the grid. This task may be solved using grid event listeners. An existing listener implementation also could be used.

Grid Configuration

Suppose that you have a grid configuration and a named parameter inside where clause of its source query. For example:

 1# src/Acme/Bundle/TaskBundle/Resources/config/oro/datagrids.yml
 2datagrids:
 3    # ...
 4    acme-tasks-grid:
 5        # ...
 6        source:
 7            query:
 8                where:
 9                    and:
10                    - task.relatedContact = :contactId
11        # ...
12    # ...

Our goal is to set :contactId parameter with the value from the request.

Solution 1: Grid Parameter Binding

The easiest way that should be sufficient for most situations is to use the parameter binding option of the datasource to configure mapping between datagrid and query parameters.

You can do this by adding the bind_parameters option to your datagrids.yml using the following syntax:

 1# src/Acme/Bundle/TaskBundle/Resources/config/oro/datagrids.yml
 2datagrids:
 3    # ...
 4    acme-tasks-grid:
 5        # ...
 6        source:
 7            query:
 8                where:
 9                    and:
10                    - task.relatedContact = :contactId
11            bind_parameters:
12                # Get the "contactId" parameter from the datagrid
13                # and set its value to the "contactId" parameter in the datasource query
14                - contactId
15        # ...
16    # ...

In case if names of the parameter in the grid and the query do not match you can pass associative array of parameters, where the key will be the name of the query parameter, and the value will match the name of the parameter in the grid:

 1# src/Acme/Bundle/TaskBundle/Resources/config/oro/datagrids.yml
 2datagrids:
 3    # ...
 4    acme-tasks-grid:
 5        # ...
 6        source:
 7            query:
 8                where:
 9                    and:
10                    - task.relatedContact = :contactId
11            bind_parameters:
12                # Get the "relatedContactId" parameter from the datagrid
13                # and set its value to the "contactId" parameter in the datasource query
14                contactId: relatedContactId
15        # ...
16    # ...

Caution

A datasource must implement the Oro\Bundle\DataGridBundle\Datasource\ParameterBinderAwareInterface to support the bind_parameters option.

Now we need to pass the parameter with name “relatedContactId” to our grid. The controller receives a contact entity and passes it to the view:

 1<?php
 2    // src/Acme/Bundle/TaskBundle/Controller/TaskController.php
 3    namespace Acme\Bundle\TaskBundle\Controller;
 4
 5    use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
 6    use Sensio\Bundle\FrameworkExtraBundle\Configuration\Template;
 7
 8    use Symfony\Bundle\FrameworkBundle\Controller\Controller;
 9
10    use Oro\Bundle\ContactBundle\Entity\Contact;
11
12    class TaskController extends Controller
13    {
14        // ...
15
16        /**
17         * @Route("/contact/{id}/tasks", name="acme_task_contact_tasks", requirements={"id"="\d+"})
18         * @Template
19         */
20        public function contactTasksAction(Contact $contact)
21        {
22            return ['contact' => $contact];
23        }
24
25        // ...
26    }

The view passes the “relatedContactId” parameter to the grid:

1{# src/Acme/Bundle/TaskBundle/Resources/views/Task/contactTasks.html.twig #}
2{% import 'OroDataGridBundle::macros.html.twig' as dataGrid %}
3
4<div class="widget-content">
5    {{ dataGrid.renderGrid('acme-tasks-grid', {relatedContactId: contact.id}) }}
6</div>

Solution 2. Create Custom Event Listener

If the first example does not work for you for some reason (datasource does not support parameters binding, you need to implement additional logic before binding parameters, etc.), you can create a listener for the oro_datagrid.datagrid.build.after event and set the parameter for the source query in this listener:

 1<?php
 2// src/Acme/Bundle/TaskBundle/EventListener/ParameterListener.php
 3namespace Acme\Bundle\TaskBundle\EventListener;
 4
 5use Doctrine\ORM\QueryBuilder;
 6
 7use Oro\Bundle\DataGridBundle\Datasource\Orm\OrmDatasource;
 8use Oro\Bundle\DataGridBundle\Event\BuildAfter;
 9
10class ParameterListener
11{
12    protected $parameterName;
13    protected $requestParameterName;
14
15    public function __construct($parameterName, $requestParameterName = null)
16    {
17        $this->parameterName = $parameterName;
18        $this->requestParameterName = $requestParameterName ? $requestParameterName : $this->parameterName;
19    }
20
21    public function onBuildAfter(BuildAfter $event)
22    {
23        $datagrid   = $event->getDatagrid();
24        $datasource = $datagrid->getDatasource();
25        $parameters = $datagrid->getParameters();
26
27        if ($datasource instanceof OrmDatasource) {
28            /** @var QueryBuilder $queryBuilder */
29            $queryBuilder = $datasource->getQueryBuilder();
30
31            $queryBuilder->setParameter($this->parameterName, $parameters->get($this->requestParameterName));
32        }
33    }
34}

Register this listener in the container:

1# src/Acme/Bundle/TaskBundle/Resources/config/services.yml
2services:
3    acme_task.event_listener.acme_tasks_grid_parameter_listener:
4        class: Acme\Bundle\TaskBundle\EventListener\ParameterListener
5        arguments:
6            - contactId
7        tags:
8            - { name: kernel.event_listener, event: oro_datagrid.datagrid.build.after.acme-tasks-grid, method: onBuildAfter }

Remember that you still need to pass the relatedContactId parameter to the grid to the grid from a Twig template Remember that you still need to pass the relatedContactId parameter to the grid to the grid from a Twig template (see example in the previous solution).

References