In admin form, I created custom field type and then, add html content by custom phtml file.

But, after that label not display with custom field type.

Custom Field Type :

$fieldset->addType(
    'custom_field_labels',
    '\Vendor\Magento\Block\Adminhtml\Module\Edit\Renderer\CustomField'
);

$fieldset->addField(
    'custom_text',
    'custom_field_labels',
    [
        'name' => 'custom_text',
        'label' => __('Custom Text'),
        'title' => __('Custom Text'),
    ]
);

Vendor\Magento\Block\Adminhtml\Module\Edit\Renderer\CustomField.php

<?php

namespace Vendor\Magento\Block\Adminhtml\Module\Edit\Renderer;


class CustomField extends \Magento\Framework\Data\Form\Element\AbstractElement {

    protected $_blockFactory;

    public function __construct(
        \Magento\Framework\View\Element\BlockFactory $_blockFactory
    ) {
        $this->_blockFactory = $_blockFactory;
    }

    /**
     * Get the after element html.
     *
     * @return mixed
     */
    public function getElementHtml() {
        $customDiv = $this->_blockFactory
            ->createBlock('Vendor\Magento\Block\Adminhtml\Module\Edit\Tabs\MyTab')
            ->setTemplate('Vendor_Magento::product/tabs/mytabs.phtml')->toHtml();
        return $customDiv;
    }
}

enter image description here

How can I solve that issue?

Any help would be appreciated.

有帮助吗?

解决方案

Please check __construct() in this file.

Vendor\Magento\Block\Adminhtml\Module\Edit\Renderer\CustomField.php

<?php
namespace Vendor\Magento\Block\Adminhtml\Module\Edit\Renderer;

class CustomField extends \Magento\Framework\Data\Form\Element\AbstractElement
{
    protected $_blockFactory;

    public function __construct(
        \Magento\Framework\Data\Form\Element\Factory $factoryElement,
        \Magento\Framework\Data\Form\Element\CollectionFactory $factoryCollection,
        Escaper $escaper,
        $data = [],
        \Magento\Framework\View\Element\BlockFactory $_blockFactory
    ) {
        $this->_blockFactory = $_blockFactory;
        parent::__construct($factoryElement, $factoryCollection, $escaper, $data);
    }

    /**
     * Get the after element html.
     *
     * @return mixed
     */
    public function getElementHtml() {
        $customDiv = $this->_blockFactory
            ->createBlock('Vendor\Magento\Block\Adminhtml\Module\Edit\Tabs\MyTab')
            ->setTemplate('Vendor_Magento::product/tabs/mytabs.phtml')->toHtml();
        return $customDiv;
    }
}

This will work :)

许可以下: CC-BY-SA归因
scroll top