3

I am creating a form for admin but my form content display on tab section instead of content area, I have tried several possibility but never succeed, so someone can guide me at some point to resolve this issue ?

Here is my layout file

NAME_MODULENAME_edit.xml

<?xml version="1.0"?>
<page xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" layout="admin-2columns-left" xsi:noNamespaceSchemaLocation="../../../../../../../lib/internal/Magento/Framework/View/Layout/etc/page_configuration.xsd">
    <update handle="styles"/>
    <update handle="editor"/>
    <body>
        <referenceContainer name="content">
            <block class="NAME\MODULENAME\Block\Adminhtml\OrderField\Edit" name="modulename_edit"/>
        </referenceContainer>
        <referenceContainer name="left">
            <block class="NAME\MODULENAME\Block\Adminhtml\OrderField\Edit\Tabs" name="modulename_edit_tabs">
                <block class="NAME\MODULENAME\Block\Adminhtml\OrderField\Edit\Tab\Main" name="modulename_edit_tab_main" />
                <action method="addTab">
                    <argument name="name" xsi:type="string">main_section</argument>
                    <argument name="block" xsi:type="string">modulename_edit_tab_main</argument>
                </action>
            </block>
        </referenceContainer>
    </body>
</page>

My block edit file

<?php

namespace NAME\MODULENAME\Block\Adminhtml\OrderField;

class Edit extends \Magento\Backend\Block\Widget\Form\Container {

    /**
     * Core registry
     *
     * @var \Magento\Framework\Registry
     */
    protected $_coreRegistry = null;

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

    /**
     * Initialize blog post edit block
     *
     * @return void
     */
    protected function _construct() {
        #$this->_objectId = 'field_id';
        $this->_controller = 'adminhtml_orderField';
        $this->_blockGroup = 'NAME_OrderField';

        #$this->_mode = 'edit';

        parent::_construct();

        $this->buttonList->update('save', 'label', __('Save Field'));
        $this->buttonList->add(
                'saveandcontinue', [
            'label' => __('Save and Continue Edit'),
            'class' => 'save',
            'data_attribute' => [
                'mage-init' => [
                    'button' => ['event' => 'saveAndContinueEdit', 'target' => '#edit_form'],
                ],
            ]
                ], -100
        );

        $this->buttonList->update('delete', 'label', __('Delete Field'));
    }

    /**
     * Retrieve text for header element depending on loaded post
     *
     * @return \Magento\Framework\Phrase
     */
    public function getHeaderText() {
        if ($this->_coreRegistry->registry('order_field')->getFieldId()) {
            return __("Edit Field '%1'", $this->escapeHtml($this->_coreRegistry->registry('order_field')->getAttributeLabel()));
        } else {
            return __('New Field');
        }
    }

    /**
     * Getter of url for "Save and Continue" button
     * tab_id will be replaced by desired by JS later
     *
     * @return string
     */
    protected function _getSaveAndContinueUrl() {
        return $this->getUrl('orderfield/*/save', ['_current' => true, 'back' => 'edit', 'active_tab' => '{{tab_id}}']);
    }

}

And my form file

<?php

namespace NAME\MODULENAME\Block\Adminhtml\OrderField\Edit;

class Form extends \Magento\Backend\Block\Widget\Form\Generic {

    /**
     * Prepare form
     *
     * @return $this
     */
    protected function _prepareForm() {
        /** @var \Magento\Framework\Data\Form $form */
        $form = $this->_formFactory->create(
                ['data' => ['field_id' => 'edit_form', 'action' => $this->getData('action'), 'method' => 'post']]
        );
        $form->setUseContainer(true);
        $this->setForm($form);
        return parent::_prepareForm();
    }

}

currently my output like this

enter image description here

  • Did you end up solving this? – micwallace Oct 3 at 10:46
0

In your layout edit block
Change your layout=""admin-2columns-left" to layout="admin-1column"

<?xml version="1.0"?>
<page xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" layout="admin-1column" xsi:noNamespaceSchemaLocation="urn:magento:framework:View/Layout/etc/page_configuration.xsd">
<update handle="styles"/>
<update handle="editor"/>
<body>
    <referenceContainer name="content">
        .. Block content tab
    </referenceContainer>
    <referenceContainer name="left">
       .. Block tabs
    </referenceContainer>
</body>
</page>
0
class Tabs extends WidgetTabs
{
/**
 * @return void
 */
protected function _construct()
{
    parent::_construct();
    $this->setId('ultranoir_menumanager_item_form');
    $this->setDestElementId('edit_form');
    $this->setTitle(__('Item Information'));
}
}

The value of $this->setDestElementId has to be 'edit_form' as the field_id your defined in namespace NAME\MODULENAME\Block\Adminhtml\OrderField\Edit;

$form = $this->_formFactory->create(
            ['data' => ['field_id' => 'edit_form', 'action' => $this->getData('action'), 'method' => 'post']]
    );
0

The parameter you need to use when initializing the form needs to be "id" rather than "field_id":

/** @var \Magento\Framework\Data\Form $form */
$form = $this->_formFactory->create(['data' => ['id' => 'edit_form', 'action' => $this->getData('action'), 'method' => 'post']]);
New contributor
micwallace is a new contributor to this site. Take care in asking for clarification, commenting, and answering. Check out our Code of Conduct.

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.