Magento 2.3 - Add product name column in custom ui grid with product name filter
In case you are using custom filter you can create custom filter strategy in your DataProvider
:
Declare it in the di.xml
:
<type name="Vendor\Module\Ui\DataProvider\YourCustomDataProvider">
<arguments>
<argument name="addFilterStrategies" xsi:type="array">
<item name="methods" xsi:type="object">Vendor\Module\Ui\DataProvider\YourCustomDataProvider\YourCustomFilterStrategy</item>
</argument>
</arguments>
</type>
Create that filter strategy class:
<?php
namespace Vendor\Module\Ui\DataProvider\YourCustomDataProvider;
use Magento\Framework\Data\Collection;
use Vendor\Model\Model\ResourceModel\YourCustom\Collection as RealCollection;
use Magento\Ui\DataProvider\AddFilterToCollectionInterface;
/**
* Class YourCustomFilterStrategy
*/
class YourCustomFilterStrategy implements AddFilterToCollectionInterface
{
/**
* @param Collection|RealCollection $collection
* @param string $field
* @param null $condition
*/
public function addFilter(Collection $collection, $field, $condition = null)
{
if (isset($condition['eq'])) {
//$collection->addShippingMethodFilter($condition['eq']);
// Your custom filter for that field here in any collection or in collection
// which used as main for the data provider (the `RealCollection` in my example)
}
}
}
Note: in my example custom filter in the collection looks like this:
/**
* @param string $method
* @return $this
*/
public function addShippingMethodFilter($method)
{
$conditions = [
$this->_translateCondition(
DeliveryOptionInterface::KEY_METHODS,
[
['like' => $method],
['like' => '%,' . $method],
['like' => $method . ',%'],
['like' => '%,' . $method . ',%'],
['null' => true]
]
),
$this->_translateCondition(
DeliveryOptionInterface::KEY_SHIPPING_METHODS_CHOICE_LIMITER,
[
['eq' => DeliveryOptionInterface::SHIPPING_METHODS_CHOICE_LIMIT_ALL_METHODS]
]
)
];
$resultCondition = '(' . implode(') ' . Select::SQL_OR . ' (', $conditions) . ')';
$this->_select->where($resultCondition, null, Select::TYPE_CONDITION);
return $this;
}
It filters a data in the collection by comma-separated shipping methods. Just write custom filter according your needs. I think it should take products collection by ID's with name
attribute, then filter it by name
as a string. In result you can take all suitable product ID's which could be used in your collection as a filter, like: $collection->addFieldToFilter('product_id', ['in' => $productIds])
or like in my example with a wildcard (%
) symbol.
Your DataProvider
should be modified as well:
First add the filter strategies attribute in the constructor:
/**
* Construct
*
* @param string $name
* @param string $primaryFieldName
* @param string $requestFieldName
* @param OrderAddressCollectionFactory $collectionFactory
* @param \Magento\Framework\App\RequestInterface $request
* @param \Magento\Framework\Api\FilterBuilder $filterBuilder
* @param \Magento\Ui\DataProvider\AddFilterToCollectionInterface[] $addFilterStrategies
* @param array $meta
* @param array $data
*/
public function __construct(
$name,
$primaryFieldName,
$requestFieldName,
OrderAddressCollectionFactory $collectionFactory,
\Magento\Framework\App\RequestInterface $request,
\Magento\Framework\Api\FilterBuilder $filterBuilder,
array $addFilterStrategies = [],
array $meta = [],
array $data = []
) {
parent::__construct($name, $primaryFieldName, $requestFieldName, $meta, $data);
$this->collection = $collectionFactory->create();
$this->request = $request;
$this->filterBuilder = $filterBuilder;
$this->addFilterStrategies = $addFilterStrategies;
}
Note: Do not copy it as-is, because this is just an example, use own construct method with $addFilterStrategies
argument added!
and update the addFilter
method this way:
/**
* {@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);
}
}
You can add any filter
PS: do not forget to update all namespaces, classnames, arguments, etc. according your module. PPS: This way you can add as many custom filters to your collection as you want, each in custom class.