0

i am using a plugin to filter products on admin panel by product attribute, but got no results. here is my plugin code.

<?php

namespace Learning\HelloPage\Plugin;

class ProductDataProvider
{
      public function aftergetData(\Magento\Catalog\Ui\DataProvider\Product\ProductDataProvider $subject
)
        {
            if (!$subject->getCollection()->isLoaded()) {
                echo 'jhsdg';
                $subject->getCollection()->addFieldToFilter('admin_user_id', array('eq' => '2')); //replace with dynamic admin user from session
                $subject->getCollection()->load();
                echo 'sghd';
            }
        }
}

and my etc/di.xml file

<?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\Catalog\Ui\DataProvider\Product\ProductDataProvider">
        <plugin name="productdata" type="Learning\HelloPage\Plugin\ProductDataProvider" />
    </type>

</config>

what am i doing wrong? please suggest any solution

1 Answer 1

2

Change method name aftergetData to afterGetData

[Update]

I think around plugin is best for you. Try following way using around plugin:


class ProductDataProvider
{
    public function aroundGetData(
        \Magento\Catalog\Ui\DataProvider\Product\ProductDataProvider $subject,
        \Closure $proceed
    ) {
        if (!$subject->getCollection()->isLoaded()) {
            $subject->getCollection()->addFieldToFilter('admin_user_id', array('eq' => '2'));
            $subject->getCollection()->load();
        }

        return $proceed();
    }
}

Try after plugin following way:


class ProductDataProvider
{
    public function afterGetData(
        \Magento\Catalog\Ui\DataProvider\Product\ProductDataProvider $subject,
        $result
    ) {
        if (!$subject->getCollection()->isLoaded()) {
            $subject->getCollection()->addFieldToFilter('admin_user_id', array('eq' => '2'));
            $subject->getCollection()->load();
        }

        $items = $subject->getCollection()->toArray();

        return [
            'totalRecords' => $subject->getCollection()->getSize(),
            'items' => array_values($items),
        ];
    }
}

Don't use any echo/print inside this function. Try error log or magento log for testing your plugin works or not.

For example:


class ProductDataProvider
{
    /**
     * @var \Psr\Log\LoggerInterface
     */
    protected $logger;

    /**
     * ProductDataProvider constructor.
     *
     * @param \Psr\Log\LoggerInterface $logger
     */
    public function __construct(
        \Psr\Log\LoggerInterface $logger
    ) {
        $this->logger = $logger;
    }

    /**
     * @param \Magento\Catalog\Ui\DataProvider\Product\ProductDataProvider $subject
     * @param $result
     * @return array
     */
    public function afterGetData(
        \Magento\Catalog\Ui\DataProvider\Product\ProductDataProvider $subject,
        $result
    ) {
        if (!$subject->getCollection()->isLoaded()) {
            $subject->getCollection()->addFieldToFilter('admin_user_id', array('eq' => '2'));
            $subject->getCollection()->load();
        }
        $this->logger->info('TEST');
        $items = $subject->getCollection()->toArray();

        return [
            'totalRecords' => $subject->getCollection()->getSize(),
            'items' => array_values($items),
        ];
    }
}

Magento log file location: Magento Root /var/log/system.log

Clear cache.

5
  • hello sir, still i got nothing it keeps loading, but got no results
    – Jay Kapoor
    Commented Sep 5, 2017 at 8:15
  • @Sohel It should be aftergetData, as original method name is getData
    – Keyur Shah
    Commented Sep 5, 2017 at 11:13
  • @JayKapoor check updated answer.
    – Sohel Rana
    Commented Sep 5, 2017 at 11:51
  • @KeyurShah yes, that's why first character should be uppercase. Not aftergetData, should be afterGetData.
    – Sohel Rana
    Commented Sep 5, 2017 at 11:53
  • Yes, I am wrong, :) +1 from me @SohelRana
    – Keyur Shah
    Commented Sep 5, 2017 at 12:25

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.