Reputation: 11
First I'm not a hardcore coder in Magento 2, so most likely I'm missing something. I did search the web but it seems that I can't find the solution. Hopefully somebody can help me here.
The situation I have created a custom module and I have a custom controller that must pull data from a custom table. To do this I have created a custom UI DataProvider that should pull the data. When I access the controller the params in the url are providing an internal ID. When I retreive this ID and create a simple search I do not get any values in my table. In this case the ID is 59. When I hardcode the ID in the search I do get results in my table.
I tested with some debug print_r outputs the query and the items pulled and in both cases the data is present before returning the data in the function getData(). So at this point I really do not understand why it works when I hardcode the value 59, but it doesn't when I retreive it with getParam.
I expect that maybe in some way I'm using the collection incorrect. If I add the same filter in the parent:: then I get an error and its seems its using the Magento product collection. The section where it goes wrong is:
$items = $this->purchaseOrderItemCollection
->addFieldToFilter('purchase_order_id', array('eq' => $id))
//->addFieldToFilter('purchase_order_id', array('eq' => 59))
->getData()
;
If I comment the addFieldToFilter with the $id and uncomment the line with hardcode 59 it works. I also tried here to set the value $id through the registery because maybe I thought Magento does not remember the data somewhere else, but this did not resolve the issue.
Below my controller.
<?php
namespace DssSolutions\Management\Ui\DataProvider\PurchaseOrderItem;
use DssSolutions\Management\Api\PurchaseOrderSalesOrderItemRepositoryInterface;
use DssSolutions\Management\Model\PurchaseOrderSalesOrderItem\DataProvider;
use DssSolutions\Management\Model\ResourceModel\PurchaseOrderItem;
use DssSolutions\Management\Model\ResourceModel\PurchaseOrderSalesOrderItem;
use DssSolutions\Management\Model\ResourceModel\PurchaseOrdersLines;
use Magento\Catalog\Model\ResourceModel\Product\CollectionFactory;
use Magento\Framework\App\ObjectManager;
use Magento\Store\Model\Store;
use Magento\Ui\DataProvider\Modifier\ModifierInterface;
use Magento\Ui\DataProvider\Modifier\PoolInterface;
/**
* Class ProductDataProvider
*
* @api
* @since 100.0.2
*/
class ReceiveOrderedProductDataProvider extends \Magento\Ui\DataProvider\AbstractDataProvider
{
/**
* Product collection
*
* @var \DssSolutions\Management\Model\ResourceModel\PurchaseOrderItem\Collection
*/
protected $collection;
/**
* @var \Magento\Ui\DataProvider\AddFieldToCollectionInterface[]
*/
protected $addFieldStrategies;
/**
* @var \Magento\Ui\DataProvider\AddFilterToCollectionInterface[]
*/
protected $addFilterStrategies;
/**
* @var PoolInterface
*/
private $modifiersPool;
/**
* @var \Magento\Framework\App\Request\Http
*/
protected $request;
/**
* @var PurchaseOrderItem\Collection
*/
protected $purchaseOrderItemCollection;
/**
* @var \Magento\Framework\Registry
*/
protected $_registry;
/**
* @param string $name
* @param string $primaryFieldName
* @param string $requestFieldName
* @param CollectionFactory $collectionFactory
* @param \Magento\Ui\DataProvider\AddFieldToCollectionInterface[] $addFieldStrategies
* @param \Magento\Ui\DataProvider\AddFilterToCollectionInterface[] $addFilterStrategies
* @param array $meta
* @param array $data
* @param PoolInterface|null $modifiersPool
*/
public function __construct(
$name,
$primaryFieldName,
$requestFieldName,
CollectionFactory $collectionFactory,
\Magento\Framework\App\Request\Http $request,
\Magento\Framework\Registry $registry,
\DssSolutions\Management\Model\ResourceModel\PurchaseOrderItem\Collection $purchaseOrderItemCollection,
array $addFieldStrategies = [],
array $addFilterStrategies = [],
array $meta = [],
array $data = [],
PoolInterface $modifiersPool = null
) {
parent::__construct($name, $primaryFieldName, $requestFieldName, $meta, $data);
$this->request = $request;
$this->_registry = $registry;
$this->purchaseOrderItemCollection = $purchaseOrderItemCollection;
$this->collection = $collectionFactory->create();
$this->addFieldStrategies = $addFieldStrategies;
$this->addFilterStrategies = $addFilterStrategies;
$this->modifiersPool = $modifiersPool ?: ObjectManager::getInstance()->get(PoolInterface::class);
}
/**
* Get data
*
* @return array
*/
public function getData()
{
if (!$this->getCollection()->isLoaded()) {
$this->getCollection()->load();
}
$this->_registry->register('current_purchase_order', $this->request->getParam('purchase_id', null));
$id = $this->_registry->registry('current_purchase_order');
$items = $this->purchaseOrderItemCollection
->addFieldToFilter('purchase_order_id', array('eq' => $id))
//->addFieldToFilter('purchase_order_id', array('eq' => 59))
->getData()
;
$collectionCounter = 1;
foreach ($items as $k => $product){
$items[$k]['qty_receivable'] = ($product['qty_orderred'] - $product['qty_received']);
$collectionCounter++;
}
$data = [
'totalRecords' => $collectionCounter,
'items' => array_values($items),
];
/** @var ModifierInterface $modifier */
foreach ($this->modifiersPool->getModifiersInstances() as $modifier) {
$data = $modifier->modifyData($data);
}
return $data;
}
/**
* Add field to select
*
* @param string|array $field
* @param string|null $alias
* @return void
*/
public function addField($field, $alias = null)
{
if (isset($this->addFieldStrategies[$field])) {
$this->addFieldStrategies[$field]->addField($this->getCollection(), $field, $alias);
} else {
parent::addField($field, $alias);
}
}
/**
* @inheritdoc
*/
public function addFilter(\Magento\Framework\Api\Filter $filter)
{
if (isset($this->addFilterStrategies[$filter->getField()])) {
$this->addFilterStrategies[$filter->getField()]
->addFilter(
$this->getCollection(),
$filter->getField(),
[$filter->getConditionType() => $filter->getValue()]
);
} else {
parent::addFilter($filter);
}
}
/**
* @inheritdoc
* @since 103.0.0
*/
public function getMeta()
{
$meta = parent::getMeta();
/** @var ModifierInterface $modifier */
foreach ($this->modifiersPool->getModifiersInstances() as $modifier) {
$meta = $modifier->modifyMeta($meta);
}
return $meta;
}
}
Upvotes: 0
Views: 4178
Reputation: 11
After a lot of investigation I found out that the UI DataProvider is subjected to change and that URL Params aren't always submitted and this is because the UI Grid is using ajax and the route mui/index/render.
So to get this working the datasource had to be adjusted to it includes an item that sumbits the url params. This is done with the following code in my example.
<item name="filter_url_params" xsi:type="array">
<item name="purchase_order_id" xsi:type="string">*</item>
</item>
In the DataProvider then add the following code:
/** * @return void */protected function prepareUpdateUrl()
{
if (!isset($this->data['config']['filter_url_params'])) {
return;
}
foreach ($this->data['config']['filter_url_params'] as $paramName => $paramValue) {
if ('*' == $paramValue) {
$paramValue = $this->request->getParam($paramName);
}
if ($paramValue) {
$this->data['config']['update_url'] = sprintf(
'%s%s/%s/',
$this->data['config']['update_url'],
$paramName,
$paramValue
);
$this->addFilter($this->filterBuilder->setField($paramName)->setValue($paramValue)->setConditionType('eq')->create());
}
}
}
With this the filtering works and dont forget to add also the field as a field in the xml, otherwise Magento can't filter.
For the interested the correct dataprovider code is now:
<?php
namespace DssSolutions\Management\Ui\DataProvider\PurchaseOrderItem;
use Magento\Ui\DataProvider\AbstractDataProvider;
use Magento\Ui\DataProvider\Modifier\PoolInterface;
use Magento\Framework\Api\FilterBuilder;
use DssSolutions\Management\Model\ResourceModel\PurchaseOrderItem\Collection;
/**
* Class PurchaseOrderItem
* @package DssSolutions\Management\Ui\DataProvider\PurchaseOrsderItem
*/
class ReceiveOrderedProductDataProvider extends AbstractDataProvider
{
/**
* @var PoolInterface
*/
protected $pool;
/**
* @var \Magento\Framework\Registry
*/
protected $_coreRegistry;
/**
* @var \Magento\Framework\App\Request\Http
*/
protected $request;
/**
* @var FilterBuilder
*/
protected $filterBuilder;
/**
* Warehouse constructor.
* @param string $name
* @param string $primaryFieldName
* @param string $requestFieldName
* @param PoolInterface $pool
* @param array $meta
* @param array $data
*/
public function __construct(
$name,
$primaryFieldName,
$requestFieldName,
PoolInterface $pool,
Collection $collection,
\Magento\Framework\Registry $registry,
\Magento\Framework\App\Request\Http $request,
FilterBuilder $filterBuilder,
array $meta = [],
array $data = []
) {
parent::__construct($name, $primaryFieldName, $requestFieldName, $meta, $data);
$this->pool = $pool;
$this->collection = $collection;
$this->_coreRegistry = $registry;
$this->request = $request;
$this->filterBuilder = $filterBuilder;
$this->prepareUpdateUrl();
}
/**
* {@inheritdoc}
*/
public function getData()
{
$this->collection->addFieldToSelect('purchase_order_item_id');
$this->collection->addFieldToSelect('purchase_order_id');
$this->collection->addFieldToSelect('product_sku');
$this->collection->addFieldToSelect('qty_orderred');
$this->collection->addFieldToSelect('qty_received');
$this->collection->addFieldToSelect('qty_returned');
$items = $this->collection->getData();
foreach ($items as $k => $product){
$items[$k]['qty_receivable'] = ($product['qty_orderred'] - $product['qty_received']);
}
$data = [
'totalRecords' => $this->collection->count(),
'items' => array_values($items),
];
return $data;
}
/** * @return void */protected function prepareUpdateUrl()
{
if (!isset($this->data['config']['filter_url_params'])) {
return;
}
foreach ($this->data['config']['filter_url_params'] as $paramName => $paramValue) {
if ('*' == $paramValue) {
$paramValue = $this->request->getParam($paramName);
}
if ($paramValue) {
$this->data['config']['update_url'] = sprintf(
'%s%s/%s/',
$this->data['config']['update_url'],
$paramName,
$paramValue
);
$this->addFilter($this->filterBuilder->setField($paramName)->setValue($paramValue)->setConditionType('eq')->create());
}
}
}
/**
* {@inheritdoc}
*/
public function getMeta()
{
$meta = parent::getMeta();
/** @var ModifierInterface $modifier */
foreach ($this->pool->getModifiersInstances() as $modifier) {
$meta = $modifier->modifyMeta($meta);
}
return $meta;
}
}
Upvotes: 0