Question

How Can we add custome grid with custome collection on customer group edit form in fieldset.See Image for details

Was it helpful?

Solution

After adding preference for the class

<preference for="Magento\Customer\Block\Adminhtml\Group\Edit\Form" type="Vendor\Module\Block\Adminhtml\PathToFile" />

Add below code in it

$fieldset->addType(
    'some_thing',
    'Vendor\Module\Block\Adminhtml\Renderer\ClassName'
);
$field = $fieldset->addField(
    'some_thing',
    'some_thing',
    [
        'name' => 'some_thing',
        'label' => __('Something'),
        'title' => __('Something')
    ]
);

In your Vendor/Module/Block/Adminhtml/Renderer/Classname.php

namespace Vendor\Module\Block\Adminhtml\Renderer;

class Classname extends \Magento\Framework\Data\Form\Element\AbstractElement
{
    public function getElementHtml()
    {
        $objectManager = \Magento\Framework\App\ObjectManager::getInstance();
        $product = $objectManager->create('Vendor\Module\Block\Adminhtml\Path to your grid file')->getBlockGrid(); //can inject also I used ObjectManager for testing

        $html = parent::getElementHtml();
        $value = $this->getValue();
        $html.=$product;
        return $html;
    }
}
?>

In your Grid file at Vendor/Module/Block/Adminhtml/Path to gridfile.php

<?php

namespace Vendor\Module\Block\Adminhtml;

class Grid extends \Magento\Backend\Block\Widget\Grid\Extended
{
    protected $productFactory;

    public function __construct(
        \Magento\Backend\Block\Template\Context $context,
        \Magento\Backend\Helper\Data $backendHelper,
        \Magento\Catalog\Model\ProductFactory $productFactory,
        \Magento\Framework\Registry $coreRegistry,
        array $data = []
    ) {
        $this->productFactory = $productFactory;
        $this->_coreRegistry = $coreRegistry;
        parent::__construct($context, $backendHelper, $data);
    }

    /**
     * @return void
     */
    protected function _construct()
    {
        // parent::_construct();
        // $this->setId('hello_tab_grid');
        // $this->setDefaultSort('entity_id');
        $this->setUseAjax(true);
    }

    /**
     * @return Grid
     */
    protected function _prepareCollection()
    {
        $collection = $this->productFactory->create()->getCollection()->addAttributeToSelect("*");
        $this->setCollection($collection);
        return parent::_prepareCollection();
    }

    /**
     * @return Extended
     */
    protected function _prepareColumns()
    {
        $this->addColumn(
            'entity_id',
            [
                'header' => __('Product Id'),
                'sortable' => true,
                'index' => 'entity_id',
                'header_css_class' => 'col-id',
                'column_css_class' => 'col-id'
            ]
        );
        $this->addColumn(
            'name',
            [
                'header' => __('Product Name'),
                'index' => 'name'
            ]
        );
        $this->addColumn(
            'sku',
            [
                'header' => __('Sku'),
                'index' => 'sku'
            ]
        );

        return parent::_prepareColumns();
    }

    /**
     * @return string
     */
    public function getGridUrl()
    {
        return $this->getUrl('moduler/*/action', ['_current' => true]);
    }
public function getBlockGrid()
    {
        if (null === $this->blockGrid) {
            $this->blockGrid = $this->getLayout()->createBlock(
                '\Vendor\Module\Block\Adminhtml\Path to your grid file',
                'category.product.grid'
            );
        }
        return $this->blockGrid->toHtml();
    }
    
}

OTHER TIPS

In order to add custom grid in customer group edit form you need to override the controller and custom block to render grid in it.

Create custom module for it

Vendor/Module/etc.xml

<?xml version="1.0" ?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
    <preference for="Magento\Customer\Controller\Adminhtml\Group\NewAction" type="Vendor\Module\Rewrite\Magento\Customer\Controller\Adminhtml\Group\NewAction"/>
</config>

Vendor/Module/Rewrite/Magento/Customer/Controller/Adminhtml/Group/NewAction.php

<?php
/**
 * Copyright © Dev All rights reserved.
 * See COPYING.txt for license details.
 */
declare(strict_types=1);

namespace Vendor\Module\Rewrite\Magento\Customer\Controller\Adminhtml\Group;

class NewAction extends \Magento\Customer\Controller\Adminhtml\Group\NewAction
{
    /**
     * Edit or create customer group.
     *
     * @return \Magento\Backend\Model\View\Result\Page
     */
    public function execute()
    {
        $groupId = $this->_initGroup();

        /** @var \Magento\Backend\Model\View\Result\Page $resultPage */
        $resultPage = $this->resultPageFactory->create();
        $resultPage->setActiveMenu('Magento_Customer::customer_group');
        $resultPage->getConfig()->getTitle()->prepend(__('Customer Groups'));
        $resultPage->addBreadcrumb(__('Customers'), __('Customers'));
        $resultPage->addBreadcrumb(__('Customer Groups'), __('Customer Groups'), $this->getUrl('customer/group'));

        if ($groupId === null) {
            $resultPage->addBreadcrumb(__('New Group'), __('New Customer Groups'));
            $resultPage->getConfig()->getTitle()->prepend(__('New Customer Group'));
        } else {
            $resultPage->addBreadcrumb(__('Edit Group'), __('Edit Customer Groups'));
            $resultPage->getConfig()->getTitle()->prepend(
                $this->groupRepository->getById($groupId)->getCode()
            );
        }

        $resultPage->getLayout()->addBlock(\Magento\Customer\Block\Adminhtml\Group\Edit::class, 'group', 'content')
            ->setEditMode((bool)$groupId);


        $resultPage->getLayout()->addBlock(\Vendor\Module\Block\Adminhtml\Group\Grid::class, 'custom_grid', 'content')
            ->setEditMode((bool)$groupId);

        return $resultPage;
    }
}

Vendor/Module/Block/Adminhtml/Group/Grid.php

<?php

namespace Vendor\Module\Block\Adminhtml\Group;

class Grid extends \Magento\Backend\Block\Widget\Grid\Extended
{
    protected $productFactory;

    public function __construct(
        \Magento\Backend\Block\Template\Context $context,
        \Magento\Backend\Helper\Data $backendHelper,
        \Magento\Catalog\Model\ProductFactory $productFactory,
        \Magento\Framework\Registry $coreRegistry,
        array $data = []
    ) {
        $this->productFactory = $productFactory;
        $this->_coreRegistry = $coreRegistry;
        parent::__construct($context, $backendHelper, $data);
    }

    /**
     * @return void
     */
    protected function _construct()
    {
        // parent::_construct();
        // $this->setId('hello_tab_grid');
        // $this->setDefaultSort('entity_id');
        $this->setUseAjax(true);
    }

    /**
     * @return Grid
     */
    protected function _prepareCollection()
    {
        $collection = $this->productFactory->create()->getCollection()->addAttributeToSelect("*");
        $this->setCollection($collection);
        return parent::_prepareCollection();
    }

    /**
     * @return Extended
     */
    protected function _prepareColumns()
    {
        $this->addColumn(
            'entity_id',
            [
                'header' => __('Product Id'),
                'sortable' => true,
                'index' => 'entity_id',
                'header_css_class' => 'col-id',
                'column_css_class' => 'col-id'
            ]
        );
        $this->addColumn(
            'name',
            [
                'header' => __('Product Name'),
                'index' => 'name'
            ]
        );
        $this->addColumn(
            'sku',
            [
                'header' => __('Sku'),
                'index' => 'sku'
            ]
        );

        return parent::_prepareColumns();
    }

    /**
     * @return string
     */
    public function getGridUrl()
    {
        return $this->getUrl('hello/*/helloGrid', ['_current' => true]);
    }
    
}

This is just a overview to show custom grid, for full implementation and operations on custom grid please follow this Blog

Thanks!

Licensed under: CC-BY-SA with attribution
Not affiliated with magento.stackexchange
scroll top