Back to Top

How to use custom product datagrid in Akeneo without a repository

Updated 5 August 2024

Create a custom product data datagrid in Akeneo: Define columns & configure data source. Use Akeneo UI or YAML file in custom bundle. Integrate datagrid into Akeneo UI via custom tab or section. Using custom datagrids in Akeneo, combining the best aspects of UI and YAML configurations and PHP. A datagrid in Akeneo is a configurable UI component that displays tabular data.

Display the custom product data grid: Integrate the custom product data grid into the Akeneo UI by creating a custom tab or section within the product view page and rendering the grid there. Extend existing Akeneo views or create a custom UI extension.

Akeneo provides a REST API that allows you to connect external systems or custom applications’ product data. You can use the API to fetch product information from external sources and synchronize it with Akeneo.

DataGrid

datagrid:
    acme_testbundle-product-picker-grid:
        options:
            entityHint: product
            requireJSModules:
                - pim/datagrid/column-form-listener
            columnListener:
                dataField: identifier
                columnName: is_checked
            routerEnabled: false
        source:
            type:              acme_testbundle_datasource_product
            entity:            '%pim_catalog.entity.product.class%'
        columns:
            is_checked:
                frontend_type: boolean
                data_name:     is_checked
                editable:      true
            identifier:
                label: ID
                data_name:     identifier
                type:          field
            image:
                label:         Image
                data_name:     image
                frontend_type: product-and-product-model-image
            label:
                label:         Label
                data_name:     label
                type:          field
                frontend_type: product-and-product-model-label
            
        properties:
            id: ~
            technical_id: ~
            document_type: ~

Add DataSource Type

parameters:
    acme_testbundle_datagrid.datasource.product.class: Acme\TestBundle\Datasource\ProductDatasource # Pim\Bundle\DataGridBundle\Datasource\ProductDatasource #

services:  
    acme_testbundle_datagrid.datasource.product:
        class: '%acme_testbundle_datagrid.datasource.product.class%'
        arguments:
             - '@pim_catalog.object_manager.product'
             - '@pim_enrich.query.product_and_product_model_query_builder_from_size_factory'
              - '@pim_datagrid_serializer'
              - '@pim_datagrid.event_subscriber.filter_entity_with_values_subscriber'
        calls:
            - [ setMassActionRepository,   ['@pim_catalog.repository.product_mass_action'] ]
        tags:
            - { name: oro_datagrid.datasource, type: acme_testbundle_datasource_product }


DataSourceAdapterResolver

Start your headless eCommerce
now.
Find out More
pim_datagrid.datasource.adapter_resolver:<br>        class: '%pim_datagrid.datasource.adapter_resolver.class%'<br>        arguments:<br>            - '%pim_filter.datasource.orm_adapter.class%'<br>            - '%pim_filter.datasource.product_orm_adapter.class%'<br>        calls:<br>            - [addProductDatasource, ['pim_datasource_product']]<br>            - [addProductDatasource, ['pim_datasource_associated_product']]<br>            - [addProductDatasource, ['pim_datasource_associated_product_model']]<br>            - [addProductDatasource, ['pim_datasource_group_product']]<br>            - [addProductDatasource, ['wk_chatgpt_datasource_product']]<br>

Format data for data grid

namespace Acme\TestBundle\Datasource;

use Doctrine\Common\Persistence\ObjectManager;
use Oro\Bundle\DataGridBundle\Datasource\ResultRecord;
use Symfony\Component\Serializer\Normalizer\NormalizerInterface;
use Oro\Bundle\PimDataGridBundle\Datasource\ProductDatasource as BaseDataSource;
use Oro\Bundle\PimDataGridBundle\EventSubscriber\FilterEntityWithValuesSubscriberConfiguration;
use Akeneo\Pim\Enrichment\Component\Product\Query\ProductQueryBuilderFactoryInterface;
use Akeneo\Pim\Enrichment\Component\Product\Model\EntityWithValuesInterface;
use Oro\Bundle\PimDataGridBundle\EventSubscriber\FilterEntityWithValuesSubscriber;

/**
 * Product datasource, executes elasticsearch query
 *
 */
class ProductDatasource extends BaseDataSource
{
    /** @var ProductQueryBuilderInterface */
    protected $pqb;

    /** @var ProductQueryBuilderFactoryInterface */
    protected $factory;

    /** @var NormalizerInterface */
    protected $normalizer;

    /** @var FilterEntityWithValuesSubscriber */
    protected $filterEntityWithValuesSubscriber;

    /**
     * @param ObjectManager                       $om
     * @param ProductQueryBuilderFactoryInterface $factory
     * @param NormalizerInterface                 $serializer
     * @param FilterEntityWithValuesSubscriber    $filterEntityWithValuesSubscriber
     */
    public function __construct(
        ObjectManager $om,
        ProductQueryBuilderFactoryInterface $factory,
        NormalizerInterface $serializer,
        FilterEntityWithValuesSubscriber $filterEntityWithValuesSubscriber,
    ) {
        parent::__construct($om, $factory, $serializer, $filterEntityWithValuesSubscriber);
    }

    /**
     * {@inheritdoc}
     */
    public function getResults()
    {
        $productList = []; // set product list
        $productModelList = [] set product model list;
        
        $rows = ['data' =&gt; []];
        $entitiesWithValues =  $productList-&gt;getItems();
        $modelEntitiesWithValues =  $productModelList-&gt;getItems();
        
        foreach ($entitiesWithValues as $entityWithValue) {
            $normalizedItem = $this-&gt;normalizeEntityWithValues($entityWithValue);
            
            $rows['data'][] = new ResultRecord($normalizedItem);
        }

        foreach ($modelEntitiesWithValues as $modelEntityWithValue) {
            $normalizedItem = $this-&gt;normalizeEntityWithValues($modelEntityWithValue);
            $rows['data'][] = new ResultRecord($normalizedItem);
        }
        
        $rows['totalRecords'] = $productList-&gt;getCount() + $productModelList-&gt;getCount();
        
        
        return $rows;
    }

    /**
     * Normalizes an entity with values with the complete set of fields required to show it.
     *
     * @param array $item
     *
     * @return array
     */
    private function normalizeEntityWithValues($item): array
    {
        $productId = $this-&gt;getProductId($item);
        $normalizedItem = [
            "identifier" =&gt; $item['identifier'] ?? $item['code'],
            "family" =&gt; $item['family'],
            "groups" =&gt; "",
            "enabled" =&gt; $item['enabled'],
            "values" =&gt;  $item['value'],
            "created" =&gt; $item['created'],
            "updated" =&gt; $item['updated'],
            "label" =&gt; $item['identifier'] ?? $item['code'],
            "image" =&gt; null,
            "document_type" =&gt; "product",
            "technical_id" =&gt; $productId,
            "id" =&gt; $productId,
            "search_id" =&gt; "product_".$productId,
            "is_checked" =&gt; true,
          ] ;
        

        return $normalizedItem;
    }
}

Please be aware that the provided code is a simplified example and it may be necessary for you to modify and extend it according to your specific requirements and the structure of your custom data source.

Remember to adjust the namespace and class names in the example to match your project’s structure.

It’s worth noting that Akeneo provides a flexible and extensible framework, allowing you to adapt and customize the data sources based on your business needs. The data source selected is determined by the unique integration needs, the type of data to be imported, and the frequency of updates.

The best fo example for Create a custom product datagrid in Akeneo https://webkul.com/blog/akeneo-shareable-product-url-generator-documenation/

. . .

Leave a Comment

Your email address will not be published. Required fields are marked*


Be the first to comment.

Back to Top

Message Sent!

If you have more details or questions, you can reply to the received confirmation email.

Back to Home