I've been using https://mage2gen.com/ 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
Vendor/Module/Ui/Component/Listing/Column/Yourclass.php
'confirm' => [
'title' => __('Delete "${ $.$data.name }"'),
'message' => __('Are you sure you wan\'t to delete a "${ $.$data.name }" record?')
]
Change "name" to your column name
2) Add Mass delete
Vendor\Module\Controller\Adminhtml\Module\MassDelete.php
<?php
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;
parent::__construct($context);
}
/**
* @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) {
$record->setId($record->getYourModelId());
$record->delete();
$recordDeleted++;
}
$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');
}
}
app/code/Vendor/Module/view/adminhtml/ui_component/vendor_module_yourmodel_listing.xml
<listingToolbar name="listing_top">
<settings>
<sticky>true</sticky>
</settings>
<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>
</item>
</argument>
<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>
</item>
</item>
</argument>
</action>
</massaction>
<paging name="listing_paging"/>
</listingToolbar>
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');
app/code/Vendor/Module/Controller/Adminhtml/YourModel/Delete.php
$id = $this->getRequest()->getParam(YourModelInterface::YOURMODEL_ID);
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;
$this->yourModelRepositoryInterface->deleteById($id);
Top comments (0)