I am trying to add my custom table field to order grid but its not working fine for sales order grid but its working fine product grid so below are the code adding column to sales order grid and product grid
i feel problem is in type name for product i gave
<type name="Magento\Catalog\Ui\DataProvider\Product\ProductDataProvider">
working fine
but for order grid i gave
<type name="sales_order_grid_data_source">
but sales order grid through error
Fatal error: Call to undefined method Magento\Framework\View\Element\UiComponent\DataProvider\DataProvider::addField() in D:\xampp\htdocs\magento2\vendor\magento\module-ui\Component\Listing\Columns\Column.php on line 116
below are the code for ading my custom field to order and product grid
app\code\Sugarcode\Test\etc\adminhtml\di.xml
<type name="Magento\Catalog\Ui\DataProvider\Product\ProductDataProvider"> <arguments> <argument name="addFieldStrategies" xsi:type="array"> <item name="title" xsi:type="object">Sugarcode\Test\Ui\DataProvider\Test\AddTitleFieldToCollection</item> </argument> <argument name="addFilterStrategies" xsi:type="array"> <item name="title" xsi:type="object">Sugarcode\Test\Ui\DataProvider\Test\AddTitleFilterToCollection</item> </argument> </arguments> </type> <type name="sales_order_grid_data_source"> <arguments> <argument name="addFieldStrategies" xsi:type="array"> <item name="title" xsi:type="object">Sugarcode\Test\Ui\DataProvider\Test\AddTitleFieldToOrderCollection</item> </argument> <argument name="addFilterStrategies" xsi:type="array"> <item name="title" xsi:type="object">Sugarcode\Test\Ui\DataProvider\Test\AddTitleFilterToOrderCollection</item> </argument> </arguments> </type>
app\code\Sugarcode\Test\view\adminhtml\ui_component\sales_order_grid.xml
<columns name="sales_order_columns"> <column name="title"> <argument name="data" xsi:type="array"> <item name="config" xsi:type="array"> <item name="filter" xsi:type="string">text</item> <item name="add_field" xsi:type="boolean">true</item> <item name="label" xsi:type="string" translate="true">Titles</item> <item name="sortOrder" xsi:type="number">75</item> </item> </argument> </column> </columns>
app\code\Sugarcode\Test\view\adminhtml\ui_component\product_listing.xml
<columns name="product_columns" class="Magento\Catalog\Ui\Component\Listing\Columns"> <column name="title"> <argument name="data" xsi:type="array"> <item name="config" xsi:type="array"> <item name="filter" xsi:type="string">text</item> <item name="add_field" xsi:type="boolean">true</item> <item name="label" xsi:type="string" translate="true">Titles</item> <item name="sortOrder" xsi:type="number">75</item> </item> </argument> </column> </columns>
4 app\code\Sugarcode\Test\Ui\DataProvider\Test\AddTitleFieldToCollection.php
<?php
/**
* Copyright © 2015 Magento. All rights reserved.
* See COPYING.txt for license details.
*/
namespace Sugarcode\Test\Ui\DataProvider\Test;
use Magento\Framework\Data\Collection;
use Magento\Ui\DataProvider\AddFieldToCollectionInterface;
/**
* Class AddQuantityFieldToCollection
*/
class AddTitleFieldToCollection implements AddFieldToCollectionInterface
{
/**
* {@inheritdoc}
*/
public function addField(Collection $collection, $field, $alias = null)
{
$collection->joinField(
'title',
'testtable', //tablename
'title', //field
'id=entity_id', //join condition
'1', // condition
'left'
);
}
}
5 app\code\Sugarcode\Test\Ui\DataProvider\Test\AddTitleFieldToOrderCollection.php
<?php
/**
* Copyright © 2015 Magento. All rights reserved.
* See COPYING.txt for license details.
*/
namespace Sugarcode\Test\Ui\DataProvider\Test;
use Magento\Framework\Data\Collection;
use Magento\Ui\DataProvider\AddFieldToCollectionInterface;
/**
* Class AddQuantityFieldToCollection
*/
class AddTitleFieldToOrderCollection implements AddFieldToCollectionInterface
{
/**
* {@inheritdoc}
*/
public function addField(Collection $collection, $field, $alias = null)
{
$collection->joinField(
'title',
'testtable',
'title',
'id=entity_id',
'1',
'left'
);
}
}
6 app\code\Sugarcode\Test\Ui\DataProvider\Test\AddTitleFilterToCollection.php
<?php
/**
* Copyright © 2015 Magento. All rights reserved.
* See COPYING.txt for license details.
*/
namespace Sugarcode\Test\Ui\DataProvider\Test;
use Magento\Eav\Model\Entity\Collection\AbstractCollection;
use Magento\Framework\Data\Collection;
use Magento\Ui\DataProvider\AddFilterToCollectionInterface;
/**
* Class AddQuantityFilterToCollection
*/
class AddTitleFilterToCollection implements AddFilterToCollectionInterface
{
/**
* {@inheritdoc}
*/
public function addFilter(Collection $collection, $field, $condition = null)
{
if (isset($condition['like'])) {
$collection->getSelect()->where(
AbstractCollection::ATTRIBUTE_TABLE_ALIAS_PREFIX . 'title.title like ?',
$condition['like']
);
}
}
}
7 app\code\Sugarcode\Test\Ui\DataProvider\Test\AddTitleFilterToOrderCollection.php
<?php
/**
* Copyright © 2015 Magento. All rights reserved.
* See COPYING.txt for license details.
*/
namespace Sugarcode\Test\Ui\DataProvider\Test;
use Magento\Eav\Model\Entity\Collection\AbstractCollection;
use Magento\Framework\Data\Collection;
use Magento\Ui\DataProvider\AddFilterToCollectionInterface;
/**
* Class AddQuantityFilterToCollection
*/
class AddTitleFilterToOrderCollection implements AddFilterToCollectionInterface
{
/**
* {@inheritdoc}
*/
public function addFilter(Collection $collection, $field, $condition = null)
{
if (isset($condition['like'])) {
$collection->getSelect()->where(
AbstractCollection::ATTRIBUTE_TABLE_ALIAS_PREFIX . 'title.title like ?',
$condition['like']
);
}
}
}
can any one help me :-)