Magento – filter_condition_callback using Ui Component – Magento 2

magento2rendereruicomponent

I have a customer column in my custom module admin grid (created using UI component) where I show the customer name. But in database I save customer Id. I need to use the filter on customer name as input box in my grid filters. When I filter the grid by customer ID it works fine.

My question is how I can filter it by using customer name?
Same as we do in Magento 1 using filter_condition_callback.

Best Answer

Yes. If one wants to join eav_attribute_option_value table to filtering option value, one can use below code.

$this->addColumn(
        'product_manufacturer',
        [
            'header' => __('Manufacturer'),
            'index' => 'manufacturer',
            'filter_condition_callback' => [$this, 'filterCustomManufacturer'],
            'header_css_class' => 'col-manufacturer',
            'column_css_class' => 'col-manufacturer'
        ]
    );

Below add filter condition callback function including joins like,

protected function filterCustomManufacturer($collection, $column)
{
    if (!$value = $column->getFilter()->getValue()) {
        return $this;
    }

    $collection->getSelect()->joinLeft(array('c_p_e_i'=>'catalog_product_entity_int'),
    'e.row_id = c_p_e_i.row_id',array('value'));

    $collection->getSelect()->joinLeft(array('attribute_option'=>'eav_attribute_option_value'),
    'c_p_e_i.value = attribute_option.option_id and attribute_option.store_id = 0',
     array('eaov_val'=>'value' , 'eaov_option'=>'option_id'));

     $collection->getSelect()->where("`attribute_option`.`value` like ?", "%$value%");

    return $this;
}