1

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.

1 Answer 1

2

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 :)

2
  • Why object manager?
    – Niket
    Commented Oct 14, 2019 at 8:00
  • I've updated content for this file, please check this code that will definitely works now. :) Commented Oct 14, 2019 at 8:56

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.