强制将过滤器应用于销售订单网格

Force apply filter to sales order grid

我正在尝试强制按状态申请文件管理器以针对特定用户的角色订购网格,因此他只能看到已处理的订单。 the same of image, 我在这段代码中得到了角色名称:

class OrderDataProvider extends \Magento\Ui\DataProvider\AbstractDataProvider
{
    public function __construct(
        Action\Context $context, /* object of "Magento\Backend\App\Action" */
        $name,
        $primaryFieldName,
        $requestFieldName,
        CollectionFactory $collectionFactory,
        \Magento\Sales\Model\ResourceModel\Order\CollectionFactory  $orderCollection,
        array $addFieldStrategies = [],
        array $addFilterStrategies = [],
        array $meta = [],
        array $data = []
    ) {
        parent::__construct($name, $primaryFieldName, $requestFieldName, $meta, $data);

        $auth= $context->getAuth();
        $loginUser=$auth->getUser();
        $loginUserRole=$loginUser->getRole();
    }

我想为这个角色应用过滤器。

如果没有创建,请创建一个自定义模块。

app/code/[VendorName]/[ModuleName]/etc/di.xml

<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
    <type name="Magento\Framework\View\Element\UiComponent\DataProvider\CollectionFactory">
        <arguments>
            <argument name="collections" xsi:type="array">
                <item name="sales_order_grid_data_source" xsi:type="string">[VendorName]\[ModuleName]\Model\ResourceModel\Order\Grid\Collection</item>
            </argument>
        </arguments>
    </type>
</config>

app/code/[VendorName]/[ModuleName]/Model/ResourceModel/Order/Grid/Collection.php

<?php

namespace [VendorName]\[ModuleName]\Model\ResourceModel\Order\Grid;

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;

use Magento\Sales\Model\ResourceModel\Order\Grid\Collection as OriginalCollection;

/**
 * Order grid extended collection
 */
class Collection extends OriginalCollection
{
    /**
     * @var \Magento\Backend\Model\Auth\Session
     */
    protected $_adminSession;

    public function __construct(
        EntityFactory $entityFactory,
        Logger $logger,
        FetchStrategy $fetchStrategy,
        EventManager $eventManager,
        \Magento\Backend\Model\Auth\Session $adminSession,
        $mainTable = 'sales_order_grid',
        $resourceModel = \Magento\Sales\Model\ResourceModel\Order::class
    ) {
        $this->_adminSession = $adminSession;
        parent::__construct($entityFactory, $logger, $fetchStrategy, $eventManager, $mainTable, $resourceModel);
    }
   protected function _renderFiltersBefore()
   {
        $roleData = $this->_adminSession->getUser()->getRole()->getData();
        if ($roleData['role_name'] == '[Name of your role here]') {
            $this->getSelect()->where('status = "processing"');
        }
        parent::_renderFiltersBefore();
   }
}