1

I have a phtml file having an HTML form, I want to use it as an admin form.

To manage admin form magento2 either uses "UI Component" or "_prepareForm()" block method.

I've created a module using module creator which uses "_prepareForm()" and in this method to use phtml I've called:-

$this->setTemplate(myphtml.phtml);

Which is working fine, but I'm not able to show fields value in the form during edit because I'm not able to get and set the values.

Here is the complete code of block from where I'm calling phtml file:-

<?php

namespace Namespace\Modulename\Block\Adminhtml\Modulename\Edit\Tab;

/**
 * Modulename edit form main tab
 */
class Main extends \Magento\Backend\Block\Widget\Form\Generic implements \Magento\Backend\Block\Widget\Tab\TabInterface
{
    /**
     * @var \Magento\Store\Model\System\Store
     */
    protected $_systemStore;

    /**
     * @var \Nav\Gametype\Model\Status
     */
    protected $_status;



    /**
     * @param \Magento\Backend\Block\Template\Context $context
     * @param \Magento\Framework\Registry $registry
     * @param \Magento\Framework\Data\FormFactory $formFactory
     * @param \Magento\Store\Model\System\Store $systemStore
     * @param array $data
     */
    public function __construct(
        \Magento\Backend\Block\Template\Context $context,
        \Magento\Framework\Registry $registry,
        \Magento\Framework\Data\FormFactory $formFactory,
        \Magento\Store\Model\System\Store $systemStore,
        \Nav\Gametype\Model\Status $status,
        array $data = []
    ) {
        $this->_systemStore = $systemStore;
        $this->_status = $status;
        parent::__construct($context, $registry, $formFactory, $data);
    }

    /**
     * Prepare form
     *
     * @return $this
     * @SuppressWarnings(PHPMD.ExcessiveMethodLength)
     */


    protected function _prepareForm()
    {
        /* @var $model \Nav\Gametype\Model\BlogPosts */
        $model = $this->_coreRegistry->registry('gametype');

        $isElementDisabled = false;

        /** @var \Magento\Framework\Data\Form $form */
        $form = $this->_formFactory->create();

       //  $form->setHtmlIdPrefix('page_');

        $fieldset = $form->addFieldset('base_fieldset', ['legend' => __('Game Information')]);

        if ($model->getId()) {
            $fieldset->addField('nav_game_type_id', 'hidden', ['name' => 'nav_game_type_id']);
        }

           /*   $fieldset->addField(
        'title',
        'text',
        [
            'name' => 'title',
            'label' => __('Title'),
            'title' => __('Title'),
            'required' => true,
            'disabled' => $isElementDisabled
        ]
    );*/

         $this->setTemplate('myphtml.phtml');

        if (!$model->getId()) {
            $model->setData('is_active', $isElementDisabled ? '0' : '1');
        }

        $form->setValues($model->getData());

        $this->setForm($form);

        return parent::_prepareForm();
    }

    /**
     * Prepare label for tab
     *
     * @return \Magento\Framework\Phrase
     */
    public function getTabLabel()
    {
        return __('Game Information');
    }


    /**
     * Prepare title for tab
     *
     * @return \Magento\Framework\Phrase
     */
    public function getTabTitle()
    {
        return __('Game Information');
    }

    /**
     * {@inheritdoc}
     */
    public function canShowTab()
    {
        return true;
    }

    /**
     * {@inheritdoc}
     */
    public function isHidden()
    {
        return false;
    }

    /**
     * Check permission for passed action
     *
     * @param string $resourceId
     * @return bool
     */
    protected function _isAllowedAction($resourceId)
    {
        return $this->_authorization->isAllowed($resourceId);
    }

    public function getTargetOptionArray(){
        return array(
                    '_self' => "Self",
                    '_blank' => "New Page",
                    );
    }
}

How can i get and set the form values?how can i link my phtml to block/model so that i can set the fields values?

Note1:- You can also suggest me some alternate solution to achieve the same.

Note2:- Also i tried to call this phtm from layout file but it doesn't call under "" tag.

Waiting for the reply...

Thanks!!!

0

Finally, I've resolved the issue...

earlier I was calling phtml file separately via the layout but this time I just added phtml file in the layout where Main.php was calling and it works for me.

Earlier:-

   <block class="Namespace\Modulename\Block\Adminhtml\Modulename\Edit\Tab\Main" name="block_name"/>

Now:-

   <block class="Namespace\Modulename\Block\Adminhtml\Modulename\Edit\Tab\Main" name="block_name" template="Namespace_Modulename::myphtml.phtml"/>

Thanks!!!

0

I guess you are doing something wrong. You are rendering admin form using the custom template by calling
$this->setTemplate('myphtml.phtml');

and you are passing form data to $form->setValues($model->getData());. Magento will auto populate the form fields only if it renderes through the _prepareForm() block method.

If you would like to populate the form fields values in your custom template. Add a new method to this block class which returns the current form data object/array and calls it in your template to set values in respective form fields.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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