cancel
Showing results for 
Search instead for 
Did you mean: 

Move customer custom attribute inside custom tab

Move customer custom attribute inside custom tab

Hello Magento Community,

I have created the custom customer attribute (Yes/No).It displayed in account information tab, I  want to move the custom attribute inside custom tab.
Looking forward to hear from anyone asap.
Thanks for your help
balsingh

2 REPLIES 2

Re: Move customer custom attribute inside custom tab

@balsingh The attributes under the account information tab are rendered generically based on a setting you can add to the customer attribute (include in forms, it's an own mapping table). If you have a custom tab, you have to render it yourself, but the "how" depends on how you implemented the custom tab.

 

Hope it helps!

 

Thanks

Re: Move customer custom attribute inside custom tab

Hello @rahul Gupta 

Thanks for the reply,
I have created tab using block, Please check the attached code.its not saving the attribute value. can you please look into this and guide me how to proceed further in this

<?php
namespace Hemag\EDI\Block\Adminhtml\Customer\Edit;

use Magento\Customer\Controller\RegistryConstants;
use Magento\Ui\Component\Layout\Tabs\TabInterface;
use Magento\Backend\Block\Widget\Form;
use Magento\Backend\Block\Widget\Form\Generic;
use Magento\Customer\Api\CustomerRepositoryInterface;
/**
 * Customer account form block
 */
class Tabs extends Generic implements TabInterface
{
     /**
     * @var \Magento\Store\Model\System\Store
     */
    protected $_systemStore;
    /**
     * Core registry
     *
     * @var \Magento\Framework\Registry
     */
    protected $_coreRegistry;
    protected $customerRepository;
    /**
     * @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,
        \Magento\Framework\Data\FormFactory $formFactory,
        \Magento\Store\Model\System\Store $systemStore,
        CustomerRepositoryInterface $customerRepository,
        array $data = []
    ) {
        $this->_coreRegistry = $registry;
        $this->_systemStore = $systemStore;
        $this->customerRepository = $customerRepository;
        parent::__construct($context, $registry, $formFactory, $data);
    }

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

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

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

    /**
     * @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;
    }
    public function getAttributeValue()
    {
        $customerId = $this->getCustomerId();
        if($customerId){
            $customer = $this->customerRepository->getById($customerId);
            $customerdata = $customer->getCustomAttribute('hemag_edi_api_customer_allowed');
            if($customerdata){
                return $customerdata->getValue();
            }else{
               return 0; 
            }
        }else{
            return 0; 
        }
        
        
    }
    public function initForm()
    {
        if (!$this->canShowTab()) {
            return $this;
        }
     
        $form = $this->_formFactory->create();
        $form->setHtmlIdPrefix('myform_');
        
        $fieldset = $form->addFieldset('base_fieldset', ['legend' => __('Enterprice Data Integration')]);
            
            $fieldset->addField(
                'hemag_edi_api_customer_allowed',
                'checkbox',
                [
                    'name' => 'hemag_edi_api_customer_allowed',
                    'data-form-part' => $this->getData('customer_form'),
                    'label' => __('Enable API Access'),
                    'title' => __('If enabled, the customer is allowed to access the EDI API.'),
                    'value' => $this->getAttributeValue(),
                    'checked' => $this->getAttributeValue(),
                    'tabindex' => 1,
                    'after_element_html' => '<small>If enabled, the customer is allowed to access the EDI API.</small>',
                ]
            );
        $this->setForm($form);
        return $this;
    }
    /**
     * @return string
     */
    protected function _toHtml()
    {
        if ($this->canShowTab()) {
            $this->initForm();
            return parent::_toHtml();
        } else {
            return '';
        }
    }


}

<?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>
        <referenceBlock name="customer_form">
        	 <block class="Hemag\EDI\Block\Adminhtml\Customer\Edit\Tabs" name="hemag_edi_edit_tab_view" />
        </referenceBlock>    
    </body>
</page>

Thanks