1

I have created custom form in magento2 admin panel.

Now I want to add one more section same as Tab, let say 'Tab1'.

enter image description here

This is my general.php file.

Namespace/Modulename/Block/Adminhtml/Custom/Edit/Tab/General.php

<?php
namespace Bls\Stdprofile\Block\Adminhtml\Custom\Edit\Tab;
class Tab extends \Magento\Backend\Block\Widget\Form\Generic implements \Magento\Backend\Block\Widget\Tab\TabInterface
{
    /**
     * @var \Magento\Store\Model\System\Store
     */
    protected $_systemStore;

    /**
     * @param \Magento\Backend\Block\Template\Context $context
     * @param \Magento\Framework\Registry $registry
     * @param \Magento\Framework\Data\FormFactory $formFactory
     * @param \Magento\Store\Model\System\Store $systemStore
     * @param array $data
     */
    public function __construct(
        \Magento\Backend\Block\Template\Context $context,
        \Magento\Framework\Registry $registry,
        \Magento\Framework\Data\FormFactory $formFactory,
        \Magento\Store\Model\System\Store $systemStore,
        array $data = array()
    ) {
        $this->_systemStore = $systemStore;
        parent::__construct($context, $registry, $formFactory, $data);
    }

    /**
     * Prepare form
     *
     * @return $this
     */
    protected function _prepareForm()
    {
      /* @var $model \Magento\Cms\Model\Page */
      $model = $this->_coreRegistry->registry('stdprofile_custom');
      $isElementDisabled = false;
      /** @var \Magento\Framework\Data\Form $form */
      $form = $this->_formFactory->create();

      $form->setHtmlIdPrefix('page_');

      $fieldset = $form->addFieldset('base_fieldset', array('legend' => __('Tab')));

      if ($model->getId()) {
        $fieldset->addField('id', 'hidden', array('name' => 'id'));
    }

    $fieldset->addField(
        'col1',
        'text',
        array(
            'name' => 'col1',
            'label' => __('Company Name:'),
            'title' => __('Company Name:'),
            /*'required' => true,*/
        )
    );
    $fieldset->addField(
        'col2',
        'text',
        array(
            'name' => 'col2',
            'label' => __('Address:'),
            'title' => __('Address:'),
            /*'required' => true,*/
        )
    );

    if (!$model->getId()) {
        $model->setData('status', $isElementDisabled ? '2' : '1');
    }

    $form->setValues($model->getData());
    $this->setForm($form);

    return parent::_prepareForm();   
}

    /**
     * Prepare label for tab
     *
     * @return string
     */
    public function getTabLabel()
    {
        return __('Tab');
    }

    /**
     * Prepare title for tab
     *
     * @return string
     */
    public function getTabTitle()
    {
        return __('Tab');
    }

    /**
     * {@inheritdoc}
     */
    public function canShowTab()
    {
        return true;
    }

    /**
     * {@inheritdoc}
     */
    public function isHidden()
    {
        return false;
    }

    /**
     * Check permission for passed action
     *
     * @param string $resourceId
     * @return bool
     */
    protected function _isAllowedAction($resourceId)
    {
        return $this->_authorization->isAllowed($resourceId);
    }
}

1 Answer 1

1

Add a multiple tabs in admin form by below code

Namespace/Modulename/Block/Adminhtml/Custom/Edit/Tab/General.php

 <?php
    namespace Bls\Stdprofile\Block\Adminhtml\Custom\Edit\Tab;
    class Tab extends \Magento\Backend\Block\Widget\Form\Generic implements \Magento\Backend\Block\Widget\Tab\TabInterface
    {
        /**
         * @var \Magento\Store\Model\System\Store
         */
        protected $_systemStore;

        /**
         * @param \Magento\Backend\Block\Template\Context $context
         * @param \Magento\Framework\Registry $registry
         * @param \Magento\Framework\Data\FormFactory $formFactory
         * @param \Magento\Store\Model\System\Store $systemStore
         * @param array $data
         */
        public function __construct(
            \Magento\Backend\Block\Template\Context $context,
            \Magento\Framework\Registry $registry,
            \Magento\Framework\Data\FormFactory $formFactory,
            \Magento\Store\Model\System\Store $systemStore,
            array $data = array()
        ) {
            $this->_systemStore = $systemStore;
            parent::__construct($context, $registry, $formFactory, $data);
        }

        /**
         * Prepare form
         *
         * @return $this
         */
        protected function _prepareForm()
        {
          /* @var $model \Magento\Cms\Model\Page */
          $model = $this->_coreRegistry->registry('stdprofile_custom');
          $isElementDisabled = false;
          /** @var \Magento\Framework\Data\Form $form */
          $form = $this->_formFactory->create();

          $form->setHtmlIdPrefix('page_');

          $fieldset = $form->addFieldset('base_fieldset', array('legend' => __('Tab')));

          if ($model->getId()) {
            $fieldset->addField('id', 'hidden', array('name' => 'id'));
        }

        $fieldset->addField(
            'col1',
            'text',
            array(
                'name' => 'col1',
                'label' => __('Company Name:'),
                'title' => __('Company Name:'),
                /*'required' => true,*/
            )
        );
        $fieldset->addField(
            'col2',
            'text',
            array(
                'name' => 'col2',
                'label' => __('Address:'),
                'title' => __('Address:'),
                /*'required' => true,*/
            )
        );
      //multiple tabs in admin form
      $second_fieldset = $form->addFieldset('second_fieldset', array('legend' => __('Tab1')));

        $second_fieldset ->addField(
            'tab1_col1',
            'text',
            array(
                'name' => 'tab1_col1',
                'label' => __('Required Name:'),
                'title' => __('Required Name:'),
            )
        );

        if (!$model->getId()) {
            $model->setData('status', $isElementDisabled ? '2' : '1');
        }

        $form->setValues($model->getData());
        $this->setForm($form);

        return parent::_prepareForm();   
    }

        /**
         * Prepare label for tab
         *
         * @return string
         */
        public function getTabLabel()
        {
            return __('Tab');
        }

        /**
         * Prepare title for tab
         *
         * @return string
         */
        public function getTabTitle()
        {
            return __('Tab');
        }

        /**
         * {@inheritdoc}
         */
        public function canShowTab()
        {
            return true;
        }

        /**
         * {@inheritdoc}
         */
        public function isHidden()
        {
            return false;
        }

        /**
         * Check permission for passed action
         *
         * @param string $resourceId
         * @return bool
         */
        protected function _isAllowedAction($resourceId)
        {
            return $this->_authorization->isAllowed($resourceId);
        }
    }

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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