1

I have created a custom module using \Magento\Backend\Block\Widget\Form\Container form with Magento2.3 and I am facing issue while edit form is not showing in Magento Cloud, But local environment is working fine.

I am testing, the Block action is loaded, But the Edit/Form.php not loading.

enter image description here

Below is my code.

The Controller [import.php]

 <?php
namespace Test\api\Controller\Adminhtml\AdList;

use Magento\Framework\Controller\ResultFactory;

class import extends \Magento\Backend\App\Action
{
  /**
   * @var \Magento\Framework\Registry
   */
  private $coreRegistry;
  /**
   * @var \Test\api\Model\AdsTableFactory
   */
  private $adsTableFactory;

  /**
   * @param \Magento\Backend\App\Action\Context $context
   * @param \Magento\Framework\Registry $coreRegistry,
   * @param \Test\api\Model\AdsTableFactory $adsTableFactory
   */
  public function __construct(
      \Magento\Backend\App\Action\Context $context,
      \Magento\Framework\Registry $coreRegistry,
      \Test\api\Model\AdsTableFactory $adsTableFactory
  ) {
      parent::__construct($context);
      $this->coreRegistry = $coreRegistry;
      $this->adsTableFactory = $adsTableFactory;
  }

  /**
   * Mapped Grid List page.
   * @return \Magento\Backend\Model\View\Result\Page
   */
  public function execute()
  {
      $rowId = (int) $this->getRequest()->getParam('id');
      $rowData = $this->adsTableFactory->create();
      /** @var \Magento\Backend\Model\View\Result\Page $resultPage */
      if ($rowId) {
          $rowData = $rowData->load($rowId);
          $rowName = $rowData->getName();
          if (!$rowData->getId()) {
              $this->messageManager->addError(__('row data no longer exist.'));
              $this->_redirect('test_app/AdList/rowdata');
              return;
          }
      }
      $this->coreRegistry->register('row_data', $rowData);
      $resultPage = $this->resultFactory->create(ResultFactory::TYPE_PAGE);
      $title = $rowId ? __('Edit Row').$rowName : __('Add Row');
      $resultPage->getConfig()->getTitle()->prepend($title);
      return $resultPage;
  }

  protected function _isAllowed()
  {
       return $this->_authorization->isAllowed('Test_Api::test_app');
  }
}
?>

Layout XML [test_app_adlist_import.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">
    <body>
        <referenceContainer name="content">
          <block class="Test\Api\Block\Adminhtml\AdList\CustomBlock" cacheable="false" name="test_app_adlist_import" />
        </referenceContainer>
    </body>
</page>

The Block Action [CustomBlock.php]

<?php
namespace Test\api\Block\Adminhtml\AdList;

class CustomBlock extends \Magento\Backend\Block\Widget\Form\Container
{

  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 form
    * Add standard buttons
    * Add "Save and Continue" button
    *
    * @return void
    */
   protected function _construct()
   {
         $this->_objectId = 'id';
         $this->_blockGroup = 'Test_Api';
         $this->_controller = 'adminhtml_adlist';
         parent::_construct();

         if ($this->_isAllowedAction('Test_Api::test_app')) {
             $this->buttonList->update('save', 'label', __('Save'));
         } else {
             $this->buttonList->remove('save');
         }
         $this->buttonList->remove('reset');
   }

   /**
    * Getter for form header text
    *
    * @return \Magento\Framework\Phrase
    */
   public function getHeaderText()
   {
     return __('Add RoW Data');
   }


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


    /**
     * Get form action URL.
     *
     * @return string
     */
    public function getFormActionUrl()
    {
        if ($this->hasFormActionUrl()) {
            return $this->getData('form_action_url');
        }

        return $this->getUrl('*/*/save');
    }

}
?>

The Edit Form [Form.php]

<?php
namespace Test\api\Block\Adminhtml\AdList\Edit;
/**
 * Adminhtml Add New Row Form.
 */
class Form extends \Magento\Backend\Block\Widget\Form\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,
     */
    public function __construct(
        \Magento\Backend\Block\Template\Context $context,
        \Magento\Framework\Registry $registry,
        \Magento\Framework\Data\FormFactory $formFactory,
        \Magento\Cms\Model\Wysiwyg\Config $wysiwygConfig,
        array $data = []
    ) {
        $this->_wysiwygConfig = $wysiwygConfig;
        parent::__construct($context, $registry, $formFactory, $data);
    }
    /**
     * Prepare form.
     *
     * @return $this
     */
    protected function _prepareForm()
    {


        $dateFormat = $this->_localeDate->getDateFormat(\IntlDateFormatter::SHORT);
        $model = $this->_coreRegistry->registry('row_data');
        $form = $this->_formFactory->create(
            ['data' => [
                            'id' => 'edit_form',
                            'enctype' => 'multipart/form-data',
                            'action' => $this->getData('action'),
                            'method' => 'post'
                        ]
            ]
        );
        $form->setHtmlIdPrefix('test_');
$fieldset = $form->addFieldset(
'base_fieldset',
['legend' => __('Add Row'), 'class' => 'fieldset-wide']
);
        $fieldset->addField(
            'name',
            'text',
            [
                'name' => 'name',
                'label' => __('Name'),
                'id' => 'name',
                'title' => __('name'),
                'class' => 'required-entry',
                'required' => true,
            ]
        );
        $form->setUseContainer(true);
        $this->setForm($form);
        return parent::_prepareForm();
    }
}

?>
0

Magento module name should start with Capital letter. In your case, this is api. Change this to Api. Also your namespace should change. Ex.

namespace Test\Api\Block\Adminhtml\AdList\Edit;

Change this for all class.

  • it seems not to work; I updated the namespace and test, but the Edit/Form.php still not loading yet – Daniel Lo Apr 10 at 2:02
  • the name of the block action, can I use AdList ? or Adlist ? – Daniel Lo Apr 10 at 4:43
  • Yes, you can use AdList. – Sohel Rana Apr 10 at 6:53
  • You can add module somewhere for cheking – Sohel Rana Apr 10 at 6:54
  • What does the module moving to somewhere mean? After changed, the namespace from 'api' to 'Api'. I also have tried the controller, layout, and block. Every component has been connected. But, only one thing does understand, while the program goes into the block action. Both __construct and _construct are executed, but finally doesn't execute _prepareLayout function. – Daniel Lo Apr 11 at 1:28

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.