0

I'm having some difficulty setting up an edit form within my custom module, when i inspect i see the header "New Manufacturer", but the div which would typically be rendered with the class "page-content" is missing.enter image description here

I've specified the block as below:

<?xml version="1.0"?>
<page xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:View/Layout/etc/page_configuration.xsd">
    <update handle="editor"/>
    <body>
        <referenceContainer name="content">
            <block class="Amrita\Manufacturer\Block\Adminhtml\Grid\Edit" name="manufacturer_edit"/>
        </referenceContainer>
    </body>
</page>

The block looks like this:

<?php
namespace Amrita\Manufacturer\Block\Adminhtml\Grid;

class Edit extends \Magento\Backend\Block\Widget\Form\Container
{
    protected $_coreRegistry = null;

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

    protected function _construct()
    {
        $this->_objectId = 'manufacturer_id';
        $this->_blockGroup = 'Amrita_Manufacturer';
        $this->_controller = 'adminhtml_grid';


        parent::_construct();

        if ($this->_isAllowedAction('Amrita_Manufacturer::save_manufacturer')) {
            $this->buttonList->update('save', 'label', __('Save Manufacturer'));
            $this->buttonList->add(
                'saveandcontinue',
                [
                    'label' => __('Save and Continue Edit'),
                    'class' => 'save',
                    'data_attribute' => [
                        'mage-init' => [
                            'button' => ['event' => 'saveAndContinueEdit', 'target' => '#edit_form'],
                        ],
                    ]
                ],
                -100
            );
        } else {
            $this->buttonList->remove('save');
        }

        if ($this->_isAllowedAction('Amrita_Manufacturer::delete_manufacturer')) {
            $this->buttonList->update('delete', 'label', __('Delete Manufacturer'));
        } else {
            $this->buttonList->remove('delete');
        }
    }

    public function getModel()
    {
        return $this->_coreRegistry->registry('manufacturer_grid');
    }

    public function getHeaderText()
    {
        if ($this->getModel()->getId()) {
            return __("Edit Manufacturer '%1'", $this->escapeHtml($this->getModel()->getTitle()));
        } else {
            return __('New Manufacturer');
        }
    }

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

    /**
     * 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('manufacturer/*/save', ['_current' => true, 'back' => 'edit', 'active_tab' => '']);
    }
}

The Form.php is as follows:

<?php

namespace Amrita\Manufacturer\Block\Adminhtml\Grid\Edit;

use \Magento\Backend\Block\Widget\Form\Generic;

class Form extends Generic
{

    /**
     * @var \Magento\Store\Model\System\Store
     */
    protected $_systemStore;
    /**
     * @param \Magento\Backend\Block\Template\Context $context
     * @param \Magento\Framework\Registry $registry
     * @param \Magento\Framework\Data\FormFactory $formFactory
     * @param \Magento\Cms\Model\Wysiwyg\Config $wysiwygConfig
     * @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,
        array $data = []
    ) {
        $this->_systemStore = $systemStore;
        parent::__construct($context, $registry, $formFactory, $data);
    }

    /**
     * Init form
     *
     * @return void
     */
    protected function _construct()
    {
        parent::_construct();
        $this->setId('manufacturer_form');
        $this->setTitle(__('Manufacturer Information'));
    }

    public function getModel()
    {
        return $this->_coreRegistry->registry('manufacturer_grid');
    }

    /**
     * Prepare form
     *
     * @return $this
     */
    protected function _prepareForm()
    {
        $model = $this->getModel();

        /** @var \Magento\Framework\Data\Form $form */
        $form = $this->_formFactory->create(
            ['data' => ['id' => 'edit_form', 'action' => $this->getData('action'), 'method' => 'post']]
        );

        $form->setHtmlIdPrefix('manufacturer_');

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

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

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

        $fieldset->addField(
            'description',
            'editor',
            [
                'name' => 'description',
                'label' => __('Description'),
                'title' => __('Description'),
                'style' => 'height:36em',
                'required' => true
            ]
        );

        $fieldset->addField(
            'is_enabled',
            'select',
            [
                'label' => __('Status'),
                'title' => __('Status'),
                'name' => 'is_enabled',
                'required' => true,
                'options' => ['1' => __('Enabled'), '0' => __('Disabled')]
            ]
        );
        if (!$model->getId()) {
            $model->setData('is_enabled', '1');
        }

        $fieldset->addField(
            'is_restricted',
            'select',
            [
                'label' => __('Product Permissions'),
                'title' => __('Product Permissions'),
                'name' => 'is_restricted',
                'required' => true,
                'options' => ['1' => __('Restricted'), '0' => __('Unrestricted')]
            ]
        );
        if (!$model->getId()) {
            $model->setData('is_restricted', '1');
        }

        $form->setValues($model->getData());
        $form->setUseContainer(true);
        $this->setForm($form);

        return parent::_prepareForm();
    }
}

The new action is as follows:

<?php
namespace Amrita\Manufacturer\Controller\Adminhtml\Grid;

class NewAction extends \Magento\Backend\App\Action
{

    protected $resultForwardFactory;

    public function __construct(
        \Magento\Backend\App\Action\Context $context,
        \Magento\Backend\Model\View\Result\ForwardFactory $resultForwardFactory
    ) {
        $this->resultForwardFactory = $resultForwardFactory;
        parent::__construct($context);
    }

    public function execute()
    {
        /** @var \Magento\Backend\Model\View\Result\Forward $resultForward */
        $resultForward = $this->resultForwardFactory->create();
        return $resultForward->forward('edit');
    }

    protected function _isAllowed()
    {
        return $this->_authorization->isAllowed('Amrita_Manufacturer::save_manufacturer');
    }
}

The edit action is as follows:

<?php
namespace Amrita\Manufacturer\Controller\Adminhtml\Grid;

use Magento\Backend\App\Action;

class Edit extends \Magento\Backend\App\Action
{
    protected $_coreRegistry = null;

    protected $resultPageFactory;

    public function __construct(
        Action\Context $context,
        \Magento\Framework\View\Result\PageFactory $resultPageFactory,
        \Magento\Framework\Registry $registry
    ) {
        $this->resultPageFactory = $resultPageFactory;
        $this->_coreRegistry = $registry;
        parent::__construct($context);
    }

    /**
     * {@inheritdoc}
     */
    protected function _isAllowed()
    {
        return $this->_authorization->isAllowed('Amrita_Manufacturer::save_manufacturer');
    }

    /**
     * Init actions
     *
     * @return \Magento\Backend\Model\View\Result\Page
     */
    protected function _initAction()
    {
        // load layout, set active menu and breadcrumbs
        /** @var \Magento\Backend\Model\View\Result\Page $resultPage */
        $resultPage = $this->resultPageFactory->create();
        $resultPage->setActiveMenu('Amrita_Manufacturer::amrita_manufacturergrid')
        ->addBreadcrumb(__('Manufacturers'), __('Manufacturers'))
        ->addBreadcrumb(__('Manage Manufacturers'), __('Manage Manufacturers'));
        return $resultPage;
    }

    /**
     * Edit Blog post
     *
     * @return \Magento\Backend\Model\View\Result\Page|\Magento\Backend\Model\View\Result\Redirect
     * @SuppressWarnings(PHPMD.NPathComplexity)
     */
    public function execute()
    {
        $id = $this->getRequest()->getParam('manufacturer_id');
        $model = $this->_objectManager->create('Amrita\Manufacturer\Model\Manufacturer');

        if ($id) {
            $model->load($id);
            if (!$model->getId()) {
                $this->messageManager->addError(__('This manufacturer no longer exists.'));
                /** \Magento\Backend\Model\View\Result\Redirect $resultRedirect */
                $resultRedirect = $this->resultRedirectFactory->create();

                return $resultRedirect->setPath('*/*/');
            }
        }

        $data = $this->_objectManager->get('Magento\Backend\Model\Session')->getFormData(true);
        if (!empty($data)) {
            $model->setData($data);
        }

        $this->_coreRegistry->register('manufacturer_grid', $model);

        /** @var \Magento\Backend\Model\View\Result\Page $resultPage */
        $resultPage = $this->_initAction();
        $resultPage->addBreadcrumb(
            $id ? __('Edit Manufacturer') : __('New Manufacturer'),
            $id ? __('Edit Manufacturer') : __('New Manufacturer')
        );
        $resultPage->getConfig()->getTitle()->prepend(__('Manufacturers'));
        $resultPage->getConfig()->getTitle()
            ->prepend($model->getId() ? $model->getTitle() : __('New Manufacturer'));

        return $resultPage;
    }
}

I have a few questions:

  1. the method getManufacturerId() in the form.php, where/how is this defined.
  2. what should I add to the core registry? Is it the Cache_tag as defined in the model Manufacturer.php? $this->_coreRegistry->registry('manufacturer_grid');
2

You are missing layout defined in your xml file. set layout="admin-2columns-left" in your page tag.

<?xml version="1.0"?>
<page xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
      xsi:noNamespaceSchemaLocation="urn:magento:framework:View/Layout/etc/page_configuration.xsd" layout="admin-2columns-left">
    <update handle="editor"/>
    <body>
        <referenceContainer name="content">
            <block class="Amrita\Manufacturer\Block\Adminhtml\Grid\Edit" name="manufacturer_edit"/>
        </referenceContainer>
    </body>
</page>
1
0

Because of $this->_coreRegistry->register(), invalid core registry your form is not loading. Your core registry should be your model class, like $this->_coreRegistry->register(modulename_modelclass);

0

Please make sure that your layout configuration file has the same namespace name with route front name. For example: your route front name is manufacturer, and your layout configuration file name is manufacturer_grid_edit.

1
  • waht is configuration file? Nov 14 '19 at 10:36

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.