cancel
Showing results for 
Search instead for 
Did you mean: 

Get order_id in UI Component GRID in a new Sales View Tab

Get order_id in UI Component GRID in a new Sales View Tab

After a lot of tries, I couldn't manage to get an order_id to a Admin Grid Dataprovider.

This is the code I have

view/adminhtml/layout/sales_order_view.xml - To add a new tab to order view

<?xml version="1.0"?>
    <page xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" layout="admin-2columns-left" xsi:noNamespaceSchemaLocation="urn:magento:framework:View/Layout/etc/page_configuration.xsd">
        <body>
            <referenceContainer name="left">
                <referenceBlock name="sales_order_tabs">
                    <block class="Invoicing\Moloni\Block\Adminhtml\Order\View\Tab\Documents" name="sales_order_documents.grid.container"/>
                    <action method="addTabAfter">
                        <argument name="name" xsi:type="string">moloni_documents</argument>
                        <argument name="block" xsi:type="string">sales_order_documents.grid.container</argument>
                        <argument name="after" xsi:type="string">order_invoices</argument>
                    </action>
                </referenceBlock>
            </referenceContainer>
            <referenceBlock name="sales_order_documents.grid.container">
                <uiComponent name="orders_documents_grid"/>
            </referenceBlock>
        </body>
    </page>

view/adminhtml/ui_components/orders_documents_grid.xml

<?xml version="1.0" encoding="UTF-8"?>
<listing xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:module:Magento_Ui:etc/ui_configuration.xsd">
    <argument name="data" xsi:type="array">
        <item name="js_config" xsi:type="array">
            <item name="provider" xsi:type="string">orders_documents_grid.orders_documents_grid_data_source</item>
        </item>
    </argument>
    <settings>
        <spinner>orders_documents_grid_columns</spinner>
        <deps>
            <dep>orders_documents_grid.orders_documents_grid_data_source</dep>
        </deps>
    </settings>
    <dataSource name="orders_documents_grid_data_source" component="Magento_Ui/js/grid/provider">
        <settings>
            <filterUrlParams>
                <param name="order_id">*</param>
            </filterUrlParams>
            <updateUrl path="mui/index/render"/>
        </settings>
        <dataProvider class="Invoicing\Moloni\Ui\DataProvider\OrderDocumentsProvider" name="orders_documents_grid_data_source">
            <settings>
                <requestFieldName>id</requestFieldName>
                <primaryFieldName>entity_id</primaryFieldName>
            </settings>
        </dataProvider>
    </dataSource>

    <columns name="orders_documents_grid_columns">
        <column name="entity_name">
            <settings>
                <filter>text</filter>
                <label translate="true">Nome</label>
            </settings>
        </column>

        <column name="entity_vat">
            <settings>
                <filter>text</filter>
                <label translate="true">Contribuinte</label>
            </settings>
        </column>
    </columns>
</listing>

Ui/DataProvider/OrderDocumentsProvider.php

<?php
/**
 * Created by PhpStorm.
 * User: Nuno
 * Date: 30/07/2019
 * Time: 16:20
 */

namespace Invoicing\Moloni\Ui\DataProvider;

use Invoicing\Moloni\Libraries\MoloniLibrary\Moloni;
use Magento\Ui\DataProvider\AbstractDataProvider;
use Magento\Framework\App\Request\Http;
use Magento\Sales\Api\OrderRepositoryInterface;

class OrderDocumentsProvider extends AbstractDataProvider
{
    /**
     * @var \Magento\Ui\DataProvider\AddFieldToCollectionInterface[]
     */
    protected $addFieldStrategies;

    /**
     * @var \Magento\Ui\DataProvider\AddFilterToCollectionInterface[]
     */
    protected $addFilterStrategies;

    /**
     * @var Http
     */
    protected $request;

    /**
     * @var OrderRepositoryInterface
     */
    protected $orderRepository;

    /**
     * @var Moloni
     */
    private $moloni;

    /**
     * Construct
     *
     * @param string $name
     * @param string $primaryFieldName
     * @param string $requestFieldName
     * @param Http $request
     * @param array $meta
     * @param array $data
     */
    public function __construct(
        $name,
        $primaryFieldName,
        $requestFieldName,
        Http $request,
        OrderRepositoryInterface $orderRepository,
        Moloni $moloni,
        array $meta = [],
        array $data = []
    )
    {
        $this->request = $request;
        $this->moloni = $moloni;
        $this->orderRepository = $orderRepository;
        $this->data = $data;
        parent::__construct($name, $primaryFieldName, $requestFieldName, $meta, $data);
    }

    /**
     * @return \Magento\Sales\Api\Data\OrderInterface
     */
    public function getOrder()
    {
        $orderId = $this->request->getParam("order_id");
        return $this->orderRepository->get($orderId);
    }

    /***
     * @return array
     */
    public function getData()
    {
        $documentsList = [
            [
                'entity_name' => print_r($this->request->getParams(), true),
                'entity_vat' => print_r($this->data, true)
            ]
        ];

        return [
            'totalRecords' => count($documentsList),
            'items' => $documentsList,
        ];
    }

    public function setLimit($offset, $size)
    {
    }

    public function addOrder($field, $direction)
    {
    }

    public function addFilter(\Magento\Framework\Api\Filter $filter)
    {
    }
}

Whenever I try to open the sales order view, all other tabs load the url like this:

https://magento.retronwarz.com/admin/mui/index/render/key/0d7cba1af800b031dce791267facdc34b5a062f3932738f45e74225339a629e8/order_id/8/?namespace=sales_order_view_invoice_grid

But, when I try to load my tab, the param order_id is not sent, so it looks like:

https://magento.retronwarz.com/admin/mui/index/render/key/0d7cba1af800b031dce791267facdc34b5a062f3932738f45e74225339a629e8/?namespace=orders_documents_grid&isAjax=true

Does anyone know how I can overcome this and send the order_id to the tab URL?