Please follow below steps
admin layout xml app\code\Sugarcode\Test\view\adminhtml\layout\test_lists_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">
<head>
<css src="jquery/fileUploader/css/jquery.fileupload-ui.css"/>
</head>
<update handle="editor"/>
<body>
<referenceContainer name="content">
<block class="Sugarcode\Test\Block\Adminhtml\Lists\Edit" name="list_post_edit"/>
</referenceContainer>
<referenceContainer name="left">
<block class="Sugarcode\Test\Block\Adminhtml\Lists\Edit\Tabs" name="list_post_edit_tabs">
<block class="Sugarcode\Test\Block\Adminhtml\Lists\Edit\Tab\Main" name="list_post_edit_tab_main"/>
<action method="addTab">
<argument name="name" xsi:type="string">main_section</argument>
<argument name="block" xsi:type="string">list_post_edit_tab_main</argument>
</action>
</block>
</referenceContainer>
</body>
</page>
controller where you need to check id is there or not to fill form and save the data in register
app\code\Sugarcode\Test\Controller\Adminhtml\Lists\Edit.php
<?php
namespace Sugarcode\Test\Controller\Adminhtml\Lists;
use Magento\Backend\App\Action;
class Edit extends \Magento\Backend\App\Action
{
/**
* Core registry
*
* @var \Magento\Framework\Registry
*/
protected $_coreRegistry = null;
/**
* @var \Magento\Framework\View\Result\PageFactory
*/
protected $resultPageFactory;
/**
* @param Action\Context $context
* @param \Magento\Framework\View\Result\PageFactory $resultPageFactory
* @param \Magento\Framework\Registry $registry
*/
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 true;
}
/**
* 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('Sugarcode_Test::test')
->addBreadcrumb(__('Lists'), __('Lists'))
->addBreadcrumb(__('Manage Info'), __('Manage Info'));
return $resultPage;
}
/**
* Edit Blog Post
*
* @return \Magento\Backend\Model\View\Result\Page|\Magento\Backend\Model\View\Result\Redirect
* @SuppressWarnings(PHPMD.NPathComplexity)
*/
public function execute()
{
// 1. Get ID and create model
$id = $this->getRequest()->getParam('id');
$model = $this->_objectManager->create('Sugarcode\Test\Model\Test');
// 2. Initial checking
if ($id) {
$model->load($id);
if (!$model->getId()) {
$this->messageManager->addError(__('This info no longer exists.'));
/** \Magento\Backend\Model\View\Result\Redirect $resultRedirect */
$resultRedirect = $this->resultRedirectFactory->create();
return $resultRedirect->setPath('*/*/');
}
}
// 3. Set entered data if was error when we do save
$data = $this->_objectManager->get('Magento\Backend\Model\Session')->getFormData(true);
if (!empty($data)) {
$model->setData($data);
}
// 4. Register model to use later in blocks
$this->_coreRegistry->register('lists', $model);
// 5. Build edit form
/** @var \Magento\Backend\Model\View\Result\Page $resultPage */
$resultPage = $this->_initAction();
$resultPage->addBreadcrumb(
$id ? __('Edit Info') : __('New Info'),
$id ? __('Edit info') : __('New Info')
);
$resultPage->getConfig()->getTitle()->prepend(__('Info'));
$resultPage->getConfig()->getTitle()
->prepend($model->getId() ? $model->getTitle() : __('New Info'));
return $resultPage;
}
}
lets move on to block
app\code\Sugarcode\Test\Block\Adminhtml\Lists\Edit.php
<?php
namespace Sugarcode\Test\Block\Adminhtml\Lists;
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 = 'id';
$this->_blockGroup = 'Sugarcode_Test';
$this->_controller = 'adminhtml_lists';
parent::_construct();
$this->buttonList->update('save', 'label', __('Save Info'));
$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 List'));
}
/**
* Retrieve text for header element depending on loaded post
*
* @return \Magento\Framework\Phrase
*/
public function getHeaderText()
{
if ($this->_coreRegistry->registry('lists')->getId()) {
return __("Edit List '%1'", $this->escapeHtml($this->_coreRegistry->registry('lists')->getTitle()));
} else {
return __('New Info');
}
}
/**
* 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('test/*/save', ['_current' => true, 'back' => 'edit', 'active_tab' => '{{tab_id}}']);
}
/**
* Prepare layout
*
* @return \Magento\Framework\View\Element\AbstractBlock
*/
protected function _prepareLayout()
{
$this->_formScripts[] = "
function toggleEditor() {
if (tinyMCE.getInstanceById('page_content') == null) {
tinyMCE.execCommand('mceAddControl', false, 'content');
} else {
tinyMCE.execCommand('mceRemoveControl', false, 'content');
}
};
";
return parent::_prepareLayout();
}
}
as like in magneto1.x here alo we have mode which default folder name is edit so create folder called edit then create below files
app\code\Sugarcode\Test\Block\Adminhtml\Lists\Edit\Form.php
<?php
namespace Sugarcode\Test\Block\Adminhtml\Lists\Edit;
/**
* Adminhtml blog post edit form block
*
* @author Magento Core Team <[email protected]>
*/
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' => ['id' => 'edit_form', 'action' => $this->getData('action'), 'method' => 'post']]
);
$form->setUseContainer(true);
$this->setForm($form);
return parent::_prepareForm();
}
}
app\code\Sugarcode\Test\Block\Adminhtml\Lists\Edit\tabs.php
<?php
namespace Sugarcode\Test\Block\Adminhtml\Lists\Edit;
/**
* Admin page left menu
*/
class Tabs extends \Magento\Backend\Block\Widget\Tabs
{
/**
* @return void
*/
protected function _construct()
{
parent::_construct();
$this->setId('post_tabs');
$this->setDestElementId('edit_form');
$this->setTitle(__('Information'));
}
}
now create folder called form and file main.php as per layout xml this will load in form container
if you need tabs then you need to use interface
class Main extends \Magento\Backend\Block\Widget\Form\Generic implements \Magento\Backend\Block\Widget\Tab\TabInterface
then interface function needs to be implemented here in below code below Prepare label for tab are for tabs
app\code\Sugarcode\Test\Block\Adminhtml\Lists\Edit\Tab\Main.php
<?php
namespace Sugarcode\Test\Block\Adminhtml\Lists\Edit\Tab;
/**
* Blog post 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 \Magento\Cms\Model\Wysiwyg\Config
*/
protected $_wysiwygConfig;
/**
* @var \Sugarcode\Test\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,
\Magento\Cms\Model\Wysiwyg\Config $wysiwygConfig,
\Sugarcode\Test\Model\Status $status,
array $data = []
) {
$this->_systemStore = $systemStore;
$this->_wysiwygConfig = $wysiwygConfig;
$this->_status = $status;
parent::__construct($context, $registry, $formFactory, $data);
}
/**
* Prepare form
*
* @return $this
* @SuppressWarnings(PHPMD.ExcessiveMethodLength)
*/
protected function _prepareForm()
{
/* @var $model \SR\Weblog\Model\BlogPosts */
$model = $this->_coreRegistry->registry('lists');
$isElementDisabled = false;
/** @var \Magento\Framework\Data\Form $form */
$form = $this->_formFactory->create();
$form->setHtmlIdPrefix('page_');
$fieldset = $form->addFieldset('base_fieldset', ['legend' => __('Information')]);
if ($model->getId()) {
$fieldset->addField('id', 'hidden', ['name' => 'id']);
}
$fieldset->addField(
'title',
'text',
[
'name' => 'title',
'label' => __('Title'),
'title' => __('Title'),
'required' => true,
'disabled' => $isElementDisabled,
'value' =>'abc'
]
);
if (!$model->getId()) {
$model->setData('status', $isElementDisabled ? '0' : '1');
}
$fieldset->addField(
'status',
'select',
[
'label' => __('Status'),
'title' => __('Status'),
'name' => 'status',
'required' => true,
'options' => $this->_status->getOptionArray(),
'disabled' => $isElementDisabled
]
);
$fieldset->addField(
'short_summary',
'editor',
[
'name' => 'short_summary',
'label' => __('Summary'),
'title' => __('Summary'),
'style' => 'height:36em',
'required' => true,
'config' => $this->_wysiwygConfig->getConfig()
]
);
$dateFormat = $this->_localeDate->getDateFormat(
\IntlDateFormatter::SHORT
);
/* $fieldset->addField(
'created_at',
'date',
[
'name' => 'created_at',
'label' => __('Created Date'),
'date_format' => $dateFormat,
'disabled' => $isElementDisabled,
'class' => 'validate-date validate-date-range date-range-custom_theme-from'
]
); */
$form->setValues($model->getData());
$this->setForm($form);
return parent::_prepareForm();
}
/**
* Prepare label for tab
*
* @return \Magento\Framework\Phrase
*/
public function getTabLabel()
{
return __('Information');
}
/**
* Prepare title for tab
*
* @return \Magento\Framework\Phrase
*/
public function getTabTitle()
{
return __('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);
}
}
if you dont wants tabs then follow below code
app\code\Sugarcode\Test\view\adminhtml\layout\test_lists_details.xml
<?xml version="1.0"?>
<!--
/**
* Copyright © 2015 Magento. All rights reserved.
* See COPYING.txt for license details.
*/
-->
<page xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="../../../../../../../lib/internal/Magento/Framework/View/Layout/etc/page_configuration.xsd">
<head>
<css src="jquery/fileUploader/css/jquery.fileupload-ui.css"/>
</head>
<update handle="editor"/>
<body>
<referenceContainer name="content">
<block class="Sugarcode\Test\Block\Adminhtml\Lists\Details" name="details_block_edit"/>
</referenceContainer>
</body>
</page>
app\code\Sugarcode\Test\Controller\Adminhtml\Lists\Details.php
<?php
namespace Sugarcode\Test\Controller\Adminhtml\Lists;
use Magento\Backend\App\Action;
class Details extends \Magento\Backend\App\Action
{
/**
* Core registry
*
* @var \Magento\Framework\Registry
*/
protected $_coreRegistry = null;
/**
* @var \Magento\Framework\View\Result\PageFactory
*/
protected $resultPageFactory;
/**
* @param Action\Context $context
* @param \Magento\Framework\View\Result\PageFactory $resultPageFactory
* @param \Magento\Framework\Registry $registry
*/
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 true;
}
/**
* 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('Sugarcode_Test::test')
->addBreadcrumb(__('Lists'), __('Lists'))
->addBreadcrumb(__('Manage Info'), __('Manage Info'));
return $resultPage;
}
/**
* Edit Blog Post
*
* @return \Magento\Backend\Model\View\Result\Page|\Magento\Backend\Model\View\Result\Redirect
* @SuppressWarnings(PHPMD.NPathComplexity)
*/
public function execute()
{
// 1. Get ID and create model
$id = $this->getRequest()->getParam('id');
$model = $this->_objectManager->create('Sugarcode\Test\Model\Test');
// 2. Initial checking
if ($id) {
$model->load($id);
if (!$model->getId()) {
$this->messageManager->addError(__('This info no longer exists.'));
/** \Magento\Backend\Model\View\Result\Redirect $resultRedirect */
$resultRedirect = $this->resultRedirectFactory->create();
return $resultRedirect->setPath('*/*/');
}
}
// 3. Set entered data if was error when we do save
$data = $this->_objectManager->get('Magento\Backend\Model\Session')->getFormData(true);
if (!empty($data)) {
$model->setData($data);
}
// 4. Register model to use later in blocks
$this->_coreRegistry->register('lists', $model);
// 5. Build edit form
/** @var \Magento\Backend\Model\View\Result\Page $resultPage */
$resultPage = $this->_initAction();
$resultPage->addBreadcrumb(
$id ? __('Edit Info') : __('New Info'),
$id ? __('Edit info') : __('New Info')
);
$resultPage->getConfig()->getTitle()->prepend(__('Info'));
$resultPage->getConfig()->getTitle()
->prepend($model->getId() ? $model->getTitle() : __('New Info'));
return $resultPage;
}
}
now in blocks
app\code\Sugarcode\Test\Block\Adminhtml\Lists\Details.php
<?php
/**
* Copyright © 2015 Magento. All rights reserved.
* See COPYING.txt for license details.
*/
namespace Sugarcode\Test\Block\Adminhtml\Lists;
/**
* CMS block edit form container
*/
class Details 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);
}
/**
* @return void
*/
protected function _construct()
{
$this->_objectId = 'id';
$this->_blockGroup = 'Sugarcode_Test';
$this->_controller = 'adminhtml_lists';
$this->_mode = 'details';
parent::_construct();
$this->buttonList->update('save', 'label', __('Save Details'));
$this->buttonList->update('delete', 'label', __('Delete Details'));
$this->buttonList->add(
'saveandcontinue',
[
'label' => __('Save and Continue Edit'),
'class' => 'save',
'data_attribute' => [
'mage-init' => ['button' => ['event' => 'saveAndContinueEdit', 'target' => '#edit_form']],
]
],
-100
);
$this->_formScripts[] = "
function toggleEditor() {
if (tinyMCE.getInstanceById('block_content') == null) {
tinyMCE.execCommand('mceAddControl', false, 'block_content');
} else {
tinyMCE.execCommand('mceRemoveControl', false, 'block_content');
}
}
";
}
/**
* Get edit form container header text
*
* @return \Magento\Framework\Phrase
*/
public function getHeaderText()
{
if ($this->_coreRegistry->registry('lists')->getId()) {
return __("Edit Details '%1'", $this->escapeHtml($this->_coreRegistry->registry('lists')->getTitle()));
} else {
return __('New Details');
}
}
}
in above file imp code is
$this->_mode = 'details';
and finally
app\code\Sugarcode\Test\Block\Adminhtml\Lists\Details\Form.php
<?php
/**
* Copyright © 2015 Magento. All rights reserved.
* See COPYING.txt for license details.
*/
namespace Sugarcode\Test\Block\Adminhtml\Lists\Details;
/**
* Adminhtml cms block edit form
*/
class Form extends \Magento\Backend\Block\Widget\Form\Generic
{
/**
* @var \Magento\Cms\Model\Wysiwyg\Config
*/
protected $_wysiwygConfig;
/**
* @var \Sugarcode\Test\Model\Status
*/
protected $_status;
/**
* @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\Cms\Model\Wysiwyg\Config $wysiwygConfig,
\Sugarcode\Test\Model\Status $status,
array $data = []
) {
$this->_wysiwygConfig = $wysiwygConfig;
$this->_status = $status;
parent::__construct($context, $registry, $formFactory, $data);
}
/**
* Init form
*
* @return void
*/
protected function _construct()
{
parent::_construct();
$this->setId('block_form');
$this->setTitle(__('Block Information'));
}
/**
* Prepare form
*
* @return $this
*/
protected function _prepareForm()
{
$model = $this->_coreRegistry->registry('lists');
/** @var \Magento\Framework\Data\Form $form */
$form = $this->_formFactory->create(
['data' => ['id' => 'edit_form', 'action' => $this->getData('action'), 'method' => 'post']]
);
$form->setHtmlIdPrefix('block_');
$fieldset = $form->addFieldset(
'base_fieldset',
['legend' => __('General Information'), 'class' => 'fieldset-wide']
);
if ($model->getId()) {
$fieldset->addField('id', 'hidden', ['name' => 'id']);
}
$fieldset->addField(
'title',
'text',
['name' => 'title', 'label' => __('Block Title'), 'title' => __('Block Title'), 'required' => true]
);
$fieldset->addField(
'status',
'select',
[
'label' => __('Status'),
'title' => __('Status'),
'name' => 'status',
'required' => true,
'options' => $this->_status->getOptionArray(),
]
);
if (!$model->getId()) {
$model->setData('status', '1');
}
$fieldset->addField(
'short_summary',
'editor',
[
'name' => 'short_summary',
'label' => __('Summary'),
'title' => __('Summary'),
'style' => 'height:36em',
'required' => true,
'config' => $this->_wysiwygConfig->getConfig()
]
);
$form->setValues($model->getData());
$form->setUseContainer(true);
$this->setForm($form);
return parent::_prepareForm();
}
}
above are the two ways of edit for ,
edit is for edit a content with tab and
details is for edit a content without tabs