8

I want to show product grid in admin form using UI component, for that, I have added the following code in product_form.xml

 <fieldset name="assign_products">
 <argument name="data" xsi:type="array">
 <item name="config" xsi:type="array">
 <item name="label" xsi:type="string" translate="true">Products</item>
 <item name="collapsible" xsi:type="boolean">true</item>
 <item name="sortOrder" xsi:type="number">40</item>
 </item>
 </argument>
 <container name="assign_products_container" >
 <argument name="data" xsi:type="array">
 <item name="config" xsi:type="array">
 <item name="sortOrder" xsi:type="number">160</item>
 </item>
 </argument>
 <htmlContent name="html_content">
 <argument name="block" xsi:type="object">Company\Module\Block\Adminhtml\Module\AssignProducts</argument>
 </htmlContent>
 </container>
 </fieldset> 

And in AssignProducts.php

<?php
/**
 * Copyright © 2016 Magento. All rights reserved.
 * See COPYING.txt for license details.
 */
namespace Company\Module\Block\Adminhtml\Module;
class AssignProducts extends \Magento\Backend\Block\Template
{
 /**
 * Block template
 *
 * @var string
 */
 protected $_template = 'catalog/category/edit/assign_products.phtml';
 /**
 * @var \Magento\Catalog\Block\Adminhtml\Category\Tab\Product
 */
 protected $blockGrid;
 /**
 * @var \Magento\Framework\Registry
 */
 protected $registry;
 /**
 * @var \Magento\Framework\Json\EncoderInterface
 */
 protected $jsonEncoder;
 /**
 * AssignProducts constructor.
 *
 * @param \Magento\Backend\Block\Template\Context $context
 * @param \Magento\Framework\Registry $registry
 * @param \Magento\Framework\Json\EncoderInterface $jsonEncoder
 * @param array $data
 */
 public function __construct(
 \Magento\Backend\Block\Template\Context $context,
 \Magento\Framework\Registry $registry,
 \Magento\Framework\Json\EncoderInterface $jsonEncoder,
 array $data = []
 ) {
 $this->registry = $registry;
 $this->jsonEncoder = $jsonEncoder;
 parent::__construct($context, $data);
 }
 /**
 * Retrieve instance of grid block
 *
 * @return \Magento\Framework\View\Element\BlockInterface
 * @throws \Magento\Framework\Exception\LocalizedException
 */
 public function getBlockGrid()
 {
 if (null === $this->blockGrid) {
 $this->blockGrid = $this->getLayout()->createBlock(
 'Company\Module\Block\Adminhtml\Module\Tab\Product',
 'category.product.grid'
 );
 }
 return $this->blockGrid;
 }
 /**
 * Return HTML of grid block
 *
 * @return string
 */
 public function getGridHtml()
 {
 return $this->getBlockGrid()->toHtml();
 }
 /**
 * @return string
 */
 public function getProductsJson()
 {
 $products = $this->getCategory()->getProductsPosition();
 if (!empty($products)) {
 return $this->jsonEncoder->encode($products);
 }
 return '{}';
 }
 /**
 * Retrieve current category instance
 *
 * @return array|null
 */
 public function getCategory()
 {
 return $this->registry->registry('faqs');
 }
}

In Company\Module\Block\Adminhtml\Module\Tab\Product.php

<?php
namespace Company\Module\Block\Adminhtml\Module\Tab;
use Company\Module\Model\ModuleFactory;
class Product extends \Magento\Backend\Block\Widget\Grid\Extended
{
 /**
 * @var \Magento\Catalog\Model\ResourceModel\Product\CollectionFactory
 */
 protected $productCollectionFactory;
 /**
 * Contact factory
 *
 * @var ContactFactory
 */
 protected $moduleFactory;
 /**
 * @var \Magento\Framework\Registry
 */
 protected $registry;
 protected $_objectManager = null;
 /**
 *
 * @param \Magento\Backend\Block\Template\Context $context
 * @param \Magento\Backend\Helper\Data $backendHelper
 * @param \Magento\Framework\Registry $registry
 * @param ModuleFactory $attachmentFactory
 * @param \Magento\Catalog\Model\ResourceModel\Product\CollectionFactory $productCollectionFactory
 * @param array $data
 */
 public function __construct(
 \Magento\Backend\Block\Template\Context $context,
 \Magento\Backend\Helper\Data $backendHelper,
 \Magento\Framework\Registry $registry,
 \Magento\Framework\ObjectManagerInterface $objectManager,
 ModuleFactory $moduleFactory,
 \Magento\Catalog\Model\ResourceModel\Product\CollectionFactory $productCollectionFactory,
 array $data = []
 ) {
 $this->moduleFactory = $moduleFactory;
 $this->productCollectionFactory = $productCollectionFactory;
 $this->_objectManager = $objectManager;
 $this->registry = $registry;
 parent::__construct($context, $backendHelper, $data);
 }
 /**
 * _construct
 * @return void
 */
 protected function _construct()
 {
 parent::_construct();
 $this->setId('productsGrid');
 $this->setDefaultSort('entity_id');
 $this->setDefaultDir('DESC');
 $this->setSaveParametersInSession(true);
 $this->setUseAjax(true);
 if ($this->getRequest()->getParam('prdt_id')) {
 $this->setDefaultFilter(array('in_product' => 1));
 }
 }
 /**
 * add Column Filter To Collection
 */
 protected function _addColumnFilterToCollection($column)
 {
 // Set custom filter for in category flag
 if ($column->getId() == 'in_product') {
 $productIds = $this->_getSelectedProducts();
 if (empty($productIds)) {
 $productIds = 0;
 }
 if ($column->getFilter()->getValue()) {
 $this->getCollection()->addFieldToFilter('entity_id', ['in' => $productIds]);
 } elseif (!empty($productIds)) {
 $this->getCollection()->addFieldToFilter('entity_id', ['nin' => $productIds]);
 }
 } else {
 parent::_addColumnFilterToCollection($column);
 }
 return $this;
 }
 /**
 * prepare collection
 */
 protected function _prepareCollection()
 {
 $collection = $this->productCollectionFactory->create();
 $collection->addAttributeToSelect('name');
 $collection->addAttributeToSelect('sku');
 $collection->addAttributeToSelect('price');
 $this->setCollection($collection);
 return parent::_prepareCollection();
 }
 /**
 * @return $this
 */
 protected function _prepareColumns()
 {
 /* @var $model \Webspeaks\ProductsGrid\Model\Slide */
 $model = $this->_objectManager->get('\Company\Module\Model\Module');
 $this->addColumn(
 'in_product',
 [
 'header_css_class' => 'a-center',
 'type' => 'checkbox',
 'name' => 'in_product',
 'align' => 'center',
 'index' => 'entity_id',
 'values' => $this->_getSelectedProducts(),
 ]
 );
 $this->addColumn(
 'entity_id',
 [
 'header' => __('Product ID'),
 'type' => 'number',
 'index' => 'entity_id',
 'header_css_class' => 'col-id',
 'column_css_class' => 'col-id',
 ]
 );
 $this->addColumn(
 'name',
 [
 'header' => __('Name'),
 'index' => 'name',
 'class' => 'xxx',
 'width' => '50px',
 ]
 );
 $this->addColumn(
 'sku',
 [
 'header' => __('Sku'),
 'index' => 'sku',
 'class' => 'xxx',
 'width' => '50px',
 ]
 );
 $this->addColumn(
 'price',
 [
 'header' => __('Price'),
 'type' => 'currency',
 'index' => 'price',
 'width' => '50px',
 ]
 );
 return parent::_prepareColumns();
 }
 /**
 * @return string
 */
 public function getGridUrl()
 {
 return $this->getUrl('*/*/productsgrid', ['_current' => true]);
 }
 /**
 * @param object $row
 * @return string
 */
 public function getRowUrl($row)
 {
 return '';
 }
 protected function _getSelectedProducts()
 {
 $product = $this->getPrdt();
 return $product->getProducts($product);
 }
 /**
 * Retrieve selected products
 *
 * @return array
 */
 public function getSelectedProducts()
 {
 $product = $this->getPrdt();
 $selected = $product->getProducts($product);
 if (!is_array($selected)) {
 $selected = [];
 }
 return $selected;
 }
 protected function getPrdt()
 {
 $prdtId = $this->getRequest()->getParam('prdt_id');
 $product = $this->moduleFactory->create();
 if ($prdtId) {
 $product->load($prdtId);
 }
 return $product;
 }
 /**
 * {@inheritdoc}
 */
 public function canShowTab()
 {
 return true;
 }
 /**
 * {@inheritdoc}
 */
 public function isHidden()
 {
 return true;
 }
}
Ghulam.M
9738 silver badges25 bronze badges
asked Dec 13, 2016 at 5:23
2

0

Know someone who can answer? Share a link to this question via email, Twitter, or Facebook.

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.