Ok, let's start a story. A bunch of files we'll need.
1) Adding new tab in layout file (for example customer_index_edit.xml
)
<?xml version="1.0"?>
<page xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:View/Layout/etc/page_configuration.xsd">
<body>
<referenceBlock name="customer_form">
<block class="Amasty\Rewards\Block\Adminhtml\RewardsTab" name="amrewards" />
</referenceBlock>
</body>
</page>
2) Creating tab class
Amasty/Rewards/Block/Adminhtml/RewardsTab.php
which's main part of code is:
public function getTabUrl()
{
return $this->getUrl('amasty_rewards/rewards/index/', ['_current' => true]);
}
3) Creating new layout file amasty_rewards_rewards_index.xml
where we'll specify blocks and grid:
<?xml version="1.0"?>
<layout xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:View/Layout/etc/layout_generic.xsd">
<update handle="amasty_rewards_rewards_ajaxgrid" />
<container name="root">
<block class="Amasty\Rewards\Block\Adminhtml\Edit\Tab\View\PersonalInfo" name="personal_info" template="Amasty_Rewards::tab/view/personal_info.phtml"/>
<block class="Amasty\Rewards\Block\Adminhtml\Rewards\Edit\NewReward" name="add-points" template="Amasty_Rewards::rewards/edit/new/form.phtml"/>
</container>
</layout>
4) Creating grid layout file amasty_rewards_rewards_grid.xml
:
<?xml version="1.0"?>
<layout xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:View/Layout/etc/layout_generic.xsd">
<update handle="amasty_rewards_rewards_ajaxgrid"/>
<container name="root">
</container>
</layout>
5) And another one amasty_rewards_rewards_ajaxgrid.xml
:
<?xml version="1.0"?>
<layout xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:View/Layout/etc/layout_generic.xsd">
<referenceContainer name="root">
<block class="\Magento\Backend\Block\Widget\Grid" after="add-points" name="customer.rewards.edit.tab">
<arguments>
<argument name="dataSource" xsi:type="object">Amasty\Rewards\Model\ResourceModel\Rewards\Collection\Grid</argument>
<argument name="id" xsi:type="string">rewardsGrid</argument>
<argument name="use_ajax" xsi:type="string">true</argument>
<argument name="default_sort" xsi:type="string">id</argument>
<argument name="grid_url" xsi:type="url" path="*/*/grid">
<param name="_current">1</param>
</argument>
<argument name="product_configuration_helpers" xsi:type="array">
<item name="default" xsi:type="string">Magento\Catalog\Helper\Product\Configuration</item>
</argument>
</arguments>
<block class="Magento\Backend\Block\Widget\Grid\ColumnSet" as="grid.columnSet" name="customer.rewards.edit.tab.columnSet">
<arguments>
<argument name="empty_text" xsi:type="string" translate="true">No Items Found</argument>
</arguments>
<block class="Magento\Backend\Block\Widget\Grid\Column" as="action_date">
<arguments>
<argument name="header" xsi:type="string" translate="true">Date</argument>
<argument name="index" xsi:type="string">action_date</argument>
<argument name="gmtoffset" xsi:type="string">1</argument>
<argument name="type" xsi:type="string">date</argument>
<argument name="id" xsi:type="string">action_date</argument>
</arguments>
</block>
...
We're done with layouts. Now we need Controllers.
6) Grid controller
app/code/Amasty/Rewards/Controller/Adminhtml/Rewards/Grid.php
<?php
namespace Amasty\Rewards\Controller\Adminhtml\Rewards;
class Grid extends \Amasty\Rewards\Controller\Adminhtml\Rewards
{
public function execute()
{
$this->initCurrentCustomer();
$this->_view->loadLayout();
$this->_view->renderLayout();
}
}
7) And index controller
app/code/Amasty/Rewards/Controller/Adminhtml/Rewards/Index.php
<?php
namespace Amasty\Rewards\Controller\Adminhtml\Rewards;
class Index extends \Amasty\Rewards\Controller\Adminhtml\Rewards
{
public function execute()
{
$customerId = $this->initCurrentCustomer();
$model = $this->_objectManager->create('Amasty\Rewards\Model\Rewards');
$statistic = $model->getStatistic($customerId);
$this->_coreRegistry->register('current_amasty_rewards_statistic', $statistic);
$resultLayout = $this->resultLayoutFactory->create();
return $resultLayout;
}
}
And finally here is the result: