Skip to content

Commit 95c7134

Browse files
author
Vitaliy
authored
Merge pull request #476 from bohdan-harniuk/add-entity-columns-to-grid
Added entity columns to grid, recreated view controller, bug fixing, added actions column generation, test coverage
2 parents 5d9275a + cb89409 commit 95c7134

File tree

27 files changed

+1311
-48
lines changed

27 files changed

+1311
-48
lines changed

resources/META-INF/plugin.xml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -232,6 +232,8 @@
232232
<internalFileTemplate name="Magento Entity Save Controller Class"/>
233233
<internalFileTemplate name="Magento Entity Data Mapper"/>
234234
<internalFileTemplate name="Magento Save Entity Command Model"/>
235+
<internalFileTemplate name="Magento Entity Index Adminhtml Controller Class"/>
236+
<internalFileTemplate name="Magento Grid Ui Component Action Column Class"/>
235237

236238
<defaultLiveTemplates file="/liveTemplates/MagentoPWA.xml"/>
237239

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
<column name="${COLUMN_NAME}">
2+
<settings>
3+
#if (${COLUMN_FILTER})
4+
<filter>${COLUMN_FILTER}</filter>
5+
#end
6+
<label translate="true">${COLUMN_LABEL}</label>
7+
</settings>
8+
</column>

resources/fileTemplates/code/Magento Grid UI Component Column.xml.html

Whitespace-only changes.
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
<?php
2+
#parse("PHP File Header.php")
3+
4+
namespace ${NAMESPACE};
5+
6+
#set($uses = ${USES})
7+
#foreach ($use in $uses.split(","))
8+
use $use;
9+
#end
10+
11+
/**
12+
* ${ENTITY_NAME} backend index (list) controller.
13+
*/
14+
class ${CLASS_NAME} extends ${PARENT_CLASS_NAME} implements ${HTTP_GET_METHOD}
15+
{
16+
/**
17+
* Authorization level of a basic admin session.
18+
*/
19+
const ADMIN_RESOURCE = '${ACL}';
20+
21+
/**
22+
* Execute action based on request and return result.
23+
*
24+
* @return ${RESULT}|${RESPONSE}
25+
*/
26+
public function execute()
27+
{
28+
$resultPage = $this->resultFactory->create(${RESULT_FACTORY}::TYPE_PAGE);
29+
30+
$resultPage->setActiveMenu('${MENU}');
31+
$resultPage->addBreadcrumb(__('${ENTITY_NAME}'), __('${ENTITY_NAME}'));
32+
$resultPage->addBreadcrumb(__('Manage ${ENTITY_NAME}s'), __('Manage ${ENTITY_NAME}s'));
33+
$resultPage->getConfig()->getTitle()->prepend(__('${ENTITY_NAME} List'));
34+
35+
return $resultPage;
36+
}
37+
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
<html lang="en">
2+
<body>
3+
<p face="verdana" size="-1">
4+
5+
</p>
6+
7+
<table width="100%" border="0" cellpadding="5" cellspacing="0" style="border-collapse: collapse">
8+
<tr>
9+
<td colspan="3"><font face="verdana" size="-1">Template's predefined variables:</font></td>
10+
</tr>
11+
<tr>
12+
<td valign="top"><nobr><font face="verdana" size="-2"><b>${NAMESPACE}</b></font></nobr></td>
13+
<td width="10">&nbsp;</td>
14+
<td width="100%" valign="top"><font face="verdana" size="-1"></font></td>
15+
</tr>
16+
</table>
17+
</body>
18+
</html>
Lines changed: 122 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,122 @@
1+
<?php
2+
#parse("PHP File Header.php")
3+
4+
namespace ${NAMESPACE};
5+
6+
#set($uses = ${USES})
7+
#foreach ($use in $uses.split(","))
8+
use $use;
9+
#end
10+
11+
/**
12+
* Class to build edit and delete link for each item.
13+
*/
14+
class ${CLASS_NAME} extends ${PARENT_CLASS}
15+
{
16+
/**
17+
* Entity name.
18+
*/
19+
private const ENTITY_NAME = '${ENTITY_NAME}';
20+
21+
/**
22+
* Url paths.
23+
* #@+
24+
*/
25+
private const EDIT_URL_PATH = '${EDIT_URL_PATH}';
26+
private const DELETE_URL_PATH = '${DELETE_URL_PATH}';
27+
/** #@- */
28+
29+
/**
30+
* @var ${URL}
31+
*/
32+
private $urlBuilder;
33+
34+
/**
35+
* @param ${CONTEXT} $context
36+
* @param ${UI_COMPONENT_FACTORY} $uiComponentFactory
37+
* @param ${URL} $urlBuilder
38+
* @param array $components
39+
* @param array $data
40+
*/
41+
public function __construct(
42+
${CONTEXT} $context,
43+
${UI_COMPONENT_FACTORY} $uiComponentFactory,
44+
${URL} $urlBuilder,
45+
array $components = [],
46+
array $data = []
47+
) {
48+
parent::__construct(
49+
$context,
50+
$uiComponentFactory,
51+
$components,
52+
$data
53+
);
54+
$this->urlBuilder = $urlBuilder;
55+
}
56+
57+
/**
58+
* @inheritDoc
59+
*/
60+
public function prepareDataSource(array $dataSource): array
61+
{
62+
#set($entityIdAccessor = "['" + ${ENTITY_ID} + "']")
63+
if (isset($dataSource['data']['items'])) {
64+
foreach ($dataSource['data']['items'] as &$item) {
65+
if (isset($item$entityIdAccessor)) {
66+
$entityName = static::ENTITY_NAME;
67+
$urlData = ['${ENTITY_ID}' => $item$entityIdAccessor];
68+
69+
$editUrl = $this->urlBuilder->getUrl(static::EDIT_URL_PATH, $urlData);
70+
$deleteUrl = $this->urlBuilder->getUrl(static::DELETE_URL_PATH, $urlData);
71+
72+
#set($columnNameAccessor = "[$this->getData('name')]")
73+
$item$columnNameAccessor = [
74+
'edit' => $this->getActionData($editUrl, (string) __('Edit')),
75+
'delete' => $this->getActionData(
76+
$deleteUrl,
77+
(string) __('Delete'),
78+
(string) __('Delete %1', $entityName),
79+
(string) __('Are you sure you want to delete a %1 record?', $entityName)
80+
)
81+
];
82+
}
83+
}
84+
}
85+
86+
return $dataSource;
87+
}
88+
89+
/**
90+
* Get action link data array.
91+
*
92+
* @param string $url
93+
* @param string $label
94+
* @param string|null $dialogTitle
95+
* @param string|null $dialogMessage
96+
*
97+
* @return array
98+
*/
99+
private function getActionData(
100+
string $url,
101+
string $label,
102+
?string $dialogTitle = null,
103+
?string $dialogMessage = null
104+
): array {
105+
$data = [
106+
'href' => $url,
107+
'label' => $label,
108+
'post' => true,
109+
'__disableTmpl' => true
110+
];
111+
112+
#set($confirmKeyAccessor = "['confirm']")
113+
if ($dialogTitle && $dialogMessage) {
114+
$data$confirmKeyAccessor = [
115+
'title' => $dialogTitle,
116+
'message' => $dialogMessage
117+
];
118+
}
119+
120+
return $data;
121+
}
122+
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
<html lang="en">
2+
<body>
3+
<p face="verdana" size="-1">
4+
5+
</p>
6+
7+
<table width="100%" border="0" cellpadding="5" cellspacing="0" style="border-collapse: collapse">
8+
<tr>
9+
<td colspan="3"><font face="verdana" size="-1">Template's predefined variables:</font></td>
10+
</tr>
11+
<tr>
12+
<td valign="top"><nobr><font face="verdana" size="-2"><b>${NAMESPACE}</b></font></nobr></td>
13+
<td width="10">&nbsp;</td>
14+
<td width="100%" valign="top"><font face="verdana" size="-1"></font></td>
15+
</tr>
16+
</table>
17+
</body>
18+
</html>

resources/fileTemplates/internal/Magento UI Component Grid XML.xml.ft

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,5 +56,16 @@
5656
<sorting>desc</sorting>
5757
</settings>
5858
</column>
59+
#if (${COLUMNS})
60+
#set($columns = ${COLUMNS})
61+
#foreach ($column in $columns.split("\n"))
62+
$column
63+
#end
64+
#end
65+
<actionsColumn name="actions" class="${ACTION_COLUMN}">
66+
<settings>
67+
<indexField>${ID_FIELD_NAME}</indexField>
68+
</settings>
69+
</actionsColumn>
5970
</columns>
6071
</listing>
Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
/*
2+
* Copyright © Magento, Inc. All rights reserved.
3+
* See COPYING.txt for license details.
4+
*/
5+
6+
package com.magento.idea.magento2plugin.actions.generation.data;
7+
8+
import org.jetbrains.annotations.NotNull;
9+
10+
public class AdminListViewEntityActionData {
11+
private final String moduleName;
12+
private final String entityName;
13+
private final String namespace;
14+
private final String classFqn;
15+
private final String acl;
16+
private final String menu;
17+
18+
/**
19+
* Magento entity index adminhtml controller data constructor.
20+
*
21+
* @param moduleName String
22+
* @param entityName String
23+
* @param namespace String
24+
* @param acl String
25+
* @param menu String
26+
*/
27+
public AdminListViewEntityActionData(
28+
final @NotNull String moduleName,
29+
final @NotNull String entityName,
30+
final @NotNull String namespace,
31+
final @NotNull String classFqn,
32+
final @NotNull String acl,
33+
final @NotNull String menu
34+
) {
35+
this.moduleName = moduleName;
36+
this.entityName = entityName;
37+
this.namespace = namespace;
38+
this.classFqn = classFqn;
39+
this.acl = acl;
40+
this.menu = menu;
41+
}
42+
43+
/**
44+
* Get module name.
45+
*
46+
* @return String
47+
*/
48+
public String getModuleName() {
49+
return moduleName;
50+
}
51+
52+
/**
53+
* Get entity name.
54+
*
55+
* @return String
56+
*/
57+
public String getEntityName() {
58+
return entityName;
59+
}
60+
61+
/**
62+
* Get namespace.
63+
*
64+
* @return String
65+
*/
66+
public String getNamespace() {
67+
return namespace;
68+
}
69+
70+
/**
71+
* Get class fqn.
72+
*
73+
* @return String
74+
*/
75+
public String getClassFqn() {
76+
return classFqn;
77+
}
78+
79+
/**
80+
* Get ACL resource id.
81+
*
82+
* @return String
83+
*/
84+
public String getAcl() {
85+
return acl;
86+
}
87+
88+
/**
89+
* Get menu resource id.
90+
*
91+
* @return String
92+
*/
93+
public String getMenu() {
94+
return menu;
95+
}
96+
}

0 commit comments

Comments
 (0)