├── README.md └── app └── code └── Dev └── Grid ├── Controller └── Adminhtml │ ├── Category │ └── MassDelete.php │ └── Index │ └── Index.php ├── Model └── ResourceModel │ └── Category.php ├── Plugin └── AddAttributesToUiDataProvider.php ├── Ui ├── Component │ └── Category │ │ └── Listing │ │ └── Column │ │ └── Actions.php └── DataProvider │ └── Category │ ├── Listing │ └── Collection.php │ └── ListingDataProvider.php ├── etc ├── adminhtml │ ├── menu.xml │ └── routes.xml ├── di.xml └── module.xml ├── registration.php └── view └── adminhtml ├── layout └── dev_grid_index_index.xml └── ui_component └── dev_grid_category_listing.xml /README.md: -------------------------------------------------------------------------------- 1 | # Magento 2 Admin Grid Example Extension 2 | 3 | This extension creates a simple admin grid to display a list of categories that start with a letter `b` or a letter `B`. 4 | 5 | Installation instructions: 6 | 7 | * Clone the repository 8 | 9 | ```console 10 | git clone https://github.com/goivvy/admin-grid-tutorial.git 11 | ``` 12 | 13 | * Copy `app` folder 14 | 15 | ```console 16 | cp -r ~/admin-grid/tutorial/app /path/to/magento2/root/folder 17 | ``` 18 | 19 | * Install and recompile 20 | 21 | ```console 22 | php bin/magento setup:upgrade 23 | php bin/magento deploy:mode:set production 24 | ``` 25 | 26 | The grid could then be accessed at **Catalog** > **Inventory** > **Category Listing**. 27 | -------------------------------------------------------------------------------- /app/code/Dev/Grid/Controller/Adminhtml/Category/MassDelete.php: -------------------------------------------------------------------------------- 1 | filter = $filter; 29 | $this->collectionFactory = $collectionFactory; 30 | $this->categoryRepository = $categoryRepository 31 | ?: \Magento\Framework\App\ObjectManager::getInstance()->create(CategoryRepositoryInterface::class); 32 | parent::__construct($context); 33 | } 34 | 35 | public function execute() 36 | { 37 | if (!$this->getRequest()->isPost()) { 38 | throw new NotFoundException(__('Page not found')); 39 | } 40 | $collection = $this->filter->getCollection($this->collectionFactory->create()); 41 | $categoryDeleted = 0; 42 | foreach ($collection->getItems() as $category) { 43 | $this->categoryRepository->delete($category); 44 | $categoryDeleted++; 45 | } 46 | 47 | if ($categoryDeleted) { 48 | $this->messageManager->addSuccessMessage( 49 | __('A total of %1 record(s) have been deleted.', $categoryDeleted) 50 | ); 51 | } 52 | 53 | return $this->resultFactory->create(ResultFactory::TYPE_REDIRECT)->setPath('dev_grid/index/index'); 54 | } 55 | } 56 | -------------------------------------------------------------------------------- /app/code/Dev/Grid/Controller/Adminhtml/Index/Index.php: -------------------------------------------------------------------------------- 1 | pageFactory = $rawFactory; 18 | 19 | parent::__construct($context); 20 | } 21 | 22 | public function execute() 23 | { 24 | $resultPage = $this->pageFactory->create(); 25 | $resultPage->setActiveMenu('Magento_Catalog::catalog_products'); 26 | $resultPage->getConfig()->getTitle()->prepend(__('Admin Grid Tutorial Example')); 27 | 28 | return $resultPage; 29 | } 30 | } 31 | -------------------------------------------------------------------------------- /app/code/Dev/Grid/Model/ResourceModel/Category.php: -------------------------------------------------------------------------------- 1 | attributeRepository = $attributeRepository; 18 | $this->productMetadata = $productMetadata; 19 | } 20 | 21 | public function afterGetSearchResult(CategoryDataProvider $subject, SearchResult $result) { 22 | if ($result->isLoaded()) { 23 | return $result; 24 | } 25 | 26 | $edition = $this->productMetadata->getEdition(); 27 | 28 | $column = 'entity_id'; 29 | 30 | if($edition == 'Enterprise') 31 | $column = 'row_id'; 32 | 33 | $attribute = $this->attributeRepository->get('catalog_category', 'name'); 34 | 35 | $result->getSelect()->joinLeft( 36 | ['devgridname' => $attribute->getBackendTable()], 37 | "devgridname.".$column." = main_table.".$column." AND devgridname.attribute_id = ".$attribute->getAttributeId(), 38 | ['name' => "devgridname.value"] 39 | ); 40 | 41 | $result->getSelect()->where('devgridname.value LIKE "B%"'); 42 | 43 | return $result; 44 | } 45 | } 46 | -------------------------------------------------------------------------------- /app/code/Dev/Grid/Ui/Component/Category/Listing/Column/Actions.php: -------------------------------------------------------------------------------- 1 | _urlBuilder = $urlBuilder; 31 | $this->_viewUrl = $viewUrl; 32 | parent::__construct($context, $uiComponentFactory, $components, $data); 33 | } 34 | 35 | /** 36 | * Prepare Data Source 37 | * 38 | * @param array $dataSource 39 | * @return array 40 | */ 41 | public function prepareDataSource(array $dataSource) 42 | { 43 | if (isset($dataSource['data']['items'])) { 44 | foreach ($dataSource['data']['items'] as &$item) { 45 | $name = $this->getData('name'); 46 | if (isset($item['entity_id'])) { 47 | $item[$name]['view'] = [ 48 | 'href' => $this->_urlBuilder->getUrl($this->_viewUrl, ['id' => $item['entity_id']]), 49 | 'target' => '_blank', 50 | 'label' => __('View on Frontend') 51 | ]; 52 | } 53 | } 54 | } 55 | 56 | return $dataSource; 57 | } 58 | } 59 | -------------------------------------------------------------------------------- /app/code/Dev/Grid/Ui/DataProvider/Category/Listing/Collection.php: -------------------------------------------------------------------------------- 1 | addFilterToMap('entity_id', 'main_table.entity_id'); 12 | $this->addFilterToMap('name', 'devgridname.value'); 13 | parent::_initSelect(); 14 | } 15 | } 16 | -------------------------------------------------------------------------------- /app/code/Dev/Grid/Ui/DataProvider/Category/ListingDataProvider.php: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | -------------------------------------------------------------------------------- /app/code/Dev/Grid/etc/adminhtml/routes.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | -------------------------------------------------------------------------------- /app/code/Dev/Grid/etc/di.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | DevGridCategoryCollection 10 | 11 | 12 | 13 | 14 | 15 | catalog_category_entity 16 | Dev\Grid\Model\ResourceModel\Category 17 | 18 | 19 | 20 | -------------------------------------------------------------------------------- /app/code/Dev/Grid/etc/module.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | -------------------------------------------------------------------------------- /app/code/Dev/Grid/registration.php: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | -------------------------------------------------------------------------------- /app/code/Dev/Grid/view/adminhtml/ui_component/dev_grid_category_listing.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | dev_grid_category_listing.dev_grid_category_listing_data_source 6 | dev_grid_category_listing.dev_grid_category_listing_data_source 7 | 8 | dev_grid_category_columns 9 | 10 | 11 | add 12 | View Category Tree 13 | primary 14 | catalog/category/index 15 | 16 | 17 | 18 | 19 | 20 | Dev\Grid\Ui\DataProvider\Category\ListingDataProvider 21 | dev_grid_category_listing_data_source 22 | entity_id 23 | entity_id 24 | 25 | 26 | 27 | 28 | 29 | 30 | 31 | 32 | Magento_Ui/js/grid/provider 33 | 34 | 35 | 36 | 37 | 38 | 39 | 40 | 41 | 42 | dev_grid_category_listing.dev_grid_category_listing.dev_grid_category_columns.ids 43 | bottom 44 | Magento_Ui/js/grid/tree-massactions 45 | entity_id 46 | 47 | 48 | 49 | 50 | 51 | delete 52 | Delete 53 | 54 | 55 | Delete items 56 | Are you sure you want to delete selected items? 57 | 58 | 59 | 60 | 61 | 62 | 63 | 64 | 65 | 66 | 67 | 68 | Magento_Ui/js/form/element/ui-select 69 | ui/grid/filters/elements/ui-select 70 | 71 | 72 | 73 | 74 | 75 | 76 | 77 | 78 | 79 | 80 | 81 | 82 | entity_id 83 | 84 | 85 | 86 | 87 | 88 | 89 | textRange 90 | ID 91 | 25 92 | 93 | 94 | 95 | 96 | 97 | text 98 | ui/grid/cells/text 99 | 100 | 101 | 102 | 103 | 104 | text 105 | ui/grid/cells/text 106 | 107 | 108 | 109 | 110 | 111 | 112 | false 113 | 107 114 | entity_id 115 | 116 | 117 | catalog/category/view 118 | 119 | 120 | 121 | --------------------------------------------------------------------------------