HOW TO CREATE A TAB IN Admin CUSTOMER EDIT PAGE IN MAGENTO 2?

In Magento 2, You can add a new custom tab in the Customer Edit page. If you want to add a new tab in Customer edit section for your custom requirement, You can add easily customer tab in Customer edit section.

You need to create a simple module for add extra tab in the customer edit section.

Refer below code snippet for add extra tab,
I have taken Rbj as Packagename and CustomerTab as modulename for simplicity.

You need to create first registration.php and module.xml file for defining our module.
Path: app/code/Rbj/CustomerTab/registration.php

<?php
\Magento\Framework\Component\ComponentRegistrar::register(
    \Magento\Framework\Component\ComponentRegistrar::MODULE,
    'Rbj_CustomerTab',
    __DIR__
);

Create module.xml file, Path: app/code/Rbj/CustomerTab/etc/module.xml

<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Module/etc/module.xsd">
    <module name="Rbj_CustomerTab" setup_version="2.0.0">
        <sequence>
            <module name="Magento_Customer"/>
        </sequence>
    </module>
</config>

We have added a dependency for Magento Customer Module for add a new tab in the customer edit page. So we have defined Magento_Customer module in sequence tag in an above XML file.

Now comes to the main entry point of a module,
For Add new tab, We must override customer_index_edit.xml file to add our logic of add custom tabs.

referenceBlock customer_form contains the tab lists for customer edit section page.
We have declared Rbj\CustomerTab\Block\Adminhtml\CustomerEdit\Tab\View.php file as Block file.

So we need to create a customer_index_edit.xml file in our module at below path,
Path: app/code/Rbj/CustomerTab/view/adminhtml/layout/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="Rbj\CustomerTab\Block\Adminhtml\CustomerEdit\Tab\View" name="customertab_panel">
                <arguments>
                    <argument name="tab_label" xsi:type="string" translate="true">My Custom New Tab</argument>
                    <argument name="sort_order" xsi:type="number">100</argument>
                </arguments>
            </block>
        </referenceBlock>
    </body>
</page>

In the above file, We have declared Block file for set your custom logic, which you want to display in custom tabs of the customer edit page.

Create a New Block PHP file,
Path: app/code/Rbj/CustomerTab/Block/Adminhtml/CustomerEdit/Tab/View.php

<?php
namespace Rbj\CustomerTab\Block\Adminhtml\CustomerEdit\Tab;

class View extends \Magento\Backend\Block\Template implements \Magento\Ui\Component\Layout\Tabs\TabInterface
{
    /**
     * Template
     *
     * @var string
     */
    protected $_template = 'tab/customer_view.phtml';

    /**
     * View constructor.
     * @param \Magento\Backend\Block\Template\Context $context
     * @param \Magento\Framework\Registry $registry
     * @param array $data
     */
    public function __construct(
        \Magento\Backend\Block\Template\Context $context,
        \Magento\Framework\Registry $registry,
        array $data = []
    ) {
        $this->_coreRegistry = $registry;
        parent::__construct($context, $data);
    }

    /**
     * @return string|null
     */
    public function getCustomerId()
    {
        return $this->_coreRegistry->registry(\Magento\Customer\Controller\RegistryConstants::CURRENT_CUSTOMER_ID);
    }

    /**
     * @return \Magento\Framework\Phrase
     */
    public function getTabLabel()
    {
        return __('Custom Tab');
    }

    /**
     * @return \Magento\Framework\Phrase
     */
    public function getTabTitle()
    {
        return __('Custom Tab');
    }

    /**
     * @return bool
     */
    public function canShowTab()
    {
        if ($this->getCustomerId()) {
            return true;
        }
        return false;
    }

    /**
     * @return bool
     */
    public function isHidden()
    {
        if ($this->getCustomerId()) {
            return false;
        }
        return true;
    }

    /**
     * Tab class getter
     *
     * @return string
     */
    public function getTabClass()
    {
        return '';
    }

    /**
     * Return URL link to Tab content
     *
     * @return string
     */
    public function getTabUrl()
    {
        return '';
    }

    /**
     * Tab should be loaded trough Ajax call
     *
     * @return bool
     */
    public function isAjaxLoaded()
    {
        return false;
    }
}

Above file, We can declared template file using $_template variable.

protected $_template = ‘tab/customer_view.phtml’ used for template file for our custom tabs.

You can set Custom Tab label by getTabLabel() and set title using getTabTitle() function.You can define custom function for your requirement in above file.

If you want to show content of your custom tab via ajax, you need to return true for isAjaxLoaded() function.

You can get Current customer data by calling Magento\Framework\Registry object.

Now we need to create template file, Path: app/code/Rbj/CustomerTab/view/adminhtml/templates/tab/customer_view.phtml

<?php
/**
 * @var $block \Rbj\CustomerTab\Block\Adminhtml\CustomerEdit\Tab\View
 */
?>

<div class="fieldset-wrapper customer-information">
    <div class="fieldset-wrapper-title">
        <span class="title"><?php /* @escapeNotVerified */
            echo __('Information for new Customer tab') ?></span>
    </div>
    <table class="admin__table-secondary">
        <tbody>
        <?php echo $block->getChildHtml(); ?>
        <tr>
            <th><?php /* @escapeNotVerified */
                echo __('Customer ID:') ?></th>
            <td><?php echo $block->getCustomerId(); ?></td>
        </tr>
        <tr>
            <th><?php /* @escapeNotVerified */
                echo __('Customer History:') ?></th>
            <td><?php echo __('History of Customer display') ?></td>
        </tr>
        </tbody>
    </table>
</div>

Now Run Upgrade command to install our module.
php bin/magento setup:upgrade
php bin/magento cache:flush
php bin/magento indexer:reindex customer_grid

Now Go To Admin panel,
Login with your credentials,
Click on Left Sidebar, Customers -> All Customers Link,
Click on Any Customer, You can show tab as your new custom tab in the Customer edit page.

customer tab
customer tab

Your new tab will be available in the customer edit page.