0

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

This is the code I have To add a new tab to order view

view/adminhtml/layout/sales_order_view.xml

<?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?

Edit: For the rest of the code if you want, you can checkout https://github.com/moloni/magento2

1 Answer 1

1

Add datasource to di.xml

<item name="sales_order_view_document_grid_data_source" xsi:type="string">Invoicing\Moloni\Model\ResourceModel\Orders\Collection</item>

Now looks like:

<type name="Magento\Framework\View\Element\UiComponent\DataProvider\CollectionFactory">
    <arguments>
        <argument name="collections" xsi:type="array">
            <item name="moloni_orders_grid_data_source" xsi:type="string">Invoicing\Moloni\Model\ResourceModel\Orders\Collection</item>
            <item name="sales_order_view_document_grid_data_source" xsi:type="string">Invoicing\Moloni\Model\ResourceModel\Documents\Grid\Collection</item>
        </argument>
    </arguments>
</type>

Create app/code/Invoicing/Moloni/Model/ResourceModel/Documents/Grid/Collection.php

<?php
namespace Invoicing\Moloni\Model\ResourceModel\Documents\Grid;

use Magento\Framework\View\Element\UiComponent\DataProvider\SearchResult;
use Magento\Framework\Data\Collection\Db\FetchStrategyInterface as FetchStrategy;
use Magento\Framework\Data\Collection\EntityFactoryInterface as EntityFactory;
use Magento\Framework\Event\ManagerInterface as EventManager;
use Psr\Log\LoggerInterface as Logger;

class Collection extends SearchResult
{
    /**
     * Initialize dependencies.
     *
     * @param EntityFactory $entityFactory
     * @param Logger $logger
     * @param FetchStrategy $fetchStrategy
     * @param EventManager $eventManager
     * @param string $mainTable
     * @param string $resourceModel
     */
    public function __construct(
        EntityFactory $entityFactory,
        Logger $logger,
        FetchStrategy $fetchStrategy,
        EventManager $eventManager,
        $mainTable = 'moloni_documents',
        $resourceModel = \Invoicing\Moloni\Model\ResourceModel\Documents::class
    ) {
        parent::__construct($entityFactory, $logger, $fetchStrategy, $eventManager, $mainTable, $resourceModel);
    }
}

Modify app/code/Invoicing\Moloni\Ui\DataProvider\OrderDocumentsProvider

<?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;
use Magento\Framework\View\Element\UiComponent\DataProvider\DataProvider;
use Magento\Framework\Api\FilterBuilder;
use Magento\Framework\Api\Search\ReportingInterface;
use Magento\Framework\Api\Search\SearchCriteria;
use Magento\Framework\Api\Search\SearchCriteriaBuilder;
use Magento\Framework\Api\Search\SearchResultInterface;
use Magento\Framework\App\RequestInterface;

class OrderDocumentsProvider extends DataProvider
{
    /**
     * @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;

    /**
     * OrderDocumentsProvider constructor.
     *
     * @param string $name
     * @param string $primaryFieldName
     * @param string $requestFieldName
     * @param ReportingInterface $reporting
     * @param SearchCriteriaBuilder $searchCriteriaBuilder
     * @param RequestInterface $request
     * @param FilterBuilder $filterBuilder
     * @param OrderRepositoryInterface $orderRepository
     * @param Moloni $moloni
     * @param array $meta
     * @param array $data
     */
    public function __construct(
        $name,
        $primaryFieldName,
        $requestFieldName,
        ReportingInterface $reporting,
        SearchCriteriaBuilder $searchCriteriaBuilder,
        RequestInterface $request,
        FilterBuilder $filterBuilder,
        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, $reporting, $searchCriteriaBuilder, $request, $filterBuilder, $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)
    {
    }
}

Output looks like: enter image description here

1
  • Amazing! :) That solved my problem and you helped me finish this module! Thank you a lot! :) Commented Jul 31, 2019 at 21:57

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.