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
-
you just want to add images right??? you can do it by using by creating custom field typeJigs Parmar– Jigs Parmar2018年03月16日 10:31:20 +00:00Commented Mar 16, 2018 at 10:31
-
@jigsparmar i want to add html elementsHardik Visa– Hardik Visa2018年03月16日 11:00:13 +00:00Commented Mar 16, 2018 at 11:00
-
i add answer please check.it work for me in my custom extensionJigs Parmar– Jigs Parmar2018年03月16日 11:04:13 +00:00Commented Mar 16, 2018 at 11:04
2 Answers 2
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;
}
}
-
how add
phtmlfile in your code ?Hardik Visa– Hardik Visa2018年03月16日 11:06:46 +00:00Commented Mar 16, 2018 at 11:06 -
i add Block file you can write code thereJigs Parmar– Jigs Parmar2018年03月16日 11:08:11 +00:00Commented Mar 16, 2018 at 11:08
-
Vendor\Extension\Block\Adminhtml\Proindex\Edit\Renderer\ThumbnailJigs Parmar– Jigs Parmar2018年03月16日 11:08:20 +00:00Commented Mar 16, 2018 at 11:08
-
Didn't work for me!Wakar Ahamad– Wakar Ahamad2019年02月22日 06:51:51 +00:00Commented Feb 22, 2019 at 6:51
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;
}
}
-
I want to add in same edit formHardik Visa– Hardik Visa2018年03月16日 10:59:00 +00:00Commented Mar 16, 2018 at 10:59