Skip to content

Commit 2f6c738

Browse files
Added customer attribute generation
1 parent b6569ba commit 2f6c738

19 files changed

+1453
-18
lines changed

resources/META-INF/plugin.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,7 @@
8080
<group id="NewEavAttributeGroup" class="com.magento.idea.magento2plugin.actions.groups.NewEavAttributeGroup" text="Magento 2 EAV Attribute" popup="true">
8181
<action id="NewProductEavAttribute" class="com.magento.idea.magento2plugin.actions.generation.eavattribute.NewProductEavAttributeAction" />
8282
<action id="NewCatalogEavAttribute" class="com.magento.idea.magento2plugin.actions.generation.eavattribute.NewCategoryEavAttributeAction" />
83+
<action id="NewCustomerAttribute" class="com.magento.idea.magento2plugin.actions.generation.NewCustomerEavAttributeAction" />
8384
<add-to-group group-id="MagentoNewModuleFileGroup" anchor="last"/>
8485
</group>
8586

Lines changed: 113 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,113 @@
1+
<?php
2+
#parse("PHP File Header.php")
3+
#if (${NAMESPACE})
4+
namespace ${NAMESPACE};
5+
#end
6+
7+
#set ($uses = ${USES})
8+
#foreach ($use in $uses.split(","))
9+
use $use;
10+
#end
11+
12+
class ${CLASS_NAME} implements ${IMPLEMENTS}
13+
{
14+
/**
15+
* @var ${MODULE_DATA_SETUP_INTERFACE}
16+
*/
17+
private $moduleDataSetup;
18+
19+
/**
20+
* @var ${EAV_SETUP_FACTORY}
21+
*/
22+
private $eavSetupFactory;
23+
24+
/**
25+
* @var ${EAV_CONFIG_CLASS}
26+
*/
27+
private $eavConfig;
28+
29+
/**
30+
* @param ${MODULE_DATA_SETUP_INTERFACE} $moduleDataSetup
31+
* @param ${EAV_SETUP_FACTORY} $eavSetupFactory
32+
*/
33+
public function __construct(
34+
${MODULE_DATA_SETUP_INTERFACE} $moduleDataSetup,
35+
${EAV_SETUP_FACTORY} $eavSetupFactory,
36+
${EAV_CONFIG_CLASS} $eavConfig
37+
) {
38+
$this->moduleDataSetup = $moduleDataSetup;
39+
$this->eavSetupFactory = $eavSetupFactory;
40+
$this->eavConfig = $eavConfig;
41+
}
42+
43+
/**
44+
* Run code inside patch
45+
* If code fails, patch must be reverted, in case when we are speaking about schema - then under revert
46+
* means run PatchInterface::revert()
47+
*
48+
* If we speak about data, under revert means: $transaction->rollback()
49+
*
50+
* @return $this
51+
*/
52+
public function apply()
53+
{
54+
/** @var ${EAV_SETUP} $eavSetup */
55+
$eavSetup = $this->eavSetupFactory->create(['setup' => $this->moduleDataSetup]);
56+
57+
$eavSetup->addAttribute(
58+
${ENTITY_CLASS}::ENTITY,
59+
'${ATTRIBUTE_CODE}',
60+
[
61+
#set ($attributeSet = ${ATTRIBUTE_SET})
62+
#foreach ($attribute in $attributeSet.split("->"))
63+
$attribute
64+
#end
65+
]
66+
);
67+
68+
$eavSetup->addAttributeToSet(
69+
${CUSTOMER_METADATA_INTERFACE}::ENTITY_TYPE_CUSTOMER,
70+
${CUSTOMER_METADATA_INTERFACE}::ATTRIBUTE_SET_ID_CUSTOMER,
71+
null,
72+
'${ATTRIBUTE_CODE}'
73+
);
74+
75+
#if (${CUSTOMER_FORMS})
76+
$attribute = $this->eavConfig->getAttribute(${ENTITY_CLASS}::ENTITY, '${ATTRIBUTE_CODE}');
77+
$attribute->setData(
78+
'used_in_forms',
79+
[${CUSTOMER_FORMS}]
80+
);
81+
$attribute->save();
82+
#end
83+
84+
return $this;
85+
}
86+
87+
/**
88+
* Get array of patches that have to be executed prior to this.
89+
*
90+
* Example of implementation:
91+
*
92+
* [
93+
* \Vendor_Name\Module_Name\Setup\Patch\Patch1::class,
94+
* \Vendor_Name\Module_Name\Setup\Patch\Patch2::class
95+
* ]
96+
*
97+
* @return string[]
98+
*/
99+
public static function getDependencies()
100+
{
101+
return [];
102+
}
103+
104+
/**
105+
* Get aliases (previous names) for the patch.
106+
*
107+
* @return string[]
108+
*/
109+
public function getAliases()
110+
{
111+
return [];
112+
}
113+
}
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
/*
2+
* Copyright © Magento, Inc. All rights reserved.
3+
* See COPYING.txt for license details.
4+
*
5+
*/
6+
7+
package com.magento.idea.magento2plugin.actions.generation;
8+
9+
import com.intellij.openapi.project.Project;
10+
import com.intellij.psi.PsiDirectory;
11+
import com.magento.idea.magento2plugin.MagentoIcons;
12+
import com.magento.idea.magento2plugin.actions.generation.dialog.NewCustomerEavAttributeDialog;
13+
import com.magento.idea.magento2plugin.actions.generation.dialog.eavattribute.EavAttributeDialog;
14+
import com.magento.idea.magento2plugin.actions.generation.eavattribute.NewEavAttributeAction;
15+
16+
public class NewCustomerEavAttributeAction extends NewEavAttributeAction {
17+
18+
public static final String ACTION_NAME = "Customer Attribute";
19+
public static final String ACTION_DESCRIPTION = "Create a new Magento 2 EAV Customer Attribute";
20+
21+
public NewCustomerEavAttributeAction() {
22+
super(ACTION_NAME, ACTION_DESCRIPTION, MagentoIcons.MODULE);
23+
}
24+
25+
@Override
26+
protected EavAttributeDialog getDialogWindow(
27+
final Project project,
28+
final PsiDirectory directory
29+
) {
30+
return new NewCustomerEavAttributeDialog(project, directory, ACTION_NAME);
31+
}
32+
}

0 commit comments

Comments
 (0)