├── 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 |