0

I've created a custom tab that displays some information in the Customer Admin section. I'm just curious if there's a way to link directly to this tab so it's opened on page load.

I don't want it the default active tab, but just a way to link directly to it and it would be active on page load.

Here's how I created the tab:

MyCompany/MyModule/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">
  <head>
    <css src="MyCompany_MyModule::css/styles.css"/>
  </head>
  <body>
    <referenceBlock name="customer_form">
      <block class="MyCompany\MyModule\Block\Admin\Customer\Tab\CustomTab" name="customer_custom_tab" cacheable="false">
        <arguments>
          <argument name="tab_label" xsi:type="string" translate="true">Custom Tab</argument>
          <argument name="sort_order" xsi:type="number">50</argument>
        </arguments>
      </block>
    </referenceBlock>
  </body>
</page>

MyCompany/MyModule/Block/Admin/Customer/Tab/CustomTab.php

<?php

namespace MyCompany\MyModule\Block\Admin\Customer\Tab;

class CustomTab extends \Magento\Backend\Block\Template implements \Magento\Ui\Component\Layout\Tabs\TabInterface
{
  protected $_template = 'customer/customtab.phtml';

  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(\Magento\Customer\Controller\RegistryConstants::CURRENT_CUSTOMER_ID);
  }

  public function getTabLabel()
  {
    return __('Custom Tab');
  }

  public function getTabTitle()
  {
    return __('Custom Tab');
  }

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

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

  public function getTabClass()
  {
    return '_active';
  }

  public function getTabUrl()
  {
    return '';
  }

  public function isAjaxLoaded()
  {
    return false;
  }

}

This correctly displays my custom customer tab with content from customtab.phtml file. Just not sure how to set this tab as active via direct url.

2
  • Can you please clarify your question more? You said that "I don't want it the default active tab, but just a way to link directly to it and it would be active on page load." Do you mean to add link to open this tab? – ZealousWeb Mar 4 at 5:22
  • @ZealousWeb For example, on sales/orders you can link directly to an order and have it open the credit memo tab via https://example.com/admin/sales/order/view/order_id/22369/active_tab/order_creditmemos but I haven't found the equivalent for customer acounts – AJK Mar 4 at 13:36

Your Answer

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

Browse other questions tagged or ask your own question.