以管理員編輯形式添加模板文件


1

我想在Grid \ Edit中添加phtml文件,其中包含圖像或其他html部分

遵循Form.php文件

<?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;
    }
}

它顯示了主容器的頂部。這是在表單容器之間添加phtml的更好方法

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

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;
    }
}