1

I want to add phtml file in Grid\Edit which is contain image or other html part

Following Form.php file

<?php

namespace Vandor\Namespace\Block\Adminhtml\Grid\Edit;

use Magento\Backend\Block\Widget\Form\Generic;
use Magento\Backend\Block\Template\Context;
use Magento\Framework\Registry;
use Magento\Framework\Data\FormFactory;
use Magento\Cms\Model\Wysiwyg\Config;
class Form extends Generic
{
    protected $systemStore;
    protected $wysiwygConfig;

    public function __construct(
        Context $context,
        Registry $registry,
        FormFactory $formFactory,
        Config $wysiwygConfig,
        array $data = []
    )
    {
        $this->wysiwygConfig = $wysiwygConfig;
        parent::__construct($context, $registry, $formFactory, $data);
    }

    protected function _prepareForm()
    {
        $model = $this->_coreRegistry->registry('row_data');
        $form = $this->_formFactory->create(
            ['data' => [
                'id' => 'edit_form',
                'enctype' => 'multipart/form-data',
                'action' => $this->getData('action'),
                'method' => 'post'
            ]
            ]
        );

        $form->setHtmlIdPrefix('namegrid');
        $fieldset = $form->addFieldset(
                'base_fieldset',
                ['legend' => __('Edit Detail'), 'class' => 'fieldset-wide']
            );
        $fieldset->addField('id', 'hidden', ['name' => 'id']);

        $fieldset->addField(
            'title',
            'text',
            [
                'name' => 'title',
                'label' => __('Title'),
                'id' => 'title',
                'title' => __('Title'),
                'class' => 'required-entry',
                'required' => true,
            ]
        );

        $wysiwygConfig = $this->wysiwygConfig->getConfig(['tab_id' => $this->getTabId()]);

        $fieldset->addField(
            'content',
            'editor',
            [
                'name' => 'content',
                'label' => __('Content'),
                'style' => 'height:36em;',
                'required' => true,
                'config' => $wysiwygConfig
            ]
        );
        $form->setValues($model->getData());
        $form->setUseContainer(true);
        $this->setForm($form);
        return parent::_prepareForm();
    }
    public function getFormHtml()
    {
        $html = $this->setTemplate('Vandor_Namespace::grid/image.phtml')->toHtml();
        $html .= parent::getFormHtml();

        return $html;
    }
}

Its show top of the main container. Which is better way to add phtml in between form container

3
  • you just want to add images right??? you can do it by using by creating custom field type Mar 16, 2018 at 10:31
  • @jigsparmar i want to add html elements Mar 16, 2018 at 11:00
  • i add answer please check.it work for me in my custom extension Mar 16, 2018 at 11:04

2 Answers 2

1

To add Image In Edit form You need to add new Type

add below code in your Form.php file

$fieldset->addType(
            'thumbnail',
            '\Vendor\Extension\Block\Adminhtml\Proindex\Edit\Renderer\Thumbnail'
        );

And in that Renderer.php file

you can write below code

namespace Vendor\Extension\Block\Adminhtml\Proindex\Edit\Renderer;

use Magento\Framework\Data\Form\Element\Factory;
use Magento\Framework\Data\Form\Element\CollectionFactory;
use Magento\Framework\Escaper;

class Thumbnail extends \Magento\Framework\Data\Form\Element\AbstractElement
{
    protected $blockdata;
    protected $request;

    public function __construct(Factory $factoryElement,
    CollectionFactory $factoryCollection,
    Escaper $escaper,
    \Magento\Framework\App\Request\Http $request
    )
    {
        $this->blockdata = $blockdata;
        $this->request = $request;
        parent::__construct($factoryElement,$factoryCollection,$escaper);
    }

    public function getAfterElementHtml()
    {
        // here you can write your code.
        $html = '';
        if ($this->getValue()) 
        {
            $html = '<p>simple text.</p>';

        }
        return $html;
    }
}
4
  • how add phtml file in your code ? Mar 16, 2018 at 11:06
  • i add Block file you can write code there Mar 16, 2018 at 11:08
  • Vendor\Extension\Block\Adminhtml\Proindex\Edit\Renderer\Thumbnail Mar 16, 2018 at 11:08
  • Didn't work for me! Feb 22, 2019 at 6:51
0

Try this way it's worked for me.

You can call other Block also by using this function if you want to add phtml file.

public function getFormHtml()
{
    $html = parent::getFormHtml();
    $html .= $this->getLayout()->createBlock(
        'Digicel\IBAN\Block\Adminhtml\Customer\Edit\Tab\IbanEditBlock'
    )->toHtml();
    return $html;
}

IbanEditBlock.php

<?php

namespace Digicel\IBAN\Block\Adminhtml\Customer\Edit\Tab;

class IbanEditBlock extends \Magento\Config\Block\System\Config\Form\Field
{
    const IBAN_TEMPLATE = 'customer/ibanform.phtml';

    /**
     * @param \Magento\Backend\Block\Widget\Context $context
     * @param array                                 $data
     */
    public function __construct(
        \Magento\Backend\Block\Widget\Context $context,
        array $data = []
    ) {
        parent::__construct($context, $data);
    }

    /**
     * Set JS template to itself.
     *
     * @return $this
     */
    protected function _prepareLayout()
    {
        parent::_prepareLayout();
        if (!$this->getTemplate()) {
            $this->setTemplate(static::IBAN_TEMPLATE);
        }
        return $this;
    }
}
1
  • I want to add in same edit form Mar 16, 2018 at 10:59

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.