How to link directly to custom customer tab in admin
-
15-04-2021 - |
質問
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.
正しい解決策はありません
所属していません magento.stackexchange