├── Api
└── Data
│ └── GridInterface.php
├── Block
└── Adminhtml
│ └── Grid
│ ├── AddRow.php
│ └── Edit
│ └── Form.php
├── Controller
└── Adminhtml
│ └── Grid
│ ├── AddRow.php
│ ├── Index.php
│ ├── MassDelete.php
│ └── Save.php
├── Logger
├── Handler.php
└── Logger.php
├── Model
├── Grid.php
├── ResourceModel
│ ├── Grid.php
│ └── Grid
│ │ └── Collection.php
└── Status.php
├── README.md
├── Setup
└── InstallSchema.php
├── Ui
└── Component
│ └── Listing
│ └── Grid
│ └── Column
│ └── Action.php
├── composer.json
├── etc
├── adminhtml
│ ├── menu.xml
│ └── routes.xml
├── di.xml
├── frontend
│ └── routes.xml
└── module.xml
├── registration.php
└── view
└── adminhtml
├── layout
├── grid_grid_addrow.xml
└── grid_grid_index.xml
└── ui_component
└── grid_record_grid_list.xml
/Api/Data/GridInterface.php:
--------------------------------------------------------------------------------
1 | _coreRegistry = $registry;
31 | parent::__construct($context, $data);
32 | }
33 |
34 | /**
35 | * Initialize Imagegallery Images Edit Block.
36 | */
37 | protected function _construct()
38 | {
39 | $this->_objectId = 'row_id';
40 | $this->_blockGroup = 'Webkul_Grid';
41 | $this->_controller = 'adminhtml_grid';
42 | parent::_construct();
43 | if ($this->_isAllowedAction('Webkul_Grid::add_row')) {
44 | $this->buttonList->update('save', 'label', __('Save'));
45 | } else {
46 | $this->buttonList->remove('save');
47 | }
48 | $this->buttonList->remove('reset');
49 | }
50 |
51 | /**
52 | * Retrieve text for header element depending on loaded image.
53 | *
54 | * @return \Magento\Framework\Phrase
55 | */
56 | public function getHeaderText()
57 | {
58 | return __('Add RoW Data');
59 | }
60 |
61 | /**
62 | * Check permission for passed action.
63 | *
64 | * @param string $resourceId
65 | *
66 | * @return bool
67 | */
68 | protected function _isAllowedAction($resourceId)
69 | {
70 | return $this->_authorization->isAllowed($resourceId);
71 | }
72 |
73 | /**
74 | * Get form action URL.
75 | *
76 | * @return string
77 | */
78 | public function getFormActionUrl()
79 | {
80 | if ($this->hasFormActionUrl()) {
81 | return $this->getData('form_action_url');
82 | }
83 |
84 | return $this->getUrl('*/*/save');
85 | }
86 | }
87 |
--------------------------------------------------------------------------------
/Block/Adminhtml/Grid/Edit/Form.php:
--------------------------------------------------------------------------------
1 | _options = $options;
37 | $this->_wysiwygConfig = $wysiwygConfig;
38 | parent::__construct($context, $registry, $formFactory, $data);
39 | }
40 |
41 | /**
42 | * Prepare form.
43 | *
44 | * @return $this
45 | */
46 | protected function _prepareForm()
47 | {
48 | $dateFormat = $this->_localeDate->getDateFormat(\IntlDateFormatter::SHORT);
49 | $model = $this->_coreRegistry->registry('row_data');
50 | $form = $this->_formFactory->create(
51 | ['data' => [
52 | 'id' => 'edit_form',
53 | 'enctype' => 'multipart/form-data',
54 | 'action' => $this->getData('action'),
55 | 'method' => 'post'
56 | ]
57 | ]
58 | );
59 |
60 | $form->setHtmlIdPrefix('wkgrid_');
61 | if ($model->getEntityId()) {
62 | $fieldset = $form->addFieldset(
63 | 'base_fieldset',
64 | ['legend' => __('Edit Row Data'), 'class' => 'fieldset-wide']
65 | );
66 | $fieldset->addField('entity_id', 'hidden', ['name' => 'entity_id']);
67 | } else {
68 | $fieldset = $form->addFieldset(
69 | 'base_fieldset',
70 | ['legend' => __('Add Row Data'), 'class' => 'fieldset-wide']
71 | );
72 | }
73 |
74 | $fieldset->addField(
75 | 'title',
76 | 'text',
77 | [
78 | 'name' => 'title',
79 | 'label' => __('Title'),
80 | 'id' => 'title',
81 | 'title' => __('Title'),
82 | 'class' => 'required-entry',
83 | 'required' => true,
84 | ]
85 | );
86 |
87 | $wysiwygConfig = $this->_wysiwygConfig->getConfig(['tab_id' => $this->getTabId()]);
88 |
89 | $fieldset->addField(
90 | 'content',
91 | 'editor',
92 | [
93 | 'name' => 'content',
94 | 'label' => __('Content'),
95 | 'style' => 'height:36em;',
96 | 'required' => true,
97 | 'config' => $wysiwygConfig
98 | ]
99 | );
100 |
101 | $fieldset->addField(
102 | 'publish_date',
103 | 'date',
104 | [
105 | 'name' => 'publish_date',
106 | 'label' => __('Publish Date'),
107 | 'date_format' => $dateFormat,
108 | 'time_format' => 'H:mm:ss',
109 | 'class' => 'validate-date validate-date-range date-range-custom_theme-from',
110 | 'class' => 'required-entry',
111 | 'style' => 'width:200px',
112 | ]
113 | );
114 | $fieldset->addField(
115 | 'is_active',
116 | 'select',
117 | [
118 | 'name' => 'is_active',
119 | 'label' => __('Status'),
120 | 'id' => 'is_active',
121 | 'title' => __('Status'),
122 | 'values' => $this->_options->getOptionArray(),
123 | 'class' => 'status',
124 | 'required' => true,
125 | ]
126 | );
127 | $form->setValues($model->getData());
128 | $form->setUseContainer(true);
129 | $this->setForm($form);
130 |
131 | return parent::_prepareForm();
132 | }
133 | }
134 |
--------------------------------------------------------------------------------
/Controller/Adminhtml/Grid/AddRow.php:
--------------------------------------------------------------------------------
1 | coreRegistry = $coreRegistry;
38 | $this->gridFactory = $gridFactory;
39 | }
40 |
41 | /**
42 | * Mapped Grid List page.
43 | * @return \Magento\Backend\Model\View\Result\Page
44 | */
45 | public function execute()
46 | {
47 | $rowId = (int) $this->getRequest()->getParam('id');
48 | $rowData = $this->gridFactory->create();
49 | /** @var \Magento\Backend\Model\View\Result\Page $resultPage */
50 | if ($rowId) {
51 | $rowData = $rowData->load($rowId);
52 | $rowTitle = $rowData->getTitle();
53 | if (!$rowData->getEntityId()) {
54 | $this->messageManager->addError(__('row data no longer exist.'));
55 | $this->_redirect('grid/grid/rowdata');
56 | return;
57 | }
58 | }
59 |
60 | $this->coreRegistry->register('row_data', $rowData);
61 | $resultPage = $this->resultFactory->create(ResultFactory::TYPE_PAGE);
62 | $title = $rowId ? __('Edit Row Data ').$rowTitle : __('Add Row Data');
63 | $resultPage->getConfig()->getTitle()->prepend($title);
64 | return $resultPage;
65 | }
66 |
67 | protected function _isAllowed()
68 | {
69 | return $this->_authorization->isAllowed('Webkul_Grid::add_row');
70 | }
71 | }
72 |
--------------------------------------------------------------------------------
/Controller/Adminhtml/Grid/Index.php:
--------------------------------------------------------------------------------
1 | resultPageFactory = $resultPageFactory;
29 | }
30 |
31 | /**
32 | * Mapped eBay Order List page.
33 | *
34 | * @return \Magento\Backend\Model\View\Result\Page
35 | */
36 | public function execute()
37 | {
38 | $resultPage = $this->resultPageFactory->create();
39 | $resultPage->setActiveMenu('Webkul_Grid::grid_list');
40 | $resultPage->getConfig()->getTitle()->prepend(__('Grid List'));
41 | return $resultPage;
42 | }
43 |
44 | /**
45 | * Check Order Import Permission.
46 | *
47 | * @return bool
48 | */
49 | protected function _isAllowed()
50 | {
51 | return $this->_authorization->isAllowed('Webkul_Grid::grid_list');
52 | }
53 | }
54 |
--------------------------------------------------------------------------------
/Controller/Adminhtml/Grid/MassDelete.php:
--------------------------------------------------------------------------------
1 | _filter = $filter;
42 | $this->_collectionFactory = $collectionFactory;
43 | parent::__construct($context);
44 | }
45 |
46 | /**
47 | * @return \Magento\Backend\Model\View\Result\Redirect
48 | */
49 | public function execute()
50 | {
51 | $collection = $this->_filter->getCollection($this->_collectionFactory->create());
52 | $recordDeleted = 0;
53 | foreach ($collection->getItems() as $record) {
54 | $record->setId($record->getEntityId());
55 | $record->delete();
56 | $recordDeleted++;
57 | }
58 | $this->messageManager->addSuccess(__('A total of %1 record(s) have been deleted.', $recordDeleted));
59 |
60 | return $this->resultFactory->create(ResultFactory::TYPE_REDIRECT)->setPath('*/*/index');
61 | }
62 |
63 | /**
64 | * Check Category Map recode delete Permission.
65 | * @return bool
66 | */
67 | protected function _isAllowed()
68 | {
69 | return $this->_authorization->isAllowed('Webkul_Grid::row_data_delete');
70 | }
71 | }
72 |
--------------------------------------------------------------------------------
/Controller/Adminhtml/Grid/Save.php:
--------------------------------------------------------------------------------
1 | gridFactory = $gridFactory;
30 | }
31 |
32 | /**
33 | * @SuppressWarnings(PHPMD.CyclomaticComplexity)
34 | * @SuppressWarnings(PHPMD.NPathComplexity)
35 | */
36 | public function execute()
37 | {
38 | $data = $this->getRequest()->getPostValue();
39 | if (!$data) {
40 | $this->_redirect('grid/grid/addrow');
41 | return;
42 | }
43 | try {
44 | $rowData = $this->gridFactory->create();
45 | $rowData->setData($data);
46 | if (isset($data['id'])) {
47 | $rowData->setEntityId($data['id']);
48 | }
49 | $rowData->save();
50 | $this->messageManager->addSuccess(__('Row data has been successfully saved.'));
51 | } catch (\Exception $e) {
52 | $this->messageManager->addError(__($e->getMessage()));
53 | }
54 | $this->_redirect('grid/grid/index');
55 | }
56 |
57 | /**
58 | * @return bool
59 | */
60 | protected function _isAllowed()
61 | {
62 | return $this->_authorization->isAllowed('Webkul_Grid::save');
63 | }
64 | }
65 |
--------------------------------------------------------------------------------
/Logger/Handler.php:
--------------------------------------------------------------------------------
1 | _init('Webkul\Grid\Model\ResourceModel\Grid');
40 | }
41 | /**
42 | * Get EntityId.
43 | *
44 | * @return int
45 | */
46 | public function getEntityId()
47 | {
48 | return $this->getData(self::ENTITY_ID);
49 | }
50 |
51 | /**
52 | * Set EntityId.
53 | */
54 | public function setEntityId($entityId)
55 | {
56 | return $this->setData(self::ENTITY_ID, $entityId);
57 | }
58 |
59 | /**
60 | * Get Title.
61 | *
62 | * @return varchar
63 | */
64 | public function getTitle()
65 | {
66 | return $this->getData(self::TITLE);
67 | }
68 |
69 | /**
70 | * Set Title.
71 | */
72 | public function setTitle($title)
73 | {
74 | return $this->setData(self::TITLE, $title);
75 | }
76 |
77 | /**
78 | * Get getContent.
79 | *
80 | * @return varchar
81 | */
82 | public function getContent()
83 | {
84 | return $this->getData(self::CONTENT);
85 | }
86 |
87 | /**
88 | * Set Content.
89 | */
90 | public function setContent($content)
91 | {
92 | return $this->setData(self::CONTENT, $content);
93 | }
94 |
95 | /**
96 | * Get PublishDate.
97 | *
98 | * @return varchar
99 | */
100 | public function getPublishDate()
101 | {
102 | return $this->getData(self::PUBLISH_DATE);
103 | }
104 |
105 | /**
106 | * Set PublishDate.
107 | */
108 | public function setPublishDate($publishDate)
109 | {
110 | return $this->setData(self::PUBLISH_DATE, $publishDate);
111 | }
112 |
113 | /**
114 | * Get IsActive.
115 | *
116 | * @return varchar
117 | */
118 | public function getIsActive()
119 | {
120 | return $this->getData(self::IS_ACTIVE);
121 | }
122 |
123 | /**
124 | * Set IsActive.
125 | */
126 | public function setIsActive($isActive)
127 | {
128 | return $this->setData(self::IS_ACTIVE, $isActive);
129 | }
130 |
131 | /**
132 | * Get UpdateTime.
133 | *
134 | * @return varchar
135 | */
136 | public function getUpdateTime()
137 | {
138 | return $this->getData(self::UPDATE_TIME);
139 | }
140 |
141 | /**
142 | * Set UpdateTime.
143 | */
144 | public function setUpdateTime($updateTime)
145 | {
146 | return $this->setData(self::UPDATE_TIME, $updateTime);
147 | }
148 |
149 | /**
150 | * Get CreatedAt.
151 | *
152 | * @return varchar
153 | */
154 | public function getCreatedAt()
155 | {
156 | return $this->getData(self::CREATED_AT);
157 | }
158 |
159 | /**
160 | * Set CreatedAt.
161 | */
162 | public function setCreatedAt($createdAt)
163 | {
164 | return $this->setData(self::CREATED_AT, $createdAt);
165 | }
166 | }
167 |
--------------------------------------------------------------------------------
/Model/ResourceModel/Grid.php:
--------------------------------------------------------------------------------
1 | _date = $date;
40 | }
41 |
42 | /**
43 | * Initialize resource model.
44 | */
45 | protected function _construct()
46 | {
47 | $this->_init('wk_grid_records', 'entity_id');
48 | }
49 | }
50 |
--------------------------------------------------------------------------------
/Model/ResourceModel/Grid/Collection.php:
--------------------------------------------------------------------------------
1 | _init(
26 | 'Webkul\Grid\Model\Grid',
27 | 'Webkul\Grid\Model\ResourceModel\Grid'
28 | );
29 | }
30 | }
31 |
--------------------------------------------------------------------------------
/Model/Status.php:
--------------------------------------------------------------------------------
1 | __('Enabled'),'0' => __('Disabled')];
20 | return $options;
21 | }
22 |
23 | /**
24 | * Get Grid row status labels array with empty value for option element.
25 | *
26 | * @return array
27 | */
28 | public function getAllOptions()
29 | {
30 | $res = $this->getOptions();
31 | array_unshift($res, ['value' => '', 'label' => '']);
32 | return $res;
33 | }
34 |
35 | /**
36 | * Get Grid row type array for option element.
37 | * @return array
38 | */
39 | public function getOptions()
40 | {
41 | $res = [];
42 | foreach ($this->getOptionArray() as $index => $value) {
43 | $res[] = ['value' => $index, 'label' => $value];
44 | }
45 | return $res;
46 | }
47 |
48 | /**
49 | * {@inheritdoc}
50 | */
51 | public function toOptionArray()
52 | {
53 | return $this->getOptions();
54 | }
55 | }
56 |
--------------------------------------------------------------------------------
/README.md:
--------------------------------------------------------------------------------
1 | # Magento2 Create Admin Grid With CURD Operations
2 | This is an example module for Create Admin Collection Grid , Edit/Add Grid Row And Installer In Magento2
3 | ## Manually Installation
4 |
5 | Magento2 module installation is very easy, please follow the steps for installation-
6 |
7 | => Download and unzip the respective extension zip and create Webkul(vendor) and Grid(module) name folder inside your magento/app/code/ directory and then move all module's files into magento root directory Magento2/app/code/Webkul/Grid/ folder.
8 |
9 | ## Install with Composer as you go
10 | Specify the version of the module you need, and go.
11 |
12 | composer config repositories.reponame vcs https://github.com/webkulabhi/magento2-create-admin-grid-CURD-operations
13 | composer require webkulabhi/magento2-create-admin-grid-CURD-operations:dev-master
14 |
15 |
16 | ## Run following command via terminal from magento root directory
17 |
18 | $ php bin/magento setup:upgrade
19 | $ php bin/magento setup:di:compile
20 | $ php bin/magento setup:static-content:deploy
21 |
22 | => Flush the cache and reindex all.
23 |
24 | now module is properly installed
25 |
26 | ## Code explanations
27 |
28 | https://webkul.com/blog/create-grid-edit-add-grid-row-and-installer-in-magento2
29 |
--------------------------------------------------------------------------------
/Setup/InstallSchema.php:
--------------------------------------------------------------------------------
1 | startSetup();
35 |
36 | /*
37 | * Create table 'wk_grid_records'
38 | */
39 |
40 | $table = $installer->getConnection()->newTable(
41 | $installer->getTable('wk_grid_records')
42 | )->addColumn(
43 | 'entity_id',
44 | \Magento\Framework\DB\Ddl\Table::TYPE_INTEGER,
45 | null,
46 | ['identity' => true, 'nullable' => false, 'primary' => true],
47 | 'Grid Record Id'
48 | )->addColumn(
49 | 'title',
50 | \Magento\Framework\DB\Ddl\Table::TYPE_TEXT,
51 | 255,
52 | ['nullable' => false],
53 | 'Title'
54 | )->addColumn(
55 | 'content',
56 | \Magento\Framework\DB\Ddl\Table::TYPE_TEXT,
57 | '2M',
58 | ['nullable' => false],
59 | 'Post'
60 | )->addColumn(
61 | 'publish_date',
62 | \Magento\Framework\DB\Ddl\Table::TYPE_TIMESTAMP,
63 | null,
64 | [],
65 | 'Publish Date'
66 | )->addColumn(
67 | 'is_active',
68 | \Magento\Framework\DB\Ddl\Table::TYPE_SMALLINT,
69 | null,
70 | [],
71 | 'Active Status'
72 | )->addColumn(
73 | 'created_at',
74 | \Magento\Framework\DB\Ddl\Table::TYPE_TIMESTAMP,
75 | null,
76 | [
77 | 'nullable' => false,
78 | 'default' => \Magento\Framework\DB\Ddl\Table::TIMESTAMP_INIT,
79 | ],
80 | 'Creation Time'
81 | )->addColumn(
82 | 'update_time',
83 | \Magento\Framework\DB\Ddl\Table::TYPE_TIMESTAMP,
84 | null,
85 | [],
86 | 'Modification Time'
87 | )->setComment(
88 | 'Row Data Table'
89 | );
90 |
91 | $installer->getConnection()->createTable($table);
92 | $installer->endSetup();
93 | }
94 | }
95 |
--------------------------------------------------------------------------------
/Ui/Component/Listing/Grid/Column/Action.php:
--------------------------------------------------------------------------------
1 | _urlBuilder = $urlBuilder;
46 | $this->_editUrl = $editUrl;
47 | parent::__construct($context, $uiComponentFactory, $components, $data);
48 | }
49 |
50 | /**
51 | * Prepare Data Source.
52 | *
53 | * @param array $dataSource
54 | *
55 | * @return array
56 | */
57 | public function prepareDataSource(array $dataSource)
58 | {
59 | if (isset($dataSource['data']['items'])) {
60 | foreach ($dataSource['data']['items'] as &$item) {
61 | $name = $this->getData('name');
62 | if (isset($item['entity_id'])) {
63 | $item[$name]['edit'] = [
64 | 'href' => $this->_urlBuilder->getUrl(
65 | $this->_editUrl,
66 | ['id' => $item['entity_id']]
67 | ),
68 | 'label' => __('Edit'),
69 | ];
70 | }
71 | }
72 | }
73 |
74 | return $dataSource;
75 | }
76 | }
77 |
--------------------------------------------------------------------------------
/composer.json:
--------------------------------------------------------------------------------
1 | {
2 | "name": "webkulabhi/magento2-create-admin-grid-curd-operations",
3 | "description": "Here We Create Collection Grid , Edit/Add Grid Row And Installer In Magento2",
4 | "require": {
5 | "php": "~5.5.0|~5.6.0|~7.0.0|7.1.*|7.2.*"
6 | },
7 | "type": "magento2-module",
8 | "version": "2.0.0",
9 | "license": [
10 | "OSL-3.0",
11 | "AFL-3.0"
12 | ],
13 | "autoload": {
14 | "files": [
15 | "registration.php"
16 | ],
17 | "psr-4": {
18 | "Webkul\\Grid\\": ""
19 | }
20 | }
21 | }
22 |
--------------------------------------------------------------------------------
/etc/adminhtml/menu.xml:
--------------------------------------------------------------------------------
1 |
2 |
11 |