1

I have created an admin grid, but it is not showing the edit form. My files :

1.Stw\Skipredirectmodule\Block\Adminhtml\Skipredirectmodule.php

<?php
namespace Stw\Skipredirectmodule\Block\Adminhtml;
class Skipredirectmodule extends \Magento\Backend\Block\Widget\Grid\Container
{
    protected function _construct()
    {           
        $this->_controller = 'adminhtml_skipredirectmodule';
        $this->_blockGroup = 'Stw\Skipredirectmodule';
        $this->_headerText = __('Custom Settings');
        $this->_addButtonLabel = __('Add Custom Page'); 
        parent::_construct();           
    }
}
  1. Stw\Skipredirectmodule\Block\Adminhtml\Skipredirectmodule\Grid.php
<?php
namespace Stw\Skipredirectmodule\Block\Adminhtml\Skipredirectmodule;
class Grid extends \Magento\Backend\Block\Widget\Grid\Extended
{
    protected $moduleManager;
    protected $_setsFactory;
    protected $_productFactory;
    protected $_type;
    protected $_status;
    protected $_collectionFactory;
    protected $_visibility;
    protected $_websiteFactory;
    public function __construct(
        \Magento\Backend\Block\Template\Context $context,
        \Magento\Backend\Helper\Data $backendHelper,
        \Magento\Store\Model\WebsiteFactory $websiteFactory,
        \Stw\Skipredirectmodule\Model\ResourceModel\Skipredirectmodule\Collection $collectionFactory,
        \Magento\Framework\Module\Manager $moduleManager,
        array $data = []
    ) {

        $this->_collectionFactory = $collectionFactory;
        $this->_websiteFactory = $websiteFactory;
        $this->moduleManager = $moduleManager;
        parent::__construct($context, $backendHelper, $data);
    }
    protected function _construct()
    {
        parent::_construct();

        $this->setId('skipredirectmoduleGrid');
        $this->setDefaultSort('skipredirectmodule_id');
        $this->setDefaultDir('ASC');
        $this->setSaveParametersInSession(true);
        $this->setUseAjax(true);           
    }
    protected function _prepareCollection()
    {
        try{                                
            $collection =$this->_collectionFactory->load();           
            $this->setCollection($collection);
            parent::_prepareCollection();             
            return $this;
        }
        catch(Exception $e)
        {
            echo $e->getMessage();die;
        }
    }
    protected function _prepareColumns()
    {
        $this->addColumn(
            'skipredirectmodule_id',
            [
                'header' => __('ID'),
                'type' => 'number',
                'index' => 'skipredirectmodule_id',
                'header_css_class' => 'col-id',
                'column_css_class' => 'col-id'
            ]
        );
        $this->addColumn(
            'skipredirectmodule_countryid',
            [
                'header' => __('Title'),
                'index' => 'skipredirectmodule_countryid',
                'class' => 'column2'
            ]
        );
        $this->addColumn(
            'status',
            [
                'header' => __('Status'),
                'index' => 'status',
                'class' => 'column3',
                'type'      => 'options',
                'options'   => array(
                      1 => 'Enabled',
                      2 => 'Disabled',
          ),
            ]
        );
            $this->addColumn(
            'action',
            [
                'header' => __('Action'),
                'index' => 'action',
                'class' => 'column1',
                'type'      => 'action',
                'getter'    => 'getId',
                'actions'   => array(
                    array(
                        'caption'   => __('Edit'),
                        'url'       => array('base'=> '*/*/edit'),
                        'field'     => 'id',
                    )
                ),
                'filter'    => false,
                'sortable'  => false,
                'index'     => 'stores',
                'is_system' => true,
            ]
        );
        return parent::_prepareColumns();
    }
    protected function _prepareMassaction()
    {
        $this->setMassactionIdField('skipredirectmodule_id');
        $this->getMassactionBlock()->setFormFieldName('skipredirectmodule');

        $this->getMassactionBlock()->addItem(
            'delete',
            array(
                'label' => __('Delete'),
                'url' => $this->getUrl('skipredirectmodule/*/massDelete'),
                'confirm' => __('Are you sure?')
            )
        );
        return $this;
    }
    public function getRowUrl($row)
    {
        return $this->getUrl(
            'skipredirectmodule/*/edit',
            ['id' => $row->getId()]
        );
    }
}

3.Stw\Skipredirectmodule\Block\Adminhtml\Skipredirectmodule\Edit.php

<?php
namespace Stw\Skipredirectmodule\Block\Adminhtml\Skipredirectmodule;
class Edit extends \Magento\Backend\Block\Widget\Form\Container
{
    protected function _construct()
    {
        $this->_objectId = 'id';
        $this->_blockGroup = 'Stw_Skipredirectmodule';
        $this->_controller = 'adminhtml_skipredirectmodule';
        parent::_construct();
        $this->buttonList->update('save', 'label', __('Save Setting'));
        $this->buttonList->update('delete', 'label', __('Delete Setting'));
        $this->buttonList->add(
            'saveandcontinue',
            array(
                'label' => __('Save and Continue Edit'),
                'class' => 'save',
                'data_attribute' => array(
                    'mage-init' => array('button' => array('event' => 'saveAndContinueEdit', 'target' => '#edit_form'))
                )
            ),
            -100
        );
        $this->_formScripts[] = "
            function toggleEditor() {
                if (tinyMCE.getInstanceById('block_content') == null) {
                    tinyMCE.execCommand('mceAddControl', false, 'hello_content');
                } else {
                    tinyMCE.execCommand('mceRemoveControl', false, 'hello_content');
                }
            }
        ";
    }
    public function getHeaderText()
    {
        if ($this->_coreRegistry->registry('skipredirectmodule_data')->getId()) {
            return __("Edit Item '%1'", $this->escapeHtml($this->_coreRegistry->registry('skipredirectmodule_data')->getTitle()));
        } else {
            return __('Admin Panel');
        }
    }
}

4.Stw\Skipredirectmodule\Block\Adminhtml\Skipredirectmodule\Edit\Tabs.php

<?php
namespace Stw\Skipredirectmodule\Block\Adminhtml\Skipredirectmodule\Edit;
class Tabs extends \Magento\Backend\Block\Widget\Tabs
{
    protected function _construct()
    {    
        parent::_construct();
        $this->setId('skipredirectmodule_skipredirectmodule_tabs');
        $this->setDestElementId('edit_form');
        $this->setTitle(__('Custom Module'));
    }
}

5.Stw\Skipredirectmodule\Block\Adminhtml\Skipredirectmodule\Edit\Form.php

<?php
namespace Stw\Skipredirectmodule\Block\Adminhtml\Skipredirectmodule\Edit;
class Form extends \Magento\Backend\Block\Widget\Form\Generic
{
    protected $_customerAccountService;
    protected function _prepareForm()
    {
        $form = $this->_formFactory->create(
            array(
                'data' => array(
                    'id' => 'edit_form',
                    'action' => $this->getUrl('*/*/save'),
                    'method' => 'post',
                    'enctype' => 'multipart/form-data'
                )
            )
        );
        $form->setUseContainer(true);
        $this->setForm($form);
        return parent::_prepareForm();
    }
}

6.Stw\Skipredirectmodule\Block\Adminhtml\Skipredirectmodule\Edit\Tab\Form.php

    <?php
namespace Stw\Skipredirectmodule\Block\Adminhtml\Skipredirectmodule\Edit\Tab;
class Form extends \Magento\Backend\Block\Widget\Form\Generic implements \Magento\Backend\Block\Widget\Tab\TabInterface
{
    protected $_systemStore;
    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 = array()
    ) {
        $this->_systemStore = $systemStore;
        parent::__construct($context, $registry, $formFactory, $data);
    }
    protected function _prepareForm()
    {
        $model = $this->_coreRegistry->registry('skipredirectmodule_skipredirectmodule');
        $isElementDisabled = false;
        $form = $this->_formFactory->create();
        $form->setHtmlIdPrefix('page_');
        $fieldset = $form->addFieldset('skipredirectmodule_form', array('legend' => __('Skip Redirect Settings')));
        if ($model->getId()) {
            $fieldset->addField('id', 'hidden', array('name' => 'id'));
        }

        $fieldset->addField(
            'column1',
            'text',
            array(
                'name' => 'column1',
                'label' => __('column 1'),
                'title' => __('column 1'),
            )
        );
        $fieldset->addField(
            'column2',
            'text',
            array(
                'name' => 'column2',
                'label' => __('column 2'),
                'title' => __('column 2'),
            )
        );
        $fieldset->addField(
            'column3',
            'text',
            array(
                'name' => 'column3',
                'label' => __('column3'),
                'title' => __('column3'),
            )
        );        
        if (!$model->getId()) {
            $model->setData('status', $isElementDisabled ? '2' : '1');
        }
        $form->setValues($model->getData());
        $this->setForm($form);
        return parent::_prepareForm();   
    }
    public function getTabLabel()
    {
        return __('General');
    }
    public function getTabTitle()
    {
        return __('General');
    }
    public function canShowTab()
    {
        return true;
    }
    public function isHidden()
    {
        return false;
    }
    protected function _isAllowedAction($resourceId)
    {
        return $this->_authorization->isAllowed($resourceId);
    }
}

7.Stw\Skipredirectmodule\view\adminhtml\layout\skipredirectmodule_skipredirectmodule_index.xml

<?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="styles"/>
    <update handle="skipredirectmodule_grid"/>
    <body>
        <referenceContainer name="content">
            <block class="Stw\Skipredirectmodule\Block\Adminhtml\Skipredirectmodule" name="admin.skipredirectmodule.grid"/>
        </referenceContainer>
    </body>
</page>

8.Stw\Skipredirectmodule\view\adminhtml\layout\skipredirectmodule_skipredirectmodule_edit.xml

<?xml version="1.0" encoding="UTF-8"?>
<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="editor"/>
    <body>
       <referenceContainer name="content">
            <block class="Stw\Skipredirectmodule\Block\Adminhtml\Skipredirectmodule\Edit" name="skipredirectmodule_skipredirectmodule_edit"/>
        </referenceContainer>
         <referenceContainer name="left">
            <block class="Stw\Skipredirectmodule\Block\Adminhtml\Skipredirectmodule\Edit\Tabs" name="skipredirectmodule_skipredirectmodule_edit_tabs">
                <block class="Stw\Skipredirectmodule\Block\Adminhtml\Skipredirectmodule\Edit\Tab\Form" name="skipredirectmodule_skipredirectmodule_edit_tab_form"/>
                 <action method="addTab">
                    <argument name="name" xsi:type="string">form_section</argument>
                    <argument name="block" xsi:type="string">stw_skipredirectmodule_edit_tab_form</argument>
                </action>
                <!--CedAddTab-->
            </block>
        </referenceContainer>
    </body>
</page>

page screenshot

Please help me to solve this issue. Any help will be appreciated. Thanks.

0

You are missing controller NewAction. After that, add in execute()

$this->_forward('edit');

Btw, you should use Magento UI component :)

0

I was facing the same issue and var\log\system.log told me that what was the actual issue. I did a spelling mistake in dependency injection of a class in my case.

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.