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.

asked Oct 14, 2019 at 5:42

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

answered Oct 14, 2019 at 7:58
2
  • Why object manager? 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

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.