Magento – How to add phtml file at edit tab of Magento 2 custom module in admin

admincustommagento2module

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>

Best Answer

  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;
      }
    }
    
Related Topic