TransWikia.com

How to add custom customer attribute in custom tab in customer edit page of admin in magento2

Magento Asked by Mohit chauhan on January 1, 2021

I have created custom customer attribute successfully. Which are showing under account information tab of customer edit in admin.

I want to show them in my custom tab.

One Answer

To create a new tab in the admin customer edit, create a xml in the following path

Step 1:

/app/code/Vendorname/modulename/view/adminhtml/layout/customer_index_edit.xml

<body>
       <referenceBlock name="customer_form">
         <block class="VendornameModulenameBlockAdminhtmlEditTabTest" name="customer_edit_tab_test" >
          <action method="setTabLabel">
              <argument name="label" xsi:type="string">Test Tab</argument>
          </action>
        </block>
      </referenceBlock>
</body>

Step 2: Create a block file in the below path to load the contents when the tab is clicked

/app/code/Vendorname/Modulename/Block/Adminhtml/Edit/Tab/Test.php

<?php

namespace VendornameModulenameBlockAdminhtmlEditTab;

use MagentoCustomerControllerRegistryConstants;

use MagentoUiComponentLayoutTabsTabInterface;

class Test extends MagentoFrameworkViewElementTemplate implements TabInterface
{

    protected $_coreRegistry;

    public function __construct(
        MagentoBackendBlockTemplateContext $context,
        MagentoFrameworkRegistry $registry,
        array $data = []
    ) {
        $this->_coreRegistry = $registry;
        parent::__construct($context, $data);
    }

    /**
     * @return string|null
     */
    public function getCustomerId()
    {
        return $this->_coreRegistry->registry(RegistryConstants::CURRENT_CUSTOMER_ID);
    }
    /**
     * @return MagentoFrameworkPhrase
     */
    public function getTabLabel()
    {
        return __(‘Tab label’);
    }
    /**
     * @return MagentoFrameworkPhrase
     */
    public function getTabTitle()
    {
        return __(‘Tab Title’);
    }
    /**
     * @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()
    {
    //replace the tab with the url you want
        return $this->getUrl('test/*/test', ['_current' => true]);
    }
    /**
     * Tab should be loaded trough Ajax call
     *
     * @return bool
     */
    public function isAjaxLoaded()
    {
        return true;
    }
}

Your new test tab will be added to customer edit left side menu.

Answered by nishu on January 1, 2021

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP