0

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;
    }
}
1

Turns out I needed a data provider for my profile model class. Here's my getData() function.

/**
     * Get data
     *
     * @return array
     */
    public function getData()
    {
        if (isset($this->loadedData)) {
            return $this->loadedData;
        }
        $items = $this->collection->getItems();
        /** @var Rule $rule */
        foreach ($items as $rule) {
            $rule->load($rule->getId());
            $this->loadedData[$rule->getId()] = $rule->getData();
        }
        if (empty($this->loadedData)) {
            $this->loadedData = [];
        }

        $data = $this->_dataPersistor->get('profile');
        if (!empty($data)) {
            $rule = $this->collection->getNewEmptyItem();
            $rule->setData($data);
            $this->loadedData[$rule->getId()] = $rule->getData();
            $this->_dataPersistor->clear('profile');
        }
    return $this->loadedData;
    }

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.