Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Created Product attribute setup patch generator #527

1 change: 1 addition & 0 deletions resources/META-INF/plugin.xml
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@
<action id="MagentoCreateADataModel" class="com.magento.idea.magento2plugin.actions.generation.NewDataModelAction" />
<action id="MagentoMessageQueue" class="com.magento.idea.magento2plugin.actions.generation.NewMessageQueueAction" />
<action id="NewDbSchema" class="com.magento.idea.magento2plugin.actions.generation.NewDbSchemaAction" />
<action id="NewEavAttribute" class="com.magento.idea.magento2plugin.actions.generation.NewEavAttributeAction" />
<add-to-group group-id="NewGroup" anchor="last"/>
</group>

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
<?php
#parse("PHP File Header.php")
#if (${NAMESPACE})

namespace ${NAMESPACE};
#end

#set ($uses = ${USES})
#foreach ($use in $uses.split(","))
use $use;
#end

class ${NAME} implements ${IMPLEMENTS} {

/**
* @var ${MODULE_DATA_SETUP_INTERFACE}
*/
private $moduleDataSetup;

/**
* @var ${EAV_SETUP_FACTORY}
*/
private $eavSetupFactory;

/**
* AddRecommendedAttribute constructor.
*
* @param ${MODULE_DATA_SETUP_INTERFACE} $moduleDataSetup
* @param ${EAV_SETUP_FACTORY} $eavSetupFactory
*/
public function __construct(
${MODULE_DATA_SETUP_INTERFACE} $moduleDataSetup,
${EAV_SETUP_FACTORY} $eavSetupFactory
) {
$this->moduleDataSetup = $moduleDataSetup;
$this->eavSetupFactory = $eavSetupFactory;
}

/**
* Get array of patches that have to be executed prior to this.
*
* Example of implementation:
*
* [
* \Vendor_Name\Module_Name\Setup\Patch\Patch1::class,
* \Vendor_Name\Module_Name\Setup\Patch\Patch2::class
* ]
*
* @return string[]
*/
public static function getDependencies()
{
return [];
}

/**
* Get aliases (previous names) for the patch.
*
* @return string[]
*/
public function getAliases()
{
return [];
}

/**
* Run code inside patch
* If code fails, patch must be reverted, in case when we are speaking about schema - then under revert
* means run PatchInterface::revert()
*
* If we speak about data, under revert means: $transaction->rollback()
*
* @return $this
*/
public function apply()
{
/** @var ${EAV_SETUP} $eavSetup */
$eavSetup = $this->eavSetupFactory->create(['setup' => $this->moduleDataSetup]);

$eavSetup->addAttribute(
${ENTITY_CLASS}::ENTITY,
'${ATTRIBUTE_CODE}',
[
#set ($attributeSet = ${ATTRIBUTE_SET})
#foreach ($attribute in $attributeSet.split(","))
$attribute,
#end
]
);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
/*
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/

package com.magento.idea.magento2plugin.actions.generation;

import com.intellij.ide.IdeView;
import com.intellij.openapi.actionSystem.AnAction;
import com.intellij.openapi.actionSystem.AnActionEvent;
import com.intellij.openapi.actionSystem.CommonDataKeys;
import com.intellij.openapi.actionSystem.DataContext;
import com.intellij.openapi.actionSystem.LangDataKeys;
import com.intellij.openapi.project.Project;
import com.intellij.psi.PsiDirectory;
import com.magento.idea.magento2plugin.MagentoIcons;
import com.magento.idea.magento2plugin.actions.generation.dialog.NewEavAttributeDialog;
import org.jetbrains.annotations.NotNull;

public class NewEavAttributeAction extends AnAction {
public static final String ACTION_NAME = "Magento 2 EAV Attribute";
public static final String ACTION_DESCRIPTION = "Create a new Magento 2 EAV Attribute";

public NewEavAttributeAction() {
super(ACTION_NAME, ACTION_DESCRIPTION, MagentoIcons.MODULE);
}

@Override
public void actionPerformed(final @NotNull AnActionEvent event) {
final DataContext dataContext = event.getDataContext();
final IdeView view = LangDataKeys.IDE_VIEW.getData(dataContext);
if (view == null) {
return;
}

final Project project = CommonDataKeys.PROJECT.getData(dataContext);
if (project == null) {
return;
}

final PsiDirectory directory = view.getOrChooseDirectory();
if (directory == null) {
return;
}

NewEavAttributeDialog.open(project, directory);
}

@Override
public boolean isDumbAware() {
return false;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package com.magento.idea.magento2plugin.actions.generation.data;

@SuppressWarnings({"PMD.UnnecessaryModifier"})
public interface EavEntityDataInterface {
public String getCode();

public String getType();

public String getLabel();

public String getInput();

public String getNamespace();

public String getModuleName();

public String getDirectory();

public String getDataPatchName();

public String getEntityClass();
}
Loading