Important

You are browsing documentation for version 5.0 of OroCommerce, OroCRM, and OroPlatform, maintained until August 2024 and supported until March 2026. See version 5.1 (the latest LTS version) of the Oro documentation for information on latest features.

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

Array Datasource

This datasource provides the ability to set data for the datagrid from the array.

Example

datagrids:
    DATAGRID_NAME_HERE:
        source:
            type: array

Configuration

To configure datasource, create a datagrid event listener and subscribe to the oro_datagrid.datagrid.build.after.DATAGRID_NAME_HERE event.

acme_bundle.event_listener.datagrid.my_custom_listener:
    class: Acme\Bundle\AcmeBundle\EventListener\Datagrid\MyCustomListener
    tags:
        - { name: kernel.event_listener, event: oro_datagrid.datagrid.build.after.DATAGRID_NAME_HERE, method: onBuildAfter }
namespace Acme\Bundle\AcmeBundle\EventListener\Datagrid;

use Oro\Bundle\DataGridBundle\Datasource\ArrayDatasource\ArrayDatasource;
use Oro\Bundle\DataGridBundle\Event\BuildAfter;
use Oro\Bundle\DataGridBundle\Exception\UnexpectedTypeException;

class MyCustomListener
{
    public function onBuildAfter(BuildAfter $event)
    {
        $datagrid = $event->getDatagrid();
        $datasource = $datagrid->getDatasource();

        if (!$datasource instanceof ArrayDatasource) {
            throw new UnexpectedTypeException($datasource, ArrayDatasource::class);
        }

        // Create datagrid source array
        $source = [
            // row 1
            [
                'first_column' => 'Value in first row and first column',
                'second_column' => 'Value in first row and second column'
            ],
            // row 2
            [
                'first_column' => 'Value in second row and first column',
                'second_column' => 'Value in second row and second column'
            ],
            // ...
        ];

        $datasource->setArraySource($source);
    }
}

Predefined columns can be defined using the following configuration:

datagrids:
    DATAGRID_NAME_HERE:
        source:
            type: array
        columns:
            first_column:
                label: Column 1 Label
        sorters:
            columns:
                first_column:
                    data_name: first_column

Related Articles