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 [email protected],[email protected].

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

<table>
  <tr>
    <th>EMail</th>
  </tr>
  <tr>
    <td>[email protected]</td>
  </tr>
  <tr>
    <td>[email protected]</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
        ]
    );
3

1 Answer 1

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.

2
  • will i get email values in testtable.phtml file Feb 18, 2020 at 9:22
  • You can create another function in the same block file and get values in PHTML file like <td><?= $block->getEmail(); ?></td> Feb 18, 2020 at 9:31

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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