4

I tried to create a new tab in customer edit backend.

First i create the app/code/Namespace/Point/view/adminhtml/layout/customer_index_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="customer_form">
             <block class="Namespace\Point\Block\Adminhtml\Edit\Tab\CustomerPoint" name="customer_edit_tab_customerpoint" >
              <action method="setTabLabel">
                  <argument name="label" xsi:type="string">Namespace Point</argument>
              </action>
            </block>
          </referenceBlock>
    </body>
</page>

then i create the block:

app/code/Namespace/Point/Block/Adminhtml/Edit/Tab/CustomerPoint.php

namespace Namespace\Point\Block\Adminhtml\Edit\Tab;
use Magento\Customer\Controller\RegistryConstants;
use Magento\Ui\Component\Layout\Tabs\TabInterface;

class CustomerPoint extends \Magento\Framework\View\Element\Template implements TabInterface
{

    protected $_coreRegistry; 

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

    public function getCustomerId()
    {
        return $this->_coreRegistry->registry(RegistryConstants::CURRENT_CUSTOMER_ID);
    }

    public function getTabLabel()
    {
        return __("Namespace Point");
    }

    public function getTabTitle()
    {
        return __('Namespace Point');
    }

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

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

    public function getTabClass()
    {
        return '';
    }

    public function getTabUrl()
    {
      return $this->getUrl('namespace_point/customerpoint/tab', ['customer_id'=>$this->getCustomerId(),'_current' => true]);
    }

    public function isAjaxLoaded()
    {
        return true;
    }
}

at this point the tab is showing in customer edit admin. Then i create the route app/code/Namespace/Point/etc/adminhtml/routes.xml

<?xml version="1.0" ?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:App/etc/routes.xsd">
    <router id="admin">
        <route frontName="namespace_point" id="namespace_point">
            <module before="Magento_Backend" name="Namespace_Point"/>
        </route>
    </router>
</config>

then the controller app/code/Namespace/Point/Controller/Adminhtml/Customerpoint/Tab.php

namespace Namespace\Point\Controller\Adminhtml\CustomerPoint;

class Tab extends \Magento\Customer\Controller\Adminhtml\Index
{

    public function execute()
    {
      $this->initCurrentCustomer();
      $resultLayout = $this->resultLayoutFactory->create();
      return $resultLayout;
    }
}

then i create layout for this controller layout app/code/Namespace/Point/view/adminhtml/layout/namespace_point_customerpoint_tab.xml

<?xml version="1.0"?>
<layout xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:View/Layout/etc/page_configuration.xsd">
    <container name="root" label="Root">
        <block class="Namespace\Point\Block\Adminhtml\Edit\Tab\CustomerPoint" name="point.edit.tab.customerpoint"  template="Namespace_Point::customer_point_tab.phtml" cacheable="false"/> 
    </container>
</layout>

and finally the templates file app/code/Namespace/Point/view/adminhtml/templates/customer_point_tab.phtml

<h1> POINT </h1>

but when i click the new customer tab, it gives me blank page, when i use the tab url in my browser, it gives me blank page too, when i add die('test'); at the controller it prints test but when i add die('test'); in the block constructor it gives blank page, seems like the controller doesn't read the xml layout file to generate

  • please correct this : from = * @var \Magento\Framework\Registry */ protected $_coreRegistry; To = /* @var \Magento\Framework\Registry */ protected $_coreRegistry; – kunj Aug 31 '18 at 8:32
  • @kunj sorry its typo, but still not working – mileven Aug 31 '18 at 8:34
  • Try after cache flush and di compile, it is working for me but it will display only on customer edit not in customer create – kunj Aug 31 '18 at 8:42
  • I have updated answer – kunj Aug 31 '18 at 10:22
0

please correct this:

your layout should be like this:

<?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>
        <container name="root" label="Root">
            <block class="Namespace\Point\Block\Adminhtml\Edit\Tab\CustomerPoint" name="point.edit.tab.customerpoint"  template="Namespace_Point::customer_point_tab.phtml" cacheable="false"/>
        </container>
    </body>
</page>
  • Try after cache flush and di compile, it is working for me but it will display only on customer edit not in customer create – kunj Aug 31 '18 at 8:42

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.