Magento 2 add tab in admin form

When building a Magento extension or do a project in magento 2, some time you have to update a lot of data in a form so you need add new tab in form. For example, I want to build extension to Manager Members, The member has a lot of Information so I will group them to some group such as Base Information, Address Information , etc … .

Magento 2 add tab in admin form

In order to add new tab in admin form, you can do 2 task :
– Declare new Tab
– Create Tab

1. Declare new Tab

You can Declare new Tab in app/code/Vendor/Module/Block/Adminhtml/CustomData/Edit/Tabs.php.
For my example: I will add code to function _beforeToHtml() in file
app/code/Magebay/Hello/Adminhtml/Members/Edit/Tabs.php

<?php
 
namespace Magebay\Hello\Block\Adminhtml\Members\Edit;
 
use Magento\Backend\Block\Widget\Tabs as WidgetTabs;
 
class Tabs extends WidgetTabs
{
    // other functions 
    protected function _beforeToHtml()
    {
       //other tabs 
        $this->addTab(
            'address_info',
            [
                'label' => __('General'),
                'title' => __('General'),
                'content' => $this->getLayout()->createBlock(
                    'Magebay\Hello\Block\Adminhtml\Members\Edit\Tab\Info'
                )->toHtml(),
                'active' => true
            ]
        );
 
        return parent::_beforeToHtml();
    }
}
 

2. Create Tab

Next , Will will add address fields to class ‘Magebay\Hello\Block\Adminhtml\Members\Edit\Tab\Info

<?php
 
namespace Magebay\Hello\Block\Adminhtml\Members\Edit\Tab;
 
use Magento\Backend\Block\Widget\Form\Generic;
use Magento\Backend\Block\Widget\Tab\TabInterface;
use Magento\Backend\Block\Template\Context;
use Magento\Framework\Registry;
use Magento\Framework\Data\FormFactory;
use Magento\Cms\Model\Wysiwyg\Config;
use Magebay\Hello\Model\System\Config\Status;
 
class Info extends Generic implements TabInterface
{
    /**
     * @var \Magento\Cms\Model\Wysiwyg\Config
     */
    protected $_wysiwygConfig;
 
    /**
     * @var \Magebay\Hello\Model\System\Config\Status
     */
    protected $_status;
   /**
     * @param Context $context
     * @param Registry $registry
     * @param FormFactory $formFactory
     * @param Config $wysiwygConfig
     * @param Status $status
     * @param array $data
     */
    public function __construct(
        Context $context,
        Registry $registry,
        FormFactory $formFactory,
        Config $wysiwygConfig,
        Status $status,
        array $data = []
    ) {
        $this->_wysiwygConfig = $wysiwygConfig;
        $this->_status = $status;
        parent::__construct($context, $registry, $formFactory, $data);
    }
 
    /**
     * Prepare form fields
     *
     * @return \Magento\Backend\Block\Widget\Form
     */
    protected function _prepareForm()
    {
       /** @var $model \Magebay\Hello\Model\PostsFactory */
        $model = $this->_coreRegistry->registry('member_data');
 
        /** @var \Magento\Framework\Data\Form $form */
        $form = $this->_formFactory->create();
        $form->setHtmlIdPrefix('members_');
        $form->setFieldNameSuffix('members');
 
        $fieldset = $form->addFieldset(
            'base_fieldset',
            ['legend' => __('Address Information')]
        );
 
        if ($model->getId()) {
            $fieldset->addField(
                'address_id',
                'hidden',
                ['name' => 'address_id']
            );
        }
        $fieldset->addField(
            'title',
            'text',
            [
                'name'      => 'city',
                'label'     => __('City')
            ]
        );
        $data = $model->getData();
        $form->setValues($data);
        $this->setForm($form);
 
        return parent::_prepareForm();
    }
}

As you can see , I have added new tab in admin form . I hope the post is useful for you. We have a lot of great Magento extension like Magento Multi Vendor Marketplace Extension , Booking system Extension.if you want to have custom extension for your site, you can contact to [email protected]