1

I have made custom module in admin for add/edit and delete.

For a particular field (example email ) in the edit form, I want to show HTML instead of an edit input field.

For example, I am saving values in the database for column email as a@a.com,b@b.com.

When a user edits that form I want to show custom HTML like below

<table>
  <tr>
    <th>EMail</th>
  </tr>
  <tr>
    <td>a@ac.om</td>
  </tr>
  <tr>
    <td>b@b.com</td>
  </tr>
</table>

for applying the custom function to edit field where should I do changes, below is code of Main.php

Vendor/Magento/Block/Adminhtml/Trulookadmin/Edit/Tab/Main.php

protected function _prepareForm()
{
    /* @var $model \Obl\Trulookadmin\Model\BlogPosts */
    $model = $this->_coreRegistry->registry('formadmin');

    $isElementDisabled = false;

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

    $form->setHtmlIdPrefix('page_');

    $fieldset = $form->addFieldset('base_fieldset', ['legend' => __('Item Information')]);

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


    $fieldset->addField(
        'email',
        'text',
        [
            'name' => 'email',
            'label' => __('EMail'),
            'title' => __('EMail'),

            'disabled' => true
        ]
    );
0

I have added code in your block file, Check this.

protected $_rendererFieldset;

public function __construct(
    -------------
    \Magento\Backend\Block\Widget\Form\Renderer\Fieldset $rendererFieldset,
    -------------
)
{
    ------------
    $this->_rendererFieldset = $rendererFieldset;
    ------------
}

protected function _prepareForm()
{
    /* @var $model \Obl\Trulookadmin\Model\BlogPosts */
    $model = $this->_coreRegistry->registry('formadmin');

    $isElementDisabled = false;

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

    $form->setHtmlIdPrefix('page_');

    $fieldset = $form->addFieldset('base_fieldset', ['legend' => __('Item Information')]);

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


    $fieldset->addField(
        'html',
        'text',
        [
            'name' => 'html',
            'label' => __('EMail'),
            'title' => __('EMail')
        ]
    )->setRenderer($this->_rendererFieldset->setTemplate('Vendor_Module::testtable.phtml'));

You need to create a PHTML file in your module for Table HTML and render it on your field in your form.

Hope this will work for you.

| improve this answer | |
  • will i get email values in testtable.phtml file – user1799722 Feb 18 at 9:22
  • You can create another function in the same block file and get values in PHTML file like <td><?= $block->getEmail(); ?></td> – Kishor Thummar Feb 18 at 9:31

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.