DEV Community

Paboda Hettiarachchi
Paboda Hettiarachchi

Posted on • Updated on

Mage2gen - post module generation changes

I've been using to create modules which is so convenient. During a few code review sessions I received a few change requests to improve the code generated via mage2gen. I'm listing down the changes / bugs fixed.

1) Backend grid record delete - will give "undefined" in popup

'confirm' => [
    'title' => __('Delete "${ $.$ }"'),
    'message' => __('Are you sure you wan\'t to delete a "${ $.$ }" record?')
Enter fullscreen mode Exit fullscreen mode

Change "name" to your column name

2) Add Mass delete

namespace Vendor\Module\Controller\Adminhtml\Module;

use Magento\Backend\App\Action;
use Magento\Framework\Controller\ResultFactory;
use Magento\Backend\App\Action\Context;
use Magento\Ui\Component\MassAction\Filter;
use Vendor\Module\Model\ResourceModel\Module\CollectionFactory;

class MassDelete extends Action
     * @var Filter
    protected $_filter;

     * @var CollectionFactory
    protected $_collectionFactory;

     * @param Context           $context
     * @param Filter            $filter
     * @param CollectionFactory $collectionFactory
    public function __construct(
        Context $context,
        Filter $filter,
        CollectionFactory $collectionFactory
    ) {

        $this->_filter = $filter;
        $this->_collectionFactory = $collectionFactory;

     * @return \Magento\Framework\App\ResponseInterface|\Magento\Framework\Controller\ResultInterface
     * @throws \Magento\Framework\Exception\LocalizedException
    public function execute()
        $collection = $this->_filter->getCollection($this->_collectionFactory->create());
        $recordDeleted = 0;
        foreach ($collection->getItems() as $record) {
        $this->messageManager->addSuccessMessage(__('A total of %1 record(s) have been deleted.', $recordDeleted));

        return $this->resultFactory->create(ResultFactory::TYPE_REDIRECT)->setPath('*/*/index');

     * @return bool
    protected function _isAllowed()
        return $this->_authorization->isAllowed('Vendor_Module::row_data_delete');
Enter fullscreen mode Exit fullscreen mode


<listingToolbar name="listing_top">
    <bookmark name="bookmarks"/>
    <columnsControls name="columns_controls"/>
    <filters name="listing_filters"/>
    <massaction name="listing_massaction">
        <argument name="data" xsi:type="array">
            <item name="config" xsi:type="array">
                <item name="selectProvider" xsi:type="string">vendor_module_yourmodel_listing.vendor_module_yourmodel_listing.vendor_module_yourmodel_columns.ids</item>
                <item name="component" xsi:type="string">Magento_Ui/js/grid/tree-massactions</item>
                <item name="indexField" xsi:type="string">yourmodel_id</item>
        <action name="delete">
            <argument name="data" xsi:type="array">
                <item name="config" xsi:type="array">
                    <item name="type" xsi:type="string">delete</item>
                    <item name="label" xsi:type="string" translate="true">Delete</item>
                    <item name="url" xsi:type="url" path="vendor_module/yourmodel/massdelete"/>
                    <item name="confirm" xsi:type="array">
                        <item name="title" xsi:type="string" translate="true">Delete</item>
                        <item name="message" xsi:type="string" translate="true">Do you want to delete selected row record?</item>
    <paging name="listing_paging"/>
Enter fullscreen mode Exit fullscreen mode

3) Using const values in interface without adding the column names manually.
eg: yourmodel_id will be the primary key to the main model table. In the following code we will be doing manual work if we have left yourmodel_id without using it via the interface const used.

$id = $this->getRequest()->getParam('yourmodel_id');
Enter fullscreen mode Exit fullscreen mode


$id = $this->getRequest()->getParam(YourModelInterface::YOURMODEL_ID);
Enter fullscreen mode Exit fullscreen mode

Following files should be changed as well (check for $this->getRequest()->getParam value)

  • app/code/Vendor/Module/Controller/Adminhtml/YourModel/Edit.php
  • app/code/Vendor/Module/Controller/Adminhtml/YourModel/Save.php

4) Remove unnecessary files like COPYING.txt and composer.json in the module if not necessary.

5) Update the copyright message in all files. Module won't add the copyright message in .xml files

6) Clean the code of the module generated so that all necessary "use" files are added instead of calling calling classes in construct with the full paths.

7) Update the method definition comments

8) Update the variable definition comments in php classes
9) Use repostory interface to delete instead of the model

use Vendor/Module\Api\YourModelRepositoryInterface;
Enter fullscreen mode Exit fullscreen mode
Enter fullscreen mode Exit fullscreen mode

Top comments (0)