addCustomData(); } /** * Add custom data to collection * * @return $this */ public function addCustomData() { $this->getSelect() ->joinLeft( ['cg' => $this->getTable('customer_grid_flat')], 'cg.entity_id = main_table.customer_id', ['cg.name as customer_name', 'cg.email as customer_email', 'cg.entity_id as customer_exist'] )->joinLeft( ['sw' => $this->getTable('store_website')], 'sw.website_id = main_table.website_id', ['sw.name as website'] ); $this->getSelect()->columns( ['merge_email' => new \Zend_Db_Expr('IF(ISNULL(`cg`.`email`), `main_table`.`guest_email`, `cg`.`email`)')] ); $filtersMap = [ ConsentLogResource::ID_FIELD_NAME => 'main_table.' . ConsentLogResource::ID_FIELD_NAME, ConsentLogInterface::CUSTOMER_ID => 'main_table.' . ConsentLogInterface::CUSTOMER_ID, 'customer_name' => 'cg.name', 'customer_exist' => 'cg.entity_id', 'website' => 'sw.name', ConsentLogInterface::CREATED_AT => 'main_table.' . ConsentLogInterface::CREATED_AT, ConsentLogInterface::IP_ADDRESS => 'main_table.' . ConsentLogInterface::IP_ADDRESS, ConsentLogInterface::SETTINGS => 'main_table.' . ConsentLogInterface::SETTINGS, ]; foreach ($filtersMap as $filter => $alias) { $this->addFilterToMap($filter, $alias); } return $this; } /** * Custom filter for 'merge_email' field * * @param array|string $field * @param null $condition * @return $this|\Plumrocket\CookieConsent\Model\ResourceModel\ConsentLog\Grid\Collection */ public function addFieldToFilter($field, $condition = null) { if ('merge_email' === $field) { $this->getSelect()->where( "(`cg`.`email` LIKE ? OR (`main_table`.`guest_email` LIKE ? AND `cg`.`email` IS NULL))", $condition['like'] ); return $this; } return parent::addFieldToFilter($field, $condition); } /** * @param \Magento\Framework\DataObject|\Magento\Framework\Model\AbstractModel $item * @return \Magento\Framework\DataObject */ protected function beforeAddLoadedItem(DataObject $item) { $this->getResource()->unserializeDataProviderDocumentFields($item); return parent::beforeAddLoadedItem($item); } }