My block file form.php and I want to call phtml file in this block to display table in this custom admin form, I have no created a layout for this phtml file, if is there any other way to display the table then also suggest me, Thanks
<?php
namespace Vendor\Module\Block\Adminhtml\Blog\Edit;
use Magento\Backend\Block\Template\Context;
use Magento\Framework\Registry;
use Magento\Framework\Data\FormFactory;
use Vendor\Module\Block\Adminhtml\Status;
class Form extends \Magento\Backend\Block\Widget\Form\Generic
{
protected $_systemStore;
protected $template;
public function __construct(
Context $context,
Registry $registry,
FormFactory $formFactory,
Status $options,
array $data = []
)
{
$this->options = $options;
parent::__construct($context, $registry, $formFactory, $data);
}
protected function _prepareForm()
{
$dateFormat = $this->_localeDate->getDateFormat(\IntlDateFormatter::SHORT);
$model = $this->_coreRegistry->registry('row_data');
$form = $this->_formFactory->create(
['data' => [
'id' => 'edit_form',
'enctype' => 'multipart/form-data',
'action' => $this->getData('action'),
'method' => 'post'
]
]
);
$file1Url = $model->getFile1();
$file2Url = $model->getFile2();
$file3Url = $model->getFile3();
$form->setHtmlIdPrefix('mtgrid_');
if ($model->getId()) {
$fieldset = $form->addFieldset(
'base_fieldset',
['legend' => __('Edit Row Data'), 'class' => 'fieldset-wide']
);
$fieldset->addField('id', 'hidden', ['name' => 'id']);
} else {
$fieldset = $form->addFieldset(
'base_fieldset',
['legend' => __('Add Row Data'), 'class' => 'fieldset-wide']
);
}
$fieldset->addField(
'prapproveno',
'text',
[
'name' => 'prapproveno',
'label' => __('Prior Approve No.'),
'id' => 'prapproveno',
'title' => __('Prior Approve No.'),
'class' => '',
]
);
$fieldset->addField(
'submitdate',
'date',
[
'name' => 'submitdate',
'label' => __('Submit Date'),
'date_format' => $dateFormat,
'time_format' => 'HH:mm:ss',
'class' => 'validate-date validate-date-range date-range-custom_theme-from',
'class' => 'required-entry',
]
);
$fieldset->addField(
'chnlmanager',
'text',
[
'name' => 'chnlmanager',
'label' => __('Channel Maneger'),
'id' => 'chnlmanager',
'title' => __('Channel Manager'),
'class' => 'required-entry',
]
);
$fieldset->addField(
'status',
'select',
[
'name' => 'status',
'label' => __('Status'),
'id' => 'status',
'title' => __('Status'),
'values' => [1=>"Enable",0=>"Disable"],
'class' => 'status',
'required' => true,
]
);
$fieldset->addField(
'company',
'select',
[
'name' => 'company',
'label' => __('Company'),
'id' => 'company',
'title' => __('Company'),
'values' => $this->options->getOptionArray(),
'class' => 'required-entry',
'required' => true,
]
);
$fieldset->addField(
'branch',
'text',
[
'name' => 'branch',
'label' => __('Branch'),
'id' => 'branch',
'title' => __('Branch'),
'class' => 'required-entry',
]
);
$fieldset->addField(
'avpno',
'text',
[
'name' => 'avpno',
'label' => __('AVP/Reseller No.'),
'id' => 'avpno',
'title' => __('AVP/Reseller No.'),
'class' => 'required-entry',
]
);
$fieldset->addField(
'budgetfrom',
'select',
[
'name' => 'budgetfrom',
'label' => __('Budget From'),
'id' => 'budgetfrom',
'title' => __('Budget From'),
'values' => $this->options->getOptionArray(),
'class' => 'required-entry',
'required' => true,
]
);
$fieldset->addField(
'marketingactivity',
'select',
[
'name' => 'marketingactivity',
'label' => __('Marketing Activity'),
'id' => 'budgetfrom',
'title' => __('Budget From'),
'values' => $this->options->getOptionArray(),
'class' => 'required-entry',
'required' => true,
]
);
$fieldset->addField(
'description',
'text',
[
'name' => 'description',
'label' => __('Description'),
'id' => 'description',
'title' => __('Description'),
'class' => 'required-entry',
]
);
$fieldset->addField(
'file1',
'file',
[
'name' => 'file1',
'label' => __('Attachments'),
'id' => 'file1',
'title' => __('Attachments'),
'class' => '',
'note' => 'PDF format only',
'value'=>$file1Url
]
);
$fieldset->addField(
'file2',
'file',
[
'name' => 'file2',
'label' => __(''),
'id' => 'file2',
'title' => __(''),
'class' => '',
'note' => 'PDF format only',
'value'=>$file2Url
]
);
$fieldset->addField(
'file3',
'file',
[
'name' => 'file3',
'label' => __(''),
'id' => 'file3',
'title' => __(''),
'class' => '',
'note' => 'PDF format only',
'value'=>$file3Url
]
);
$form->setValues($model->getData());
$form->setUseContainer(true);
$this->setForm($form);
return parent::_prepareForm();
}
}
-
so you want show phtml file into form?Dhiren Vasoya– Dhiren Vasoya2020年02月08日 05:55:46 +00:00Commented Feb 8, 2020 at 5:55
-
yes@DhirenVasoyaArjun Soni– Arjun Soni2020年02月08日 06:09:10 +00:00Commented Feb 8, 2020 at 6:09
1 Answer 1
I have updated your form.php file, Check the below code.
<?php
namespace Vendor\Module\Block\Adminhtml\Blog\Edit;
use Magento\Backend\Block\Template\Context;
use Magento\Framework\Registry;
use Magento\Framework\Data\FormFactory;
use Vendor\Module\Block\Adminhtml\Status;
class Form extends \Magento\Backend\Block\Widget\Form\Generic
{
/**
* @var \Magento\Backend\Block\Widget\Form\Renderer\Fieldset
*/
protected $_rendererFieldset;
protected $_systemStore;
protected $template;
public function __construct(
Context $context,
\Magento\Backend\Block\Widget\Form\Renderer\Fieldset $rendererFieldset,
Registry $registry,
FormFactory $formFactory,
Status $options,
array $data = []
)
{
$this->options = $options;
$this->_rendererFieldset = $rendererFieldset;
parent::__construct($context, $registry, $formFactory, $data);
}
protected function _prepareForm()
{
$dateFormat = $this->_localeDate->getDateFormat(\IntlDateFormatter::SHORT);
$model = $this->_coreRegistry->registry('row_data');
$form = $this->_formFactory->create(
['data' => [
'id' => 'edit_form',
'enctype' => 'multipart/form-data',
'action' => $this->getData('action'),
'method' => 'post'
]
]
);
$file1Url = $model->getFile1();
$file2Url = $model->getFile2();
$file3Url = $model->getFile3();
$form->setHtmlIdPrefix('mtgrid_');
if ($model->getId()) {
$fieldset = $form->addFieldset(
'base_fieldset',
['legend' => __('Edit Row Data'), 'class' => 'fieldset-wide']
);
$fieldset->addField('id', 'hidden', ['name' => 'id']);
} else {
$fieldset = $form->addFieldset(
'base_fieldset',
['legend' => __('Add Row Data'), 'class' => 'fieldset-wide']
);
}
$fieldset->addField(
'prapproveno',
'text',
[
'name' => 'prapproveno',
'label' => __('Prior Approve No.'),
'id' => 'prapproveno',
'title' => __('Prior Approve No.'),
'class' => '',
]
);
$fieldset->addField(
'submitdate',
'date',
[
'name' => 'submitdate',
'label' => __('Submit Date'),
'date_format' => $dateFormat,
'time_format' => 'HH:mm:ss',
'class' => 'validate-date validate-date-range date-range-custom_theme-from',
'class' => 'required-entry',
]
);
$fieldset->addField(
'chnlmanager',
'text',
[
'name' => 'chnlmanager',
'label' => __('Channel Maneger'),
'id' => 'chnlmanager',
'title' => __('Channel Manager'),
'class' => 'required-entry',
]
);
$fieldset->addField(
'status',
'select',
[
'name' => 'status',
'label' => __('Status'),
'id' => 'status',
'title' => __('Status'),
'values' => [1=>"Enable",0=>"Disable"],
'class' => 'status',
'required' => true,
]
);
$fieldset->addField(
'company',
'select',
[
'name' => 'company',
'label' => __('Company'),
'id' => 'company',
'title' => __('Company'),
'values' => $this->options->getOptionArray(),
'class' => 'required-entry',
'required' => true,
]
);
$fieldset->addField(
'branch',
'text',
[
'name' => 'branch',
'label' => __('Branch'),
'id' => 'branch',
'title' => __('Branch'),
'class' => 'required-entry',
]
);
$fieldset->addField(
'avpno',
'text',
[
'name' => 'avpno',
'label' => __('AVP/Reseller No.'),
'id' => 'avpno',
'title' => __('AVP/Reseller No.'),
'class' => 'required-entry',
]
);
$fieldset->addField(
'budgetfrom',
'select',
[
'name' => 'budgetfrom',
'label' => __('Budget From'),
'id' => 'budgetfrom',
'title' => __('Budget From'),
'values' => $this->options->getOptionArray(),
'class' => 'required-entry',
'required' => true,
]
);
$fieldset->addField(
'marketingactivity',
'select',
[
'name' => 'marketingactivity',
'label' => __('Marketing Activity'),
'id' => 'budgetfrom',
'title' => __('Budget From'),
'values' => $this->options->getOptionArray(),
'class' => 'required-entry',
'required' => true,
]
);
$fieldset->addField(
'description',
'text',
[
'name' => 'description',
'label' => __('Description'),
'id' => 'description',
'title' => __('Description'),
'class' => 'required-entry',
]
);
$fieldset->addField(
'file1',
'file',
[
'name' => 'file1',
'label' => __('Attachments'),
'id' => 'file1',
'title' => __('Attachments'),
'class' => '',
'note' => 'PDF format only',
'value'=>$file1Url
]
);
$fieldset->addField(
'file2',
'file',
[
'name' => 'file2',
'label' => __(''),
'id' => 'file2',
'title' => __(''),
'class' => '',
'note' => 'PDF format only',
'value'=>$file2Url
]
);
$fieldset->addField(
'file3',
'file',
[
'name' => 'file3',
'label' => __(''),
'id' => 'file3',
'title' => __(''),
'class' => '',
'note' => 'PDF format only',
'value'=>$file3Url
]
);
$fieldset->addField(
'html',
'text',
[
'name' => 'html',
'label' => __('Html Content'),
'title' => __('Html Content')
]
)->setRenderer($this->_rendererFieldset->setTemplate('Vendor_Module::testtable.phtml'));
$form->setValues($model->getData());
$form->setUseContainer(true);
$this->setForm($form);
return parent::_prepareForm();
}
}
I have added additional field at the end of the form, Like
$fieldset->addField(
'html',
'text',
[
'name' => 'html',
'label' => __('Html Content'),
'title' => __('Html Content')
]
)->setRenderer($this->_rendererFieldset->setTemplate('Vendor_Module::testtable.phtml'));
Also added
\Magento\Backend\Block\Widget\Form\Renderer\Fieldset
In your __construct function, I hope this will work for you.
-
Thank You So much! @KishorThummar it works successfullyArjun Soni– Arjun Soni2020年02月08日 06:25:10 +00:00Commented Feb 8, 2020 at 6:25
-
Your welcome, Please upvote the answer also, So it will help others.Kishor Thummar– Kishor Thummar2020年02月08日 06:30:46 +00:00Commented Feb 8, 2020 at 6:30
-
Can i add block file with templete file if so then please tell me how to do that thanksArjun Soni– Arjun Soni2020年02月10日 10:26:11 +00:00Commented Feb 10, 2020 at 10:26
-
Why you need to use block file with the template, You can add functions in form.php and use it in your template file.Kishor Thummar– Kishor Thummar2020年02月10日 10:28:11 +00:00Commented Feb 10, 2020 at 10:28
-
Or you can use any Block file function in your template using below code, Like <?php $blockObj= $block->getLayout()->createBlock('Company\Helloworld\Block\Main'); echo $blockObj->getMyCustomMethod(); ?>Kishor Thummar– Kishor Thummar2020年02月10日 10:29:31 +00:00Commented Feb 10, 2020 at 10:29