大米CMS官网论坛,大米站长联盟,大米站长之家,大米开发者社区

 找回密码
 注册大米会员

QQ登录

只需一步,快速开始

查看: 3882|回复: 0
打印 上一主题 下一主题

magento2 how to add tab for manager some customer

[复制链接]

501

主题

778

帖子

7664

积分

超级版主

Rank: 8Rank: 8

积分
7664

授权用户

跳转到指定楼层
楼主
发表于 2020-11-24 19:28:19 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式

STEP 1:

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

/app/code/Vendorname/modulename/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="Vendorname\Modulename\Block\Adminhtml\Edit\Tab\Blockname" name="customer_edit_tab_credit" >



  •               <action method="setTabLabel">



  •                   <argument name="label" xsi:type="string">Customer Credit</argument>



  •               </action>



  •             </block>



  •           </referenceBlock>



  •     </body>



  • </page>


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/Credit.php



  • <?php



  • namespace Vendorname \ Modulename \Block\Adminhtml\Edit\Tab;



  • use Magento\Customer\Controller\RegistryConstants;



  • use Magento\Ui\Component\Layout\Tabs\TabInterface;







  • class Credit  extends \Magento\Framework\View\Element\Template implements TabInterface



  • {



  •     /**



  •      * Core registry



  •      *



  •      * @var \Magento\Framework\Registry



  •      */



  •     protected $_coreRegistry;



  •     /**



  •      * @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,



  •         array $data = []



  •     ) {



  •         $this->_coreRegistry = $registry;



  •         parent::__construct($context, $data);



  •     }







  •     /**



  •      * @return string|null



  •      */



  •     public function getCustomerId()



  •     {



  •         return $this->_coreRegistry->registry(RegistryConstants::CURRENT_CUSTOMER_ID);



  •     }



  •     /**



  •      * @return \Magento\Framework\Phrase



  •      */



  •     public function getTabLabel()



  •     {



  •         return __(‘Tab label’);



  •     }



  •     /**



  •      * @return \Magento\Framework\Phrase



  •      */



  •     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('credit/*/credit', ['_current' => true]);



  •     }



  •     /**



  •      * Tab should be loaded trough Ajax call



  •      *



  •      * @return bool



  •      */



  •     public function isAjaxLoaded()



  •     {



  •         return true;



  •     }



  • }


I have the block name as “Credit”. The block implements from TabInterface. The most important function in this class is : getTabUrl() and isAjaxLoaded() :

getTabUrl() contain the code return $this->getUrl(credit/*/credit, [‘_current’ => true]); equal go to action and execute the complete path of your controller in Magento 2.

STEP 3:

Following is the routes.xml for creating a controller to make the url valid.

/app/code/Vendorname/Modulename/etc/adminhtml/routes.xml



  • <?xml version="1.0"?>



  • <!--



  • /**



  • * Copyright © 2016 Magento. All rights reserved.



  • * See COPYING.txt for license details.



  • */



  • -->



  • <config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:App/etc/routes.xsd">



  •     <router id="admin">



  •         <route id="credit" frontName="credit">



  •             <module name="Vendorname_Modulename" />



  •         </route>



  •     </router>



  • </config>


STEP 4:

Lets now create a controller with the class name Credit in the below path :

/app/code/Vendorname/Modulename/Controller/Adminhtml/Index/Credit.php



  • <?php



  • /**



  • * Copyright © 2015 Magento. All rights reserved.



  • * See COPYING.txt for license details.



  • */



  • namespace Vendorname\Modulename\Controller\Adminhtml\Index;







  • class Credit extends \Magento\Customer\Controller\Adminhtml\Index



  • {



  •     /**



  •      * Customer compare grid



  •      *



  •      * @return \Magento\Framework\View\Result\Layout



  •      */



  •     public function execute()



  •     {







  •         $this->initCurrentCustomer();



  •         $resultLayout = $this->resultLayoutFactory->create();



  •         return $resultLayout;



  •     }











  • }


STEP 5:

Create another xml to mention which phtml file has to load

/app/code/Vendorname/Modulename/view/adminhtml/layout/credit_index_credit.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="content" label="Root">



  •         <block class="\Vendorname\Modulename\Block\Adminhtml\Edit\Tab\Tabname" name="Credit.edit.tab.credit"  template="Vendorname_Modulename::credit.phtml" />







  •     </container>







  • </layout>


STEP 6:

Finally create the phtml file and type your text or implement your logics with block and controller.

/app/code/Vendorname/Modulename/view/adminhtml/templates/credit.phtml

This is our new tab.

Now clear your cache. System – > Cache Management – > Flush Magento Cache

After clearing you cache, your admin will look like the image below…

customer admin edit magento 2


分享到:  QQ好友和群QQ好友和群 QQ空间QQ空间 腾讯微博腾讯微博 腾讯朋友腾讯朋友
收藏收藏 分享分享
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 注册大米会员

本版积分规则

QQ|小黑屋|大米CMS社区 ( 蜀ICP备11002200号-2广告联系:广告联系 

Powered by 大米CMS

© 2010-2020 大米CMS Inc.

快速回复 返回顶部 返回列表