Magento – Magento 2 – How to add ajax tab on admin user edit page in admin section

adminajaxmagento-2.0.6tabs

I want to add a tab on User edit page which can be accessed from grid in section System -> All users. I have managed to add tab but it is not loading through AJAX. When I click on tab it redirects to another URL.

Please see screen shot and code below.

enter image description here

Code:-

/app/code/Namespace/Module/Block/Adminhtml/Edit/Tab/Customer.php

<?php
namespace Namespace\Module\Block\Adminhtml\Edit\Tab;

class Customer extends \Magento\Backend\Block\Widget\Tabs
{
    /**
     * Class constructor
     *
     * @return void
     */
    protected function _construct()
    {
        parent::_construct();
        $this->setId('page_tabs');
        $this->setDestElementId('edit_form');
    }

    /**
     * @return $this
     */
    protected function _beforeToHtml()
    {

        $this->addTab(
            'magcr_customer_section',
            [
                'label' => __('Select Customer(s)'),
                'title' => __('Select Customer(s)'),
                'url' => $this->getUrl('magcr/index/customer'),
                'class' => 'ajax',
                'active' => FALSE
            ]
        );
        return parent::_beforeToHtml();
    }
}

/app/code/Namespace/Module/view/adminhtml/layout/adminhtml_user_edit.xml

<?xml version="1.0"?>
<page xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" layout="admin-2columns-left" xsi:noNamespaceSchemaLocation="urn:magento:framework:View/Layout/etc/page_configuration.xsd">
    <body>
        <referenceContainer name="left">
            <block class="Namespace\Module\Block\Adminhtml\Edit\Tab\Customer" name="adminhtml.user.edit.tabs1"/>
        </referenceContainer>
    </body>
</page>

Please help what am I doing wrong.

UPDATE

When I modify below code in constructor of block class, the tabs loads through ajax.

$this->setId('magcr_page_tabs');

But now problem is both 'User Info' and 'Select Customer(s)' load automatically on page load.

Best Answer

For example, I add Websites tab

  1. Find current tabs reference "adminhtml.user.edit.tabs" https://github.com/magento/magento2/blob/2.2-develop/app/code/Magento/User/view/adminhtml/layout/adminhtml_user_edit.xml#L11

  2. Create your module view xml

/app/code/Namespace/Module/view/adminhtml/layout/adminhtml_user_edit.xml

    <page xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" layout="admin-2columns-left" xsi:noNamespaceSchemaLocation="urn:magento:framework:View/Layout/etc/page_configuration.xsd">
        <body>
            <referenceBlock name="adminhtml.user.edit.tabs">
                <block class="Magento\Catalog\Block\Adminhtml\Product\Edit\Action\Attribute\Tab\Websites" name="tab_websites" template="Magento_Catalog::catalog/product/edit/action/websites.phtml"/>
                <action method="addTab">
                    <argument name="name" xsi:type="string">websites</argument>
                    <argument name="block" xsi:type="string">tab_websites</argument>
                </action>
            </referenceBlock>
        </body>
    </page>
Related Topic