【问题标题】:Force apply filter to sales order grid强制将过滤器应用于销售订单网格
【发布时间】:2019-02-20 20:38:04
【问题描述】:

我正在尝试按状态强制将文件管理器应用于特定用户角色的订单网格,因此他只能看到已处理的订单。 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();
    }

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

【问题讨论】:

    标签: magento magento2


    【解决方案1】:

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

    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();
       }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2018-06-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多