0

I have created a custom module. When we click on the admin grid to edit, it opens edit page. In that I am showing tabs. When I click on that tab I need to show template file. If you see the below screen you can get ideaenter image description here

this is my tab file

    <?php
/**
  /**
 * Exinent_Customerids Module 
 *
 * @category    customer
 * @package    
 * @author      pawan
 *
 */

namespace Vendor\Modulename\Block\Adminhtml\Customerids\Edit\Tab;

/**
 * customerid post edit form main tab
 */
use Magento\Backend\Block\Template\Context;
use Magento\Framework\Registry;

class Meat extends \Magento\Backend\Block\Template
{
//    const IBAN_TEMPLATE = 'Exinent_Customerids::test.phtml';
     protected $_template = 'test.phtml';
    protected $_coreRegistry = null;

//   public function __construct(
//        \Magento\Backend\Block\Widget\Context $context,
//        array $data = []
//    ) {
//        parent::__construct($context, $data);
//    }

//   protected function _prepareLayout()
//    {
//        parent::_prepareLayout();
//        if (!$this->getTemplate()) {
//            $this->setTemplate(static::IBAN_TEMPLATE);
//        }
//
//        return $this;
//    }
}

my layout file

<block class="Vendor\Modulename\Block\Adminhtml\Customerids\Edit\Tab\Meat" name="customerids_customerids_edit_tab_meat"/>
                <arguments>
                    <argument name="config" xsi:type="array">
                        <item name="label" xsi:type="string" translate="true">customerids_customerids_edit_tab_meat</item>
                        <item name="collapsible" xsi:type="boolean">true</item>
                        <item name="opened" xsi:type="boolean">true</item>
                        <item name="sortOrder" xsi:type="string">2</item>
                        <item name="canShow" xsi:type="boolean">true</item>
                        <item name="componentType" xsi:type="string">fieldset</item>
                    </argument>
                </arguments>
3

2 Answers 2

0
  1. You need to confirm that, you create test.phtml file at this location.
    Vendor\Modulename\view\adminhtml\templates\test.phtml

  2. You need to replace _prepareLayout function code like this.

    <?php
    namespace Vendor\Modulename\Block\Adminhtml\Customerids\Edit\Tab;
    
    use Magento\Backend\Block\Template\Context;
    use Magento\Framework\Registry;
    
    class Meat extends \Magento\Backend\Block\Template
    {
    
      protected function _prepareLayout()
      {
          $customhtml = parent::_prepareLayout();
          $customhtml .= $this->setTemplate('Vendor_Modulename::test.phtml')->toHtml();
          return $customhtml;
      }
    }
    
3
0

1.In the layout file you can add the code :

<referenceContainer name="left">
    <block class="Vendor\Modulename\Block\Adminhtml\Customerids\Edit\Tab" name="customerids_customerids_edit_tab">
        <block class="Vendor\Modulename\Block\Adminhtml\Customerids\Edit\Tab\Child" name="customerids_customerids_edit_tab_child"/>                           
        <action method="addTab">
            <argument name="name" xsi:type="string">child_section</argument>
            <argument name="block" xsi:type="string">customerids_customerids_edit_tab_child</argument>
        </action>               
    </block>
</referenceContainer>

2.Create the new file on the path :

Vendor\Modulename\Block\Adminhtml\Customerids\Edit\Tab\Child.php

With content :

namespace Vendor\Modulename\Block\Adminhtml\Customerids\Edit\Tab;

class Child extends \Magento\Backend\Block\Widget\Form\Generic implements \Magento\Backend\Block\Widget\Tab\TabInterface
{
    /**
     * @var \Magento\Backend\Block\Widget\Form\Renderer\Fieldset
     */
    protected $_rendererFieldset;
    /**
     * @var \Magento\Store\Model\System\Store
     */
    protected $_systemStore;
    protected $_customerFactory;    
    protected $_resource;   

    /**
     * @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,
        \Magento\Backend\Block\Widget\Form\Renderer\Fieldset $rendererFieldset,
        \Magento\Customer\Model\CustomerFactory $customerFactory,   
        \Magento\Framework\App\ResourceConnection $resource,  
        array $data = []
    ) {
        $this->_systemStore = $systemStore;
        $this->_rendererFieldset = $rendererFieldset;
        $this->_customerFactory = $customerFactory;  
        $this->_resource = $resource;  
        parent::__construct($context, $registry, $formFactory, $data);
    }

    /**
     * Prepare form
     *
     * @return $this
     */
    protected function _prepareForm()
    {
        /* @var $model \Magefan\Blog\Model\Category */
        $model = $this->_coreRegistry->registry('current_model');

        /*
         * Checking if user have permissions to save information
         */
        $isElementDisabled = !$this->_isAllowedAction('Vendor_Modulename::child');

        /** @var \Magento\Framework\Data\Form $form */
        $form = $this->_formFactory->create();

        $form->setHtmlIdPrefix('child_');

        $fieldset = $form->addFieldset('base_fieldset', ['legend' => __('Add Your Child')]);

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

        $fieldset->addField(
            'html', 
            'text', 
            [
                'name' => 'html',
                'label' => __('Html Content'),
                'title' => __('Html Content'),
                'required' => true,
                'disabled' => $isElementDisabled                 
            ]
        )->setRenderer($this->_rendererFieldset->setTemplate('Vendor_Modulename::child.phtml'));   

        if (!$model->getId()) {
            $model->setData('status', $isElementDisabled ? '0' : '1');
        }                       
        $this->_eventManager->dispatch('vendor_modulename_edit_tab_main_prepare_form', ['form' => $form]);

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

        return parent::_prepareForm();
    }

    /**
     * Prepare label for tab
     *
     * @return \Magento\Framework\Phrase
     */
    public function getTabLabel()
    {
        return __('Html Content');
    }

    /**
     * Prepare title for tab
     *
     * @return \Magento\Framework\Phrase
     */
    public function getTabTitle()
    {
        return __('Html Content');
    }

    /**
     * Returns status flag about this tab can be shown or not
     *
     * @return bool
     */
    public function canShowTab()
    {
        return true;
    }

    /**
     * Returns status flag about this tab hidden or not
     *
     * @return bool
     */
    public function isHidden()
    {
        return false;
    }

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

3.Create file :

Vendor/Modulename/view/adminhtml/templates/child.phtml

And put your content here

Hope it helps !

1
  • How can we access variables defined in prepareForm function to template file?
    – Khushbu
    Commented Jul 12, 2019 at 10:51

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.