Important

You are browsing documentation for version 5.0 of OroCommerce, supported until January 2025. Read the documentation for version 6.0 (the latest LTS version) to get up-to-date information.

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