I've created a grid for my "profile" model. Add and delete works fine, but when I click on the "Edit" action, the resulting page doesn't load the profile's existing fields.
I understand that we pull from the model storage when editing, which is working, but how do we populate the edit form with existing fields? Here's my Edit class.
<?php
namespace mycompany\mymodule\Controller\Adminhtml\Profile;
class Edit extends \Magento\Backend\App\Action
{
/**
* @var \Magento\Framework\View\Result\PageFactory
*/
protected $_resultPageFactory;
/**
* @var \mycompany\mymodule\Model\Profile\ProfileFactory
*/
protected $_profileFactory;
/**
* @var \Magento\Framework\Registry
*/
protected $_coreRegistry;
/**
* @var \Magento\Backend\Model\Session
*/
protected $_backendSession;
public function __construct(
\Magento\Backend\App\Action\Context $context,
\Magento\Framework\View\Result\PageFactory $resultPageFactory,
\Magento\Framework\Registry $coreRegistry,
\mycompany\mymodule\Model\Profile\ProfileFactory $profileFactory
) {
parent::__construct($context);
$this->_resultPageFactory = $resultPageFactory;
$this->_coreRegistry = $coreRegistry;
$this->_backendSession = $this->_getSession();
$this->_profileFactory = $profileFactory;
}
/**
* @return \Magento\Framework\View\Result\Page
*/
public function execute()
{
$id = $this->getRequest()->getParam('id');
$profile = $this->_initProfile();
$resultPage = $this->_resultPageFactory->create();
$resultPage->getConfig()->getTitle()->set(__('Profiles'));
if ($id) {
$profile->load($id);
if (!$profile->getId()) {
$this->messageManager->addError(__('This profile no longer exists.'));
$resultRedirect = $this->_resultRedirectFactory->create();
$resultRedirect->setPath(
'myroute/profile/profiles',
[
'profile_id' => $profile->getId(),
'_current' => true
]
);
return $resultRedirect;
}
}
$title = $profile->getId() ? $profile->getName() : __('New Profile');
$resultPage->getConfig()->getTitle()->prepend($title);
$data = $this->_backendSession->getData('profile_form', true);
if (!empty($data)) {
$profile->setData($data);
}
return $resultPage;
}
/**
* Init Profile
*
* @return \mycompany\mymodule\Model\Profile
*/
protected function _initProfile()
{
$profileId = (int) $this->getRequest()->getParam('id');
$profile = $this->_profileFactory->create();
if ($profileId) {
$profile->load($profileId);
}
$this->_coreRegistry->register('mymodule_profile', $profile);
return $profile;
}
}